如何在 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
How can I use bigint with C#?
提问by Ersin Gülbahar
采纳答案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.Numerics,using 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); }
}

