标签: biginteger

有没有办法在 R 中精确存储大量数字?

有没有办法在 R 中精确地存储大量数字?

double存储为二进制小数,其精度随值而变化,整数的范围限制为 4 个字节。

如果我想精确地存储一个非常大的数字怎么办?

r biginteger

4
推荐指数
1
解决办法
672
查看次数

如何从文件中读取/写入 C# BigIntegers?

在我的一个课程中,我有一个例程来读取和写入 Decimal 类型的数组(使用BinaryReader/BinaryWriter和方法,即:ReadDecimal()Write()

BinaryReader inputReader = new BinaryReader(File.OpenRead(BaseFilePath));
for (int x = 0; x < 6; x++) {
    for (int y = 0; y < m_Codes[x].GetLength(0); y++) {
        for (int z = 0; z < m_Codes[x].GetLength(1); z++) {
            m_Codes[x][y, z] = inputReader.ReadDecimal();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和

for (int x = 0; x < 6; x++) {
    for (int y = 0; y < m_Codes[x].GetLength(0); y++) {
        for (int z = 0; z < m_Codes[x].GetLength(1); …
Run Code Online (Sandbox Code Playgroud)

c# serialization biginteger

4
推荐指数
1
解决办法
1458
查看次数

C# 中的 MillerRabin 素性测试

欢迎。我正在尝试实施 MillerRabin 测试来检查给定的大数是否为素数。这是我的代码:

 public static bool MillerRabinTest(BigInteger number)
        {

            BigInteger d;
            var n = number - 1;
            var s = FindK(n, out d);

            BigInteger a = 2;
            BigInteger y = Calc(a, d, number);  //a^d mod number
            if (y != BigInteger.One && y != n)
            {
                for (var r = 1; r <= s - 1; r++)
                {
                    y = Calc(y, 2, number);
                    if (y == 1)
                        return false;  
                }

                if (y != n)
                    return false;
            }
            return true; //it is …
Run Code Online (Sandbox Code Playgroud)

c# biginteger primality-test

4
推荐指数
1
解决办法
3239
查看次数

我可以在C#中找到BigInteger的位数吗?

我正在解决这个问题,他们要求第一个斐波纳契数为1000位的索引,我的第一个想法是类似于:

BigInteger x = 1;
BigInteger y = 1;
BigInteger tmp = 0;

int currentIndex = 2;
while (x.NoOfDigits < 1000)
{
    tmp = x + y;
    y = x;
    x = tmp;
    currentIndex++;
}
return currentIndex;
Run Code Online (Sandbox Code Playgroud)

但是,据我所知,没有方法可以计算BigInteger的位数.这是真的?绕过它的一种方法是使用BigInteger的.ToString().Length方法,但我被告知字符串处理很慢.

BigInteger也有一个.ToByteArray(),我想把BigInteger转换成一个字节数组并检查该数组的长度 - 但我不认为这唯一地决定了BigInteger中的位数.

为了它的价值,我实现了另一种解决方法,即手动将Fibonacci数存储在数组中,并在数组填满后立即停止,并将其与基于.ToString的方法进行比较,大约为2.5时间慢,但第一种方法需要0.1秒,这也似乎很长一段时间.

编辑:我已经在下面的答案中测试了这两个建议(一个是BigInteger.Log,一个是MaxLimitMethod).我得到以下运行时间:

  • 原始方法:00:00:00.0961957
  • StringMethod:00:00:00.1535350
  • BigIntegerLogMethod:00:00:00.0387479
  • MaxLimitMethod:00:00:00.0019509

程序

using System;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        Stopwatch clock = new Stopwatch();
        clock.Start();
        int index1 = Algorithms.IndexOfNDigits(1000);
        clock.Stop();
        var elapsedTime1 = …
Run Code Online (Sandbox Code Playgroud)

c# arrays biginteger

4
推荐指数
2
解决办法
1448
查看次数

BigInteger.Parse 工作不正常

这对我来说很奇怪,但你可以试试这个简短的代码:

var num1 = BigInteger.Parse("1e+9999", NumberStyles.AllowExponent);
var num2 = BigInteger.Parse("1e+9998", NumberStyles.AllowExponent);
var div = num1 / num2; // is 1, but must be 10
var eq = num1 == num2; // true, but must be false
Run Code Online (Sandbox Code Playgroud)

建议?

.net math biginteger

4
推荐指数
1
解决办法
126
查看次数

将大整数转换为十进制字符串

冒着这个问题被投票重复,甚至被关闭的风险,我提出了这个问题。

背景

在“普通”数据类型(例如 int、long long 等)中,要将二进制数值转换为十进制字符串,您可以执行以下操作(以伪代码形式):

Set length = 0
Set divisor to largest base10 value the data type will hold (Divisor).
  Loop
    Divide number in question by divisor.
    Place result in a string at position length.
    Increment the length by 1.
    Divide the divisor by 10.
Reverse the string.
Print the string.
Run Code Online (Sandbox Code Playgroud)

(大多数)任何语言的实际实现都是相当简单的。

问题

我在使用上述方法时遇到的问题是,对于大整数(也称为任意精度算术),没有最大的基数 10 值可以开始。所以问题是“如果无法知道除数是多少,如何将除数初始化为最大可能的 base10 值?”

我尝试过的

仍在尝试起草解决方案。

研究

我在这里找到的一些链接包括以下内容:

将“大”十六进制数(字符串格式)转换为十进制数(字符串格式),无需 BigInteger 类

C:打印一个以 10 为基数的 BigInteger

将 BigInteger 转换为十进制(基数 10)字符串的最快方法?

将“大”十六进制数(字符串格式)转换为十进制数(字符串格式),无需 BigInteger 类 …

c biginteger format-conversion bigint

4
推荐指数
1
解决办法
3953
查看次数

Java 的 BigInteger 符号数值如何工作

考虑以下代码:

int i = 1;
System.out.println("1 binary: " + Long.toBinaryString(i));
long ri = Long.reverse(i);
System.out.println("1 reverse bit decimal: " + ri);
System.out.println("1 reverse bit binary: "+ Long.toBinaryString(ri));
BigInteger bil = new BigInteger(1, Longs.toByteArray(ri));
System.out.println("1 Sign-Magnitude BigInteger toString: " + bil.toString());
Run Code Online (Sandbox Code Playgroud)

输出是:

1 binary: 1
1 reverse bit decimal: -9223372036854775808
1 reverse bit binary: 1000000000000000000000000000000000000000000000000000000000000000
1 Sign-Magnitude BigInteger toString: 9223372036854775808
Run Code Online (Sandbox Code Playgroud)

谁能帮助解释为什么“1 Sign-Magnitude BigInteger toString:”的值为 9223372036854775808 (2^63)?

reverse biginteger bit

4
推荐指数
1
解决办法
2328
查看次数

如何获取BigInt的位数?

这里或这里没有bitLength()记录方法...如何检查本机 BigInt 值的位数?

我正在寻找一种“直接的方式”,通过一些内部信息......或者任何比转换更快的东西x.toString(2)。


笔记

  • 并不理想,但中间的解决方法(似乎比 更快toString())是使用基于log2(x) ... 的数学公式,适用于 BigInt。

  • 这个lib 开发了一个具体的integerLogarithm(value, base)计算log2...并实现了一个bitLength()方法(没有Math.floor()嗡嗡声...),但是有很多依赖项,它是一头大象...

javascript biginteger

4
推荐指数
2
解决办法
1997
查看次数

使用流读取文本文件并保存到 BigInteger 数组

读取文本文件、拆分数字,并将它们全部保存到BigInteger数组中的正确方法是什么?

BigInteger[] a = new BigInteger[1000];
try (Stream<String> stream = Files.lines(Paths.get(filePath))) {

} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

可以直接完成还是应该先将整个文件保存为一个大文件String,然后用流拆分?

String content = new String(Files.readAllBytes(Paths.get(filePath)));
Run Code Online (Sandbox Code Playgroud)

java file biginteger java-stream

4
推荐指数
1
解决办法
106
查看次数

为什么 C# 和 Java BigInteger 转换 byte[] 的方式不同?

这是Java代码:

new BigInteger("abc".getBytes()).toString();
Run Code Online (Sandbox Code Playgroud)

结果是6382179。

我想在 C# 中得到相同的结果,但是当我使用以下代码时:

(new System.Numerics.BigInteger(System.Text.Encoding.ASCII.GetBytes("abc"))).ToString();
Run Code Online (Sandbox Code Playgroud)

我得到6513249。

如何在 C# 中以与 Java 相同的方式转换字符串?

c# java biginteger

4
推荐指数
1
解决办法
105
查看次数