forked from Twiggecode/Integer-Sequences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBellNumber.cs
65 lines (54 loc) · 1.75 KB
/
BellNumber.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace DefaultNamespace
{
public class BellNumber
{
private static int CalculateNthBellNumber(int n)
{
// create 2d array to store Bell triangle
int[,] bellArray = new int[n, n];
if(n == 0 || n == 1)
{
return 1;
}
// arbitrary insert first bell number
bellArray[0, 0] = 1;
for(int i = 1; i < n; i++)
{
// first number in row i is the last number in row i-1;
bellArray[i, 0] = bellArray[i - 1, i - 1];
for(int j = 1; j<= i; j++)
{
// last number + the number at the same solumn but previous row
bellArray[i, j] = bellArray[i, j - 1] + bellArray[i - 1, j - 1];
}
}
// last number at last row is the number you want
return bellArray[bellArray.GetLength(0) - 1, n - 1];
}
public static void Run()
{
int nTH;
Console.WriteLine("Please enter a integer");
while (true)
{
string input = Console.ReadLine();
try
{
nTH = Convert.ToInt32(input);
if(nTH < 0)
{
throw new Exception("The number cannot be less than 0");
}
Console.WriteLine(CalculateNthBellNumber(nTH));
break;
}
catch (Exception e)
{
Console.WriteLine("Invalid, please enter an valid number");
Console.WriteLine(e);
}
}
}
}
}
}