如何在 C# 中使用 bigint?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10624815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:33:28  来源:igfitidea点击:

How can I use bigint with C#?

c#biginteger

提问by Ersin Gülbahar

I work to implement an RSAkey algorithm. But I couldn't use a 2048-bit value. How I can use it?

我致力于实现RSA密钥算法。但是我不能使用 2048 位的值。我怎样才能使用它?

I want to use big integer.

我想使用大整数。

采纳答案by BrokenGlass

You can use System.Numerics.BigInteger(add a reference to System.Numerics assembly). As mentioned in the comments this might not be the right approach though.

您可以使用System.Numerics.BigInteger(添加对 System.Numerics 程序集的引用)。正如评论中提到的,这可能不是正确的方法。

回答by Douglas

Native support for big integers has been introduced in .NET 4.0. Just add an assembly reference to System.Numerics, add a using System.Numerics;declaration at the top of your code file, and you're good to go. The type you're after is BigInteger.

.NET 4.0 中引入了对大整数的本机支持。只需添加对 的程序集引用System.Numericsusing System.Numerics;在代码文件的顶部添加声明,就可以了。你所追求的类型是BigInteger.

回答by moribvndvs

BigIntegeris available in .NET 4.0 or later. There are some third-party implementations as well(In case you are using an earlier version of the framework).

BigInteger在 .NET 4.0 或更高版本中可用。还有一些第三方实现(如果您使用的是早期版本的框架)。

回答by Kishore Kumar

Better use System.Numerics.BigInteger.

更好地使用System.Numerics.BigInteger

回答by Sunil Chandurkar

Here's using BigInteger. This method Prints Numbers in the Fibonacci Sequence up to n.

这里使用BigInteger. 此方法打印斐波那契数列中的数字,最多为n

public static void FibonacciSequence(int n)
{
    /** BigInteger easily holds the first 1000 numbers in the Fibonacci Sequence. **/
    List<BigInteger> fibonacci = new List<BigInteger>();
    fibonacci.Add(0);
    fibonacci.Add(1);
    BigInteger i = 2;
    while(i < n)
    {                
        int first = (int)i - 2;
        int second = (int) i - 1;

        BigInteger firstNumber =  fibonacci[first];
        BigInteger secondNumber = fibonacci[second];
        BigInteger sum = firstNumber + secondNumber;
        fibonacci.Add(sum);
        i++;
    }         

    foreach (BigInteger f in fibonacci) { Console.WriteLine(f); }
}