首先道歉,如果已经有这样的话题,但我还没有找到......我需要知道如何处理一个非常大的数字,如789 ^ 2346的结果:
#include <iostream>
#include <cmath>
using namespace std;
int main () {
cout << pow(789,2346) << endl;
}
Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多精确的算法来识别32位和64位整数的最高位(包括SO上的其他帖子).但我正在使用BigIntegers,并将处理长达4000位的数字.(BigInteger将希尔伯特指数保持为Hilbert空间填充曲线,该曲线在分形深度为4的情况下蜿蜒通过1000维超立方体.)但是大部分情况将涉及可以适合64位整数的数字,所以我想要一个最适合常见情况的解决方案,但可以处理极端情况.
天真的方式是:
BigInteger n = 234762348763498247634;
int count = 0;
while (n > 0) {
n >>= 1;
count++;
}
Run Code Online (Sandbox Code Playgroud)
我正在考虑将常见情况转换为Longs并使用64位算法,否则使用不同的算法来处理真正的大数字.但我不确定转换为Long的成本是多少,以及这是否会影响在64位数量上进行剩余计算的效率.有什么想法吗?
该功能的一个预期用途是帮助优化逆灰度代码计算.
更新.我编写了两种方法并运行了基准测试.
如果数字非常大(我高达10000位),那么Log的速度提高了3.5倍.
一百万次调用MostSignificantBitUsingLog(可转换为Long)已经过了96毫秒.
对EverySignificantBitUsingBinarySearch(可转换为Long)的一百万次调用已过去42毫秒.
对于MostSignificantBitUsingLog进行一万次调用已经过了74毫秒(太大而无法转换).
对于MostSignificantBitUsingBinarySearch(太大而无法转换)的一万次调用已经过了267毫秒.
以下是使用Log的代码:
public static int MostSignificantBitUsingLog(BigInteger i)
{
int bit;
if (i == 0)
bit = -1;
else
bit = (int)BigInteger.Log(i, 2.0);
return bit;
}
Run Code Online (Sandbox Code Playgroud)
这是我的二元搜索方法.可以改进以将二进制除法扩展到BigInteger范围.我会尝试下一步.
public static int MostSignificantBitUsingBinarySearch(BigInteger i)
{
int bit;
if (i.IsZero)
bit = -1;
else if (i < ulong.MaxValue)
{
ulong y = (ulong)i;
ulong s;
bit …Run Code Online (Sandbox Code Playgroud) 我试图BigInteger沿着Math.Log10方法使用值.
final BigInteger answerNo = fact;
final int digits = 1 + (int)Math.floor(Math.log10(answerNo));
Run Code Online (Sandbox Code Playgroud)
不幸的是,编译器说不兼容的类型.
如果我将ints改为BigIntegers,它仍然不喜欢它.
嗨,我有一个算法,我需要将操作应用于BigInt.
我知道可以使用Maths类操作BigInt,例如:
import java.math.*;
BigInteger a;
BigInteger b = BigInteger.ZERO;
BigInteger c = BigInteger.ONE;
BigInteger d = new BigInteger ("3");
BigInteger e = BigInteger.valueOf(5);
a.multiply(b);
a.add(b);
a.substract(b);
a.divide(b);
Run Code Online (Sandbox Code Playgroud)
我需要能够应用大于一段时间的条件,例如
while (a > 0) {
Run Code Online (Sandbox Code Playgroud)
这给了我一个语法错误,说"二元运算符'>'的错误操作数类型,第一种类型:java.math.BigInteger,第二种类型:int.
我还需要能够将modulo(%)运算符应用于BigInteger.
b = a % c;
Run Code Online (Sandbox Code Playgroud)
任何人都可以提出这样做的方法吗?
如果没有解决方案,那么我将不得不以某种方式使用reduce函数将我的BigInteger操作为一个独特的Long(这远非理想).
Silverzx.
我有这样的程序:
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
typedef long long unsigned int T_num;
typedef long double T_ld;
int main(array<System::String ^> ^args) {
T_num a = numeric_limits<T_num>::max();
T_ld b = numeric_limits<T_ld>::max();
if ( b > a ) {
cout << "decimal is bigger than integer" << endl;
} else {
cout << "integer is bigger than decimal" << endl;
}
T_num c;
b = a;
c = floor(b);
if ( c == a) {
cout << "OK" …Run Code Online (Sandbox Code Playgroud) 我试图找出如何打印出一个bigint或System.Numerics.BigInteger在F#中的值.我在这里找到了一个试图解释如何执行此操作的站点,但在Visual Studio编辑器中,它被标记为错误.到目前为止,我只是尝试一些简单的事情:
printfn "bigInt: %A " 123456789I
Run Code Online (Sandbox Code Playgroud)
但这被标记为:
为什么这不起作用?我怎么打印出来bigint?
码:
[<EntryPoint>]
let main =
printfn "bigInt: %A " 123456789I
Run Code Online (Sandbox Code Playgroud) 我试图在java中计算一个表达式,即在String中,
99999999999999999^99999999999999999
我想计算这个数模1000000007.我目前试图将大数字存储为double,但是使用double的模数给我NaN.有人可以帮忙吗?
我正在尝试执行Math.pow()操作来查找10到长数的幂.结果预计会非常大,因此,我将其存储在BigInteger中.我试过以下:
tempBigInt = java.math.BigDecimal.valueOf(Math.pow(10, i)).toBigInteger();
Run Code Online (Sandbox Code Playgroud)
我通过一个循环来运行它,其中'i'一直在增加,直到满足某个条件.此代码工作正常,除非它计算Math.pow(10,23).奇怪的是,这是唯一出错的价值,从而使我的最终结果不正确.
我也尝试通过我的IDE验证这一点,看起来Java不喜欢Math.pow(10,23).可能是一个错误.如果有人知道是什么导致了这种异常行为,你能否解释一下,并且可能提供一种更好,无错误的方法来实现这一行代码?[tempBigInt是一个BigInteger,我很长]
我有一个String数组,我想使用Java 8流将其转换为BigInteger数组。
String[] output = bigSorting(new String[]{"31415926535897932384626433832795", "1", "4900146572543628830293235422623540449026979", "10", "57500297590012603652986133599394871645776460", "5",
"497010206818067722087306230802257700034825862515267073569769100385728461314", "57500297590012603652986133599394871645776460497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314497010206818067722087306230802257700034825862515267073569769100385728461314"});
Object[] unsortedBigIntegerArr = convertFromStringArrayToBigIntegerArray(output);
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的方法,但是我无法获取BigInteger数组,但是却能够获取Object数组。
private static Object[] convertFromStringArrayToBigIntegerArray(String[] unsorted) {
return Arrays.stream(unsorted).map(BigSorting2::convertFromStringToBigInteger).toArray();
}
private static BigInteger convertFromStringToBigInteger(String unsorted) {
return new BigInteger(unsorted);
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以使用Java 8流完全完成它。
我有一个字符串,我通过解析转换为BigInteger然后将其向左移3位并再次转换为字符串.问题是它总是在实际值之前输出额外的位.例如:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim positiveString As String = "C0020ACB1086886D8C2E4D2DEDC726A6"
Dim posBigInt As BigInteger = 0
posBigInt = BigInteger.Parse(positiveString, System.Globalization.NumberStyles.AllowHexSpecifier)
posBigInt = posBigInt << Hex(3)
RichTextBox1.Text = posBigInt.ToString("X")
End Sub
Run Code Online (Sandbox Code Playgroud)
价值之前的"E"是我无法解释的.我尝试了不同的十六进制字符串但它总是产生那些额外的位 我究竟做错了什么?
biginteger ×10
java ×5
c++ ×2
math ×2
bigdecimal ×1
bit ×1
bit-shift ×1
c# ×1
casting ×1
class ×1
exponent ×1
f# ×1
java-8 ×1
java-stream ×1
long-integer ×1
modulo ×1
operators ×1
pow ×1
vb.net ×1