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
  • 751
  • 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
  • 811,555
  • 193
  • 1,581
  • 1,452
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
  • 27,210
  • 6
  • 71
  • 92