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

本文关键字:BigInteger 我可以 | 更新日期: 2023-09-27 18:25:27

我正在解决这个问题,他们要求第一个斐波那契数的索引 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;
但是,据

我所知,没有计算 BigInteger 位数的方法。这是真的吗?规避它的一种方法是使用 .ToString((.BigInteger的长度方法,但我被告知字符串处理很慢。

BigInteger 还有一个 .ToByteArray((,我想过将 BigInteger 转换为字节数组并检查该数组的长度 - 但我不认为这唯一决定了 BigInteger 中的位数。

对于它的价值,我

实现了另一种解决它的方法,即手动将斐波那契数存储在数组中,一旦数组已满,它就会停止,我将其与 .基于ToString的方法,大约慢2.5倍,但第一种方法需要0.1秒,这似乎也很长。

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

    原始方法:00:00
  • :00.0961957
  • 字符串方法:00
  • :00:00.1535350
  • 大整数日志方法:00:00:00.0387479
  • 最大限制方法: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 = clock.Elapsed;
        Console.WriteLine(index1);
        Console.WriteLine("Original method: {0}",elapsedTime1);
        Console.ReadKey();
        clock.Reset();
        clock.Start();
        int index2 = Algorithms.StringMethod(1000);
        clock.Stop();
        var elapsedTime2 = clock.Elapsed;
        Console.WriteLine(index2);
        Console.WriteLine("StringMethod: {0}", elapsedTime2);
        Console.ReadKey();
        clock.Reset();
        clock.Start();
        int index3 = Algorithms.BigIntegerLogMethod(1000);
        clock.Stop();
        var elapsedTime3 = clock.Elapsed;
        Console.WriteLine(index3);
        Console.WriteLine("BigIntegerLogMethod: {0}", elapsedTime3);
        Console.ReadKey();
        clock.Reset();
        clock.Start();
        int index4 = Algorithms.MaxLimitMethod(1000);
        clock.Stop();
        var elapsedTime4 = clock.Elapsed;
        Console.WriteLine(index4);
        Console.WriteLine("MaxLimitMethod: {0}", elapsedTime4);
        Console.ReadKey();

    }
}
static class Algorithms
{
    //Find the index of the first Fibonacci number of n digits
    public static int IndexOfNDigits(int n)
    {
        if (n == 1) return 1;
        int[] firstNumber = new int[n];
        int[] secondNumber = new int[n];
        firstNumber[0] = 1;
        secondNumber[0] = 1;
        int currentIndex = 2;
        while (firstNumber[n-1] == 0)
        {
            int carry = 0, singleSum = 0;
            int[] tmp = new int[n]; //Placeholder for the sum
            for (int i = 0; i<n; i++)
            {
                singleSum = firstNumber[i] + secondNumber[i];
                if (singleSum >= 10) carry = 1;
                else carry = 0;
                tmp[i] += singleSum % 10;
                if (tmp[i] >= 10)
                {
                    tmp[i] = 0;
                    carry = 1;
                }
                int countCarries = 0;
                while (carry == 1)
                {
                    countCarries++;
                    if (tmp[i + countCarries] == 9)
                    {
                        tmp[i + countCarries] = 0;
                        tmp[i + countCarries + 1] += 1;
                        carry = 1;
                    }
                    else
                    {
                        tmp[i + countCarries] += 1;
                        carry = 0;
                    }
                }
            }
            for (int i = 0; i < n; i++ )
            {
                secondNumber[i] = firstNumber[i];
                firstNumber[i] = tmp[i];
            }
            currentIndex++;
        }
        return currentIndex;
    }
    public static int StringMethod(int n)
    {
        BigInteger x = 1;
        BigInteger y = 1;
        BigInteger tmp = 0;
        int currentIndex = 2;
        while (x.ToString().Length < n)
        {
            tmp = x + y;
            y = x;
            x = tmp;
            currentIndex++;
        }
        return currentIndex;
    }
    public static int BigIntegerLogMethod(int n)
    {
        BigInteger x = 1;
        BigInteger y = 1;
        BigInteger tmp = 0;
        int currentIndex = 2;
        while (Math.Floor(BigInteger.Log10(x) + 1) < n)
        {
            tmp = x + y;
            y = x;
            x = tmp;
            currentIndex++;
        }
        return currentIndex;
    }
    public static int MaxLimitMethod(int n)
    {
        BigInteger maxLimit = BigInteger.Pow(10, n - 1);
        BigInteger x = 1;
        BigInteger y = 1;
        BigInteger tmp = 0;
        int currentIndex = 2;
        while (x.CompareTo(maxLimit) < 0)
        {
            tmp = x + y;
            y = x;
            x = tmp;
            currentIndex++;
        }
        return currentIndex;
    }
}

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

前提是 x> 0

int digits = (int)Math.Floor(BigInteger.Log10(x) + 1);

将获取位数。

出于好奇,我测试了

int digits = x.ToString().Length; 

方法。对于 100 000 000 次迭代,它比 Log10 解决方案慢 3 倍。

扩展我的评论 - 而不是基于位数进行测试,而是基于超过具有问题上限的常量进行测试:

public static int MaxLimitMethod(int n)
    {
        BigInteger maxLimit = BigInteger.Pow(10, n);
        BigInteger x = 1;
        BigInteger y = 1;
        BigInteger tmp = 0;
        int currentIndex = 2;
        while (x.CompareTo(maxLimit) < 0)
        {
            tmp = x + y;
            y = x;
            x = tmp;
            currentIndex++;
        }
        return currentIndex;
    }

这应该会导致性能显著提高。

更新:

这是 .NET 5 上更快的方法(因为需要GetBitLength()(:

private static readonly double exponentConvert = Math.Log10(2);
private static readonly BigInteger _ten = 10;
public static int CountDigits(BigInteger value)
{
    if (value.IsZero)
        return 1;
    value = BigInteger.Abs(value);
    if (value.IsOne)
        return 1;
    long numBits = value.GetBitLength();
    int base10Digits = (int)(numBits * exponentConvert).Dump();
    var reference = BigInteger.Pow(_ten, base10Digits);
    if (value >= reference)
        base10Digits++;
    return base10Digits;
}

对于大值,此算法中最慢的部分是BigInteger.Pow()运算。我已经优化了Singulink.Numerics.BigIntegerExtensions中的CountDigits()方法,缓存的幂为10,所以如果你对最快的实现感兴趣,请检查一下。默认情况下,它会将功率缓存到 1023 的指数,但如果您想在更大的值上牺牲内存使用量以获得更快的性能,您可以通过调用 BigIntegerPowCache.GetCache(10, maxSize) where maxSize = maxExponent + 1 来增加最大缓存指数。

在 i7-3770 CPU 上,当数字计数 <= 最大缓存指数时,此库需要 350 毫秒才能获得 1000 万个BigInteger值(单线程(的位数计数。

原答案:

如评论中所述,接受的答案是不可靠的。此方法适用于所有数字:

private static int CountDigits(BigInteger value)
{    
    if (value.IsZero)
        return 1;
        
    value = BigInteger.Abs(value);
    
    if (value.IsOne)
        return 1;
    
    int exp = (int)Math.Ceiling(BigInteger.Log10(value));
    var test = BigInteger.Pow(10, exp);
    
    return value >= test ? exp + 1 : exp;
}