Why does computing factorial of relatively small numbers (34+) return 0?

You’re going out of range of what the variable can store. That’s effectively a factorial, which grows faster than the exponential. Try using ulong (max value 2^64 = 18,446,744,073,709,551,615) instead of int (max value 2^31 = 2,147,483,647) – ulong p = 1 – that should get you a bit further.

If you need to go even further, .NET 4 and up has BigInteger, which can store arbitrarily large numbers.

Leave a Comment