How to resolve the algorithm Bell numbers step by step in the Groovy programming language
How to resolve the algorithm Bell numbers step by step in the Groovy programming language
Table of Contents
Problem Statement
Bell or exponential numbers are enumerations of the number of different ways to partition a set that has exactly n elements. Each element of the sequence Bn is the number of partitions of a set of size n where order of the elements and order of the partitions are non-significant. E.G.: {a b} is the same as {b a} and {a} {b} is the same as {b} {a}.
A simple way to find the Bell numbers is construct a Bell triangle, also known as an Aitken's array or Peirce triangle, and read off the numbers in the first column of each row. There are other generating algorithms though, and you are free to choose the best / most appropriate for your case.
Write a routine (function, generator, whatever) to generate the Bell number sequence and call the routine to show here, on this page at least the first 15 and (if your language supports big Integers) 50th elements of the sequence. If you do use the Bell triangle method to generate the numbers, also show the first ten rows of the Bell triangle.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bell numbers step by step in the Groovy programming language
Source code in the groovy programming language
class Bell {
private static class BellTriangle {
private List<Integer> arr
BellTriangle(int n) {
int length = (int) (n * (n + 1) / 2)
arr = new ArrayList<>(length)
for (int i = 0; i < length; ++i) {
arr.add(0)
}
set(1, 0, 1)
for (int i = 2; i <= n; ++i) {
set(i, 0, get(i - 1, i - 2))
for (int j = 1; j < i; ++j) {
int value = get(i, j - 1) + get(i - 1, j - 1)
set(i, j, value)
}
}
}
private static int index(int row, int col) {
if (row > 0 && col >= 0 && col < row) {
return row * (row - 1) / 2 + col
} else {
throw new IllegalArgumentException()
}
}
int get(int row, int col) {
int i = index(row, col)
return arr.get(i)
}
void set(int row, int col, int value) {
int i = index(row, col)
arr.set(i, value)
}
}
static void main(String[] args) {
final int rows = 15
BellTriangle bt = new BellTriangle(rows)
System.out.println("First fifteen Bell numbers:")
for (int i = 0; i < rows; ++i) {
System.out.printf("%2d: %d\n", i + 1, bt.get(i + 1, 0))
}
for (int i = 1; i <= 10; ++i) {
System.out.print(bt.get(i, 0))
for (int j = 1; j < i; ++j) {
System.out.printf(", %d", bt.get(i, j))
}
System.out.println()
}
}
}
You may also check:How to resolve the algorithm Bitcoin/public point to address step by step in the Tcl programming language
You may also check:How to resolve the algorithm Nth root step by step in the Racket programming language
You may also check:How to resolve the algorithm Mian-Chowla sequence step by step in the Pascal programming language
You may also check:How to resolve the algorithm Function definition step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Ursa programming language