C# 中用于查找阶乘的数据类型

本文关键字:数据类型 阶乘 查找 用于 | 更新日期: 2023-09-27 18:32:43

C

# 中最大的数据类型是什么,或者我应该使用哪种数据类型来查找 1619 的阶乘。我尝试了 ulong 和 uint64,但他们修剪了我的答案。

C# 中用于查找阶乘的数据类型

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

是一个大整数不是很快,但对于大数字是可以计算的

这里和来自 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