4

I am trying to generate nth bell number for large values between 500 to 1000.

Bells number is huge and i can't store it in ulong.

(To know bells number: http://en.wikipedia.org/wiki/Bell_number)

I tried the triangular method to compute the number.

Can any temme a way to save huge numbers like dat and perform the operation.

Here is the Code i wrote


using System;

class Program    
{        
    static void Main(string[] args)
    {
        int length;
        do
        {
            length =-1;                
            string numLength= Console.ReadLine();
            if (int.TryParse(numLength, out length))
            {   
                Console.WriteLine("Sequence length is : {0}",
                                           TriangularMethod(length));
            }
        }while(length>0);
    }

    static ulong TriangularMethod(int n)
    {
        Dictionary<long, List<ulong>> triangle = 
                                new Dictionary<long, List<ulong>>();
        triangle.Add(1, new List<ulong>(new ulong[] { 1 }));

        for (int i = 2; i <= n; i++)
        {
            triangle.Add(i, new List<ulong>());
            triangle[i].Add(triangle[i - 1].Last());
            ulong lastVal = 0;
            for (int k = 1; k < i; k++)
            {
                lastVal = triangle[i][k - 1] + triangle[i - 1][k - 1];
                triangle[i].Add(lastVal);                    
            }

            triangle.Remove(i - 2);
        }
        return triangle[n].Last();
    }        
}

If there is a faster way to compute the same. please temme.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Akanksha Gaur
  • 2,636
  • 3
  • 26
  • 50

1 Answers1

3

use BigInteger Structure available in namespace System.Numerics

I think this would be useful to you

http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx

working with incredibly large numbers in .NET

http://www.codeproject.com/Articles/2728/C-BigInteger-Class

Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208