Peter Peperoni
Peter Peperoni

Reputation: 80

Why is Factorial 1000 impossible with BigInteger?

So when you googe, it says that BigInteger in C# is infinite, but factorial of 1000 is undefined. I made this simple recursive method and it returns something very big.

static BigInteger faculty(int n) {

        if(n == 2)
            return 2;
        return n*faculty(n-1);
    }

So it's probably an overflow but does that mean that BigIntegers are not infinite?

Upvotes: 1

Views: 386

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062905

It isn't (impossible). The following works just fine here (tested on .NET Core 3.1 and .NET Framework 4.7.2):

var x = faculty(1000);
var s = x.ToString();
Console.WriteLine(s.Length); // 2568 (digits)
Console.WriteLine(s); // 402387260...000000 (lots of digits)

Upvotes: 1

Related Questions