0

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 66th value, it provides a 0 as a result and the result is negative too if the value is 65 or near. What can I do to allow more values to work with, and get result with having the System.StackOverflowException?

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103

2 Answers2

3

You may want to check out the BigInteger struct, as it allows integers of an arbitrarily-large size: http://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx

Keith Kurak
  • 662
  • 1
  • 6
  • 11
0

Uint64 will get you more, not sure how many though.

kidshaw
  • 3,423
  • 2
  • 16
  • 28
  • Didn't expect much, but thought it might be a bit more. I trust your math more than mine! – kidshaw Jul 31 '14 at 20:31
  • actually less, since uint64 is what, double the size? and with the value being in the range of (supposedly) 60s, it'd overflow just as fast... (i was thinking fibbonaci numbers) – Kevin L Jul 31 '14 at 20:32
  • Yep - big integer looks better +1 – kidshaw Jul 31 '14 at 20:33