Wednesday, January 21, 2009

Find BigNumber Factorial (!)

Sometime when work with mathematical application , there is need to find out “Big Number” Factorial.

By Use of Simple Factorial Function

C# Code.

using System;
class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 50; i++)
            {
            Console.WriteLine(i.ToString() + "\t\t" +SimpleFactorial(i).ToString());
            }
            Console.ReadLine();
        }

        static System.Int64 SimpleFactorial(System.Int64 number)
        {
            System.Int64 result = 1;
            for (System.Int64 i = 2; i <= number; i++)
            {
                result = result * i;
            }
            return result;
        }
}
If you are using above code than maximum up to value 20 you can find right answer. (Means 20!). If need is to find out Factorial larger this than you have to use FSharp Library in C#.

You can find F#Sharp Library at DownLoad .

After download , extract it and Add reference of FSharp.Core in C# Project.

using System;
using Microsoft.FSharp.Math;
class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 50; i++)
            {
           Console.WriteLine(i.ToString() + "\t\t" + FSharpFactorial(i).ToString());
            }
            Console.ReadLine();
        }       

        static BigInt FSharpFactorial(System.Int64 number)
        {
            BigInt num = new BigInt(number);
            return BigInt.Factorial(num);
        }
    }

By using FSharp Version of factorial 50! easily calculated. Even i test upto 20000! and It product result.

No comments: