TheAlgorithms/Java · error · IllegalArgumentException

sylvester() does not accept negative numbers or zero.

Error message

sylvester() does not accept negative numbers or zero.

What it means

Thrown by sylvester(int n) when the requested position n is less than or equal to 0. Sylvester's sequence is defined for positive indices starting at 1, so any non-positive index is out of domain. The method uses this guard to prevent descending into undefined recursion.

Source

Thrown at src/main/java/com/thealgorithms/recursion/SylvesterSequence.java:40

    private SylvesterSequence() {
    }

    /**
     * Calculates the nth number in Sylvester's sequence.
     *
     * <p>The sequence is defined recursively, with the first term being 2:
     * <pre>
     * a(1) = 2
     * a(n) = a(n-1) * (a(n-1) - 1) + 1 for n > 1
     * </pre>
     *
     * @param n the position in the sequence (must be greater than 0)
     * @return the nth number in Sylvester's sequence
     * @throws IllegalArgumentException if n is less than or equal to 0
     */
    public static BigInteger sylvester(int n) {
        if (n <= 0) {
            throw new IllegalArgumentException("sylvester() does not accept negative numbers or zero.");
        }
        if (n == 1) {
            return BigInteger.valueOf(2);
        } else {
            BigInteger prev = sylvester(n - 1);
            // Sylvester sequence formula: a(n) = a(n-1) * (a(n-1) - 1) + 1
            return prev.multiply(prev.subtract(BigInteger.ONE)).add(BigInteger.ONE);
        }
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate that n >= 1 before calling sylvester(), and surface a meaningful error to the user otherwise.
  2. If your input is zero-based, pass n + 1 to align with the 1-based indexing.
  3. Coerce negative or zero inputs to the nearest valid index (1) only if that matches your business logic.

Example fix

// before
BigInteger v = SylvesterSequence.sylvester(userIndex);

// after
if (userIndex < 1) throw new IllegalArgumentException("index must be >= 1, got " + userIndex);
BigInteger v = SylvesterSequence.sylvester(userIndex);
Defensive patterns

Strategy: validation

Validate before calling

if (n < 1) throw new IllegalArgumentException("Sylvester index must be >= 1, got " + n);

Type guard

// n is a primitive int; treat as validated value object if wrapping is acceptable
public static boolean isValidSylvesterIndex(int n) { return n >= 1; }

Prevention

When it happens

Trigger: Calling sylvester(0), sylvester(-1), or any sylvester(n) where n <= 0. The internal recursion itself never triggers this because it only recurses with n-1 down to the n == 1 base case.

Common situations: Passing a user-supplied or computed index without clamping; off-by-one when converting from a zero-based UI selector to the 1-based sequence position; calling with a default/uninitialized int value of 0.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/10b1884b55550bd5. Report an issue: GitHub.