The Bell numbers B(n) are defined by B(0) = 1 and B(n) = sigman-1 _i=0 (n - 1 i)
ID: 3829237 • Letter: T
Question
The Bell numbers B(n) are defined by B(0) = 1 and B(n) = sigman-1 _i=0 (n - 1 i) B (i)for n > 0. Write a recursive procedure to compute the Bell numbers. The binomial coefficient (n - 1 i) can be computed as (n k) = n!/k!(n-k)! Get the input, n, from user and display the series of Bell numbers up to n. Your code should be able to print up to B(20). Use the recursive factorial calculation, as discussed in class, to compute the binomial coefficient. Use iterative function to compute the binomial coefficient in minimum steps, without calling the factorial function explicitly. Sample Output Enter the value of n: 2theta Bell Number [theta] = 1 Bell Number [1] = 1 Bell Number [2] = 2 Bell Number [3] = 5 Bell Number [4] = 15 Bell Number [5] = 52 Bell Number [6] = 203 Bell Number [7] = 877 Bell Number [8] = 414theta Bell Number [9] = 21147 Bell Number [1theta] = 115975 Bell Number [11] = 67857theta Bell Number [12] = 4213597 Bell Number [13] = 27644437 Bell Number [14] = 190899322 Bell Number [15] = 1382958545 Bell Number [16] = 1 theta 48 theta 142147 Bell Number [17] = 828648698 theta 4 Bell Number [18] = 682 theta 768 theta 6159 Bell Number [19] = 58327422 theta 5 theta 57 Bell Number [2 theta] = 51724158235372Explanation / Answer
#include <stdio.h>
unsigned long generateBellNumbers(int n);
int main()
{
int n;
printf("Enter value for n :");
scanf("%d",&n);
int i;
for (i=0; i<=n; i++)
printf("Bell Number [%d] = %lu ",i,generateBellNumbers(i));
return 0;
}
unsigned long generateBellNumbers(int n)
{
unsigned long array[n+1][n+1];
array[0][0] = 1;
int i;
for (i=1; i<=n; i++)
{
array[i][0] = array[i-1][i-1];
int j;
for (j=1; j<=i; j++)
array[i][j] = array[i-1][j-1] + array[i][j-1];
}
return array[n][0];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.