在C#中用于计算阶乘的数据类型

4

C#中最大的数据类型是什么?或者说,我应该使用哪种数据类型来计算1619的阶乘。我尝试了ulong和uint64,但它们都对我的答案进行了修剪。

1个回答

1

看一下这个 https://msdn.microsoft.com/en-us/library/system.numerics.biginteger%28v=vs.110%29.aspx

BigInteger并不是非常快,但对于大数字来说是可以计算的。

以下是MSDN上的一个简单示例。

string positiveString = "91389681247993671255432112000000";
string negativeString = "-90315837410896312071002088037140000";
BigInteger posBigInt = 0;
BigInteger negBigInt = 0;

    try 
    {
         posBigInt = BigInteger.Parse(positiveString);
         Console.WriteLine(posBigInt);
    }
    catch (FormatException)
    {
         Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", positiveString);
    }

我尝试使用自己的示例,它正在工作

string positiveString = "91389681247993671255432112000000";
string negativeString = "-90315837410896312071002088037140000";
BigInteger b = BigInteger.Parse(positiveString);
BigInteger c = BigInteger.Parse(positiveString);
BigInteger d = b * c;

System.Console.WriteLine(d);
System.Console.ReadLine();

// result 835207383860988607360648144841987935757186784078054400000000000

谢谢,但是当我尝试使用BigInteger时,VS显示错误。我需要引用任何程序集吗? - Ankur Gupta
1
是的。首先从引用中添加System.Numerics,然后使用System.Numerics,接着你就可以使用BigInteger了。 - Evgeni Velikov
@AnkurGupta,你解决了这个问题吗?你在项目中添加了必要的程序集吗? - Evgeni Velikov
是的,我能够做到。谢谢。 - Ankur Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接