1

Trying to count factorial of a big number, for example 1000!

static void Main(string[] args)
        {
            UInt64 fact = 1;

            for (UInt64 i = 1000; i > 0; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine(fact); //returns 0, due to overflow UInt64, max fact is 56!

            Console.ReadKey();
        }

So i ask, if there is some way to join more variables to cluster, so i can make really large variable to store "big" number.

MartinS
  • 731
  • 3
  • 12
  • 27

2 Answers2

5

You can use a BigInteger. This type can store integers of arbitrary size, until you run out of memory.

Mark Byers
  • 767,688
  • 176
  • 1,542
  • 1,434
2

In .NET 4 BigInteger will do what you want.

Big integers in C# may be of more interest as well since it is a very similar question (well, more a very similar answer).

Community
  • 1
  • 1
Chris
  • 26,664
  • 5
  • 69
  • 88