{"record":{"id":"cb3c0e5bba13eb71","repo":"TheAlgorithms/Java","slug":"input-must-be-non-negative-cb3c0e","errorCode":null,"errorMessage":"Input must be non-negative","messagePattern":"Input must be non-negative","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java","lineNumber":39,"sourceCode":" * @author Navadeep0007\n * @see <a href=\"https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\">Sieve of Eratosthenes</a>\n */\npublic final class SieveOfEratosthenes {\n\n    private SieveOfEratosthenes() {\n        // Utility class, prevent instantiation\n    }\n\n    /**\n     * Finds all prime numbers up to n using the Sieve of Eratosthenes algorithm\n     *\n     * @param n the upper limit (inclusive)\n     * @return a list of all prime numbers from 2 to n\n     * @throws IllegalArgumentException if n is negative\n     */\n    public static List<Integer> findPrimes(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input must be non-negative\");\n        }\n\n        if (n < 2) {\n            return new ArrayList<>();\n        }\n\n        // Create boolean array, initially all true\n        boolean[] isPrime = new boolean[n + 1];\n        for (int i = 2; i <= n; i++) {\n            isPrime[i] = true;\n        }\n\n        // Sieve process\n        for (int i = 2; i * i <= n; i++) {\n            if (isPrime[i]) {\n                // Mark all multiples of i as not prime\n                for (int j = i * i; j <= n; j += i) {\n                    isPrime[j] = false;","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/SieveOfEratosthenes.java#L21-L57","documentation":"Thrown by SieveOfEratosthenes.findPrimes(int n) when n is negative. The sieve allocates a boolean[] of size n+1 and indexes from index 2; a negative n would produce a malformed array and meaningless iteration, so the guard rejects it up front. n in [0,1] returns an empty list rather than throwing.","triggerScenarios":"Call findPrimes(-1), findPrimes(-100), or pass a value computed as (a - b) where the subtraction went negative unexpectedly.","commonSituations":"Untested user input, a bound expression that underflows (e.g., size - margin with margin > size), or porting code that assumed unsigned arithmetic.","solutions":["Validate/abs the input or return an empty result for negative bounds at the call site.","Use Math.max(0, n) before calling if negatives are tolerable as 'no primes'.","Add an assertion/precondition in the caller documenting the non-negative contract."],"exampleFix":"// before\nList<Integer> primes = SieveOfEratosthenes.findPrimes(limit);\n\n// after\nList<Integer> primes = SieveOfEratosthenes.findPrimes(Math.max(0, limit));","handlingStrategy":"validation","validationCode":"if (n < 0) throw new IllegalArgumentException(\"n must be >= 0\");\nList<Integer> primes = SieveOfEratosthenes.findPrimes(n);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Sanitize any externally-derived upper bound before passing to numeric algorithms.","Prefer Math.max(0, n) when 'no primes' is the desired semantic for negatives."],"tags":["math","primes","sieve","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}