Skip to content

Commit 1b9373e

Browse files
authored
feat: add Bell Numbers algorithm using Aitken's Array (#7219)
* feat: added Bell Numbers algorithm using Aitken's Array * style: applied clang-format fixes
1 parent 79cdb98 commit 1b9373e

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* The Bell numbers count the number of partitions of a set.
5+
* The n-th Bell number is the number of ways a set of n elements can be partitioned
6+
* into nonempty subsets.
7+
*
8+
* <p>
9+
* This implementation uses the Bell Triangle (Aitken's array) method.
10+
* Time Complexity: O(n^2)
11+
* Space Complexity: O(n^2)
12+
* </p>
13+
*
14+
* @author Chahat Sandhu, <a href="https://github.com/singhc7">singhc7</a>
15+
* @see <a href="https://en.wikipedia.org/wiki/Bell_number">Bell Number (Wikipedia)</a>
16+
*/
17+
public final class BellNumbers {
18+
19+
private BellNumbers() {
20+
}
21+
22+
/**
23+
* Calculates the n-th Bell number using the Bell Triangle.
24+
*
25+
* @param n the index of the Bell number (must be non-negative)
26+
* @return the n-th Bell number
27+
* @throws IllegalArgumentException if n is negative or n > 25
28+
*/
29+
public static long compute(int n) {
30+
if (n < 0) {
31+
throw new IllegalArgumentException("n must be non-negative");
32+
}
33+
if (n == 0) {
34+
return 1;
35+
}
36+
if (n > 25) {
37+
throw new IllegalArgumentException("n must be <= 25. For larger n, use BigInteger implementation.");
38+
}
39+
40+
// We use a 2D array to visualize the Bell Triangle
41+
long[][] bellTriangle = new long[n + 1][n + 1];
42+
43+
// Base case: The triangle starts with 1
44+
bellTriangle[0][0] = 1;
45+
46+
for (int i = 1; i <= n; i++) {
47+
// Rule 1: The first number in a new row is the LAST number of the previous row
48+
bellTriangle[i][0] = bellTriangle[i - 1][i - 1];
49+
50+
// Rule 2: Fill the rest of the row by adding the previous neighbor and the upper-left neighbor
51+
for (int j = 1; j <= i; j++) {
52+
bellTriangle[i][j] = bellTriangle[i][j - 1] + bellTriangle[i - 1][j - 1];
53+
}
54+
}
55+
56+
// The Bell number B_n is the first number in the n-th row
57+
return bellTriangle[n][0];
58+
}
59+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class BellNumbersTest {
9+
10+
@Test
11+
void testStandardCases() {
12+
// Base cases and small numbers
13+
assertEquals(1, BellNumbers.compute(0));
14+
assertEquals(1, BellNumbers.compute(1));
15+
assertEquals(2, BellNumbers.compute(2));
16+
assertEquals(5, BellNumbers.compute(3));
17+
assertEquals(15, BellNumbers.compute(4));
18+
assertEquals(52, BellNumbers.compute(5));
19+
}
20+
21+
@Test
22+
void testMediumNumber() {
23+
// B10 = 115,975
24+
assertEquals(115975, BellNumbers.compute(10));
25+
// B15 = 1,382,958,545
26+
assertEquals(1382958545L, BellNumbers.compute(15));
27+
}
28+
29+
@Test
30+
void testLargeNumber() {
31+
// B20 = 51,724,158,235,372
32+
// We use the 'L' suffix to tell Java this is a long literal
33+
assertEquals(51724158235372L, BellNumbers.compute(20));
34+
}
35+
36+
@Test
37+
void testMaxLongCapacity() {
38+
// B25 is the largest Bell number that fits in a Java long (signed 64-bit)
39+
// B25 = 4,638,590,332,229,999,353
40+
assertEquals(4638590332229999353L, BellNumbers.compute(25));
41+
}
42+
43+
@Test
44+
void testNegativeInput() {
45+
assertThrows(IllegalArgumentException.class, () -> BellNumbers.compute(-1));
46+
}
47+
48+
@Test
49+
void testOverflowProtection() {
50+
// We expect an exception if the user asks for the impossible
51+
assertThrows(IllegalArgumentException.class, () -> BellNumbers.compute(26));
52+
}
53+
}

0 commit comments

Comments
 (0)