{"record":{"id":"cf4d1d3f931662c7","repo":"TheAlgorithms/Java","slug":"n-must-be-25-for-larger-n-use-biginteger-impl","errorCode":null,"errorMessage":"n must be <= 25. For larger n, use BigInteger implementation.","messagePattern":"n must be <= 25\\. For larger n, use BigInteger implementation\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/BellNumbers.java","lineNumber":37,"sourceCode":"    private BellNumbers() {\n    }\n\n    /**\n     * Calculates the n-th Bell number using the Bell Triangle.\n     *\n     * @param n the index of the Bell number (must be non-negative)\n     * @return the n-th Bell number\n     * @throws IllegalArgumentException if n is negative or n > 25\n     */\n    public static long compute(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"n must be non-negative\");\n        }\n        if (n == 0) {\n            return 1;\n        }\n        if (n > 25) {\n            throw new IllegalArgumentException(\"n must be <= 25. For larger n, use BigInteger implementation.\");\n        }\n\n        // We use a 2D array to visualize the Bell Triangle\n        long[][] bellTriangle = new long[n + 1][n + 1];\n\n        // Base case: The triangle starts with 1\n        bellTriangle[0][0] = 1;\n\n        for (int i = 1; i <= n; i++) {\n            // Rule 1: The first number in a new row is the LAST number of the previous row\n            bellTriangle[i][0] = bellTriangle[i - 1][i - 1];\n\n            // Rule 2: Fill the rest of the row by adding the previous neighbor and the upper-left neighbor\n            for (int j = 1; j <= i; j++) {\n                bellTriangle[i][j] = bellTriangle[i][j - 1] + bellTriangle[i - 1][j - 1];\n            }\n        }\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/BellNumbers.java#L19-L55","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nBellNumbers.compute(30); // throws 'n must be <= 25...'\n\n// after (use BigInteger implementation for large n)\npublic static BigInteger bellNumberBig(int n) {\n    BigInteger[][] triangle = new BigInteger[n + 1][n + 1];\n    triangle[0][0] = BigInteger.ONE;\n    for (int i = 1; i <= n; i++) {\n        triangle[i][0] = triangle[i - 1][i - 1];\n        for (int j = 1; j <= i; j++) {\n            triangle[i][j] = triangle[i][j - 1].add(triangle[i - 1][j - 1]);\n        }\n    }\n    return triangle[n][0];\n}\nBigInteger result = (n <= 25)\n    ? BigInteger.valueOf(BellNumbers.compute(n))\n    : bellNumberBig(n);","handlingStrategy":"validation","validationCode":"// Check bounds before calling BellNumbers.compute\nif (n < 0 || n > 25) {\n    throw new IllegalArgumentException(\n        \"Bell number index must be in [0, 25], got: \" + n);\n}\nlong bell = BellNumbers.compute(n);\n// For n > 25, implement or find a BigInteger-based Bell number method","typeGuard":"static boolean isValidBellIndex(int n) {\n    return n >= 0 && n <= 25;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["combinatorics","overflow","bell-numbers","input-validation","bigint","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}