TheAlgorithms/Java · error · IllegalArgumentException
n must be <= 25. For larger n, use BigInteger implementation
Error message
n must be <= 25. For larger n, use BigInteger implementation.
What it means
Thrown by BellNumbers.compute(int n) when n > 25. The method stores Bell numbers in a long, and B(26) exceeds Long.MAX_VALUE (B(25) = 4638590332229999683 which is near the long limit). The library intentionally caps at n=25 and directs callers to a BigInteger-based implementation for larger values.
Source
Thrown at src/main/java/com/thealgorithms/maths/BellNumbers.java:37
private BellNumbers() {
}
/**
* Calculates the n-th Bell number using the Bell Triangle.
*
* @param n the index of the Bell number (must be non-negative)
* @return the n-th Bell number
* @throws IllegalArgumentException if n is negative or n > 25
*/
public static long compute(int n) {
if (n < 0) {
throw new IllegalArgumentException("n must be non-negative");
}
if (n == 0) {
return 1;
}
if (n > 25) {
throw new IllegalArgumentException("n must be <= 25. For larger n, use BigInteger implementation.");
}
// We use a 2D array to visualize the Bell Triangle
long[][] bellTriangle = new long[n + 1][n + 1];
// Base case: The triangle starts with 1
bellTriangle[0][0] = 1;
for (int i = 1; i <= n; i++) {
// Rule 1: The first number in a new row is the LAST number of the previous row
bellTriangle[i][0] = bellTriangle[i - 1][i - 1];
// Rule 2: Fill the rest of the row by adding the previous neighbor and the upper-left neighbor
for (int j = 1; j <= i; j++) {
bellTriangle[i][j] = bellTriangle[i][j - 1] + bellTriangle[i - 1][j - 1];
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- If n <= 25, call BellNumbers.compute(n) as-is.
- For n > 25, use a BigInteger-based Bell number implementation (e.g., the Apache Commons Math or a custom BigInteger Bell triangle).
- Add a bounds check before the call: if (n > 25) switch to BigInteger implementation or cap the input.
Example fix
// before
BellNumbers.compute(30); // throws 'n must be <= 25...'
// after (use BigInteger implementation for large n)
public static BigInteger bellNumberBig(int n) {
BigInteger[][] triangle = new BigInteger[n + 1][n + 1];
triangle[0][0] = BigInteger.ONE;
for (int i = 1; i <= n; i++) {
triangle[i][0] = triangle[i - 1][i - 1];
for (int j = 1; j <= i; j++) {
triangle[i][j] = triangle[i][j - 1].add(triangle[i - 1][j - 1]);
}
}
return triangle[n][0];
}
BigInteger result = (n <= 25)
? BigInteger.valueOf(BellNumbers.compute(n))
: bellNumberBig(n); Defensive patterns
Strategy: validation
Validate before calling
// Check bounds before calling BellNumbers.compute
if (n < 0 || n > 25) {
throw new IllegalArgumentException(
"Bell number index must be in [0, 25], got: " + n);
}
long bell = BellNumbers.compute(n);
// For n > 25, implement or find a BigInteger-based Bell number method Type guard
static boolean isValidBellIndex(int n) {
return n >= 0 && n <= 25;
} Prevention
- Be aware that Bell numbers grow extremely fast — B(26) overflows long.
- Keep a BigInteger-based Bell number implementation available for large n.
- If n comes from user input or dynamic computation, cap it at 25 or route to the BigInteger path.
When it happens
Trigger: Calling BellNumbers.compute(26), BellNumbers.compute(30), or BellNumbers.compute(50). Any n in the range [26, Integer.MAX_VALUE] after passing the n >= 0 and n != 0 checks triggers this.
Common situations: A computation pipeline that dynamically computes n from a set size or partition count without bounds checking. A user requesting a large Bell number via an API or CLI. Academic or research code exploring combinatorial properties at scale where the caller is unaware of the long overflow limit.
Related errors
- n must be non-negative
- Index must be non-negative
- Invalid unit '{}'. Supported units are: {}
- inputUnit must be different from outputUnit.
- NULL_INPUT
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/cf4d1d3f931662c7.
Report an issue: GitHub.