简体   繁体   English

以长整数保存阶乘

[英]Saving factorial in long integer

I am using the following code to get the Factorial, but it seems to be limited for me. 我正在使用以下代码来获取阶乘,但对我来说似乎很有限。

private Int64 GetFactorial(Int64 value)
{
    if (value <= 1)
    {
        return 1;
    }
    return value * GetFactorial(value - 1);
}

But it seems to only allow the values upto 65 on 66 th value, it provides a 0 as a result and the result is negative too if the value is 65 or near. 但是它似乎只允许第66个值上的最大值达到65 ,结果提供0 ,如果该值等于或接近65,结果也为负。 What can I do to allow more values to work with, and get result with having the System.StackOverflowException ? 我该怎么做才能让更多的值可以使用,并通过拥有System.StackOverflowException获得结果?

您可能要检查BigInteger结构,因为它允许任意大小的整数: http : //msdn.microsoft.com/zh-cn/library/system.numerics.biginteger(v=vs.110)。 aspx

Uint64可以为您带来更多收益,但不确定多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM