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.