TheAlgorithms/Java · error · IllegalArgumentException

Input must be a positive integer. Provided:

Error message

Input must be a positive integer. Provided: 

What it means

Thrown by the recursive lucasSeries(int n) when n < 1. The Lucas sequence is 1-indexed with L(1)=2, L(2)=1, L(n)=L(n-1)+L(n-2). Positions below 1 have no defined value, and the recursion would miss its base cases.

Source

Thrown at src/main/java/com/thealgorithms/maths/LucasSeries.java:29

 * @see <a href="https://en.wikipedia.org/wiki/Lucas_number">Lucas Number</a>
 * @author TheAlgorithms Contributors
 */
public final class LucasSeries {
    private LucasSeries() {
    }

    /**
     * Calculate the nth Lucas number using recursion.
     * Time Complexity: O(2^n) - exponential due to recursive calls
     * Space Complexity: O(n) - recursion depth
     *
     * @param n the position in the Lucas sequence (1-indexed, must be positive)
     * @return the nth Lucas number
     * @throws IllegalArgumentException if n is less than 1
     */
    public static int lucasSeries(int n) {
        if (n < 1) {
            throw new IllegalArgumentException("Input must be a positive integer. Provided: " + n);
        }
        if (n == 1) {
            return 2;
        }
        if (n == 2) {
            return 1;
        }
        return lucasSeries(n - 1) + lucasSeries(n - 2);
    }

    /**
     * Calculate the nth Lucas number using iteration.
     * Time Complexity: O(n) - single loop through n iterations
     * Space Complexity: O(1) - constant space usage
     *
     * @param n the position in the Lucas sequence (1-indexed, must be positive)
     * @return the nth Lucas number
     * @throws IllegalArgumentException if n is less than 1

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure n >= 1 before calling lucasSeries; adjust 0-based indices by adding 1
  2. Prefer the iterative lucasSeriesIteration for any production use
  3. Add an explicit bounds check at the call site with a clear error message

Example fix

// before
int result = LucasSeries.lucasSeries(arrayIndex);

// after
int result = LucasSeries.lucasSeries(arrayIndex + 1);  // convert 0-based to 1-based
Defensive patterns

Strategy: validation

Validate before calling

if (n < 1) {
    throw new IllegalArgumentException("Position must be >= 1 (1-indexed): " + n);
}
int result = LucasSeries.lucasSeries(n);

Type guard

static boolean isValidLucasPosition(int n) {
    return n >= 1;
}

Prevention

When it happens

Trigger: Calling lucasSeries(0) or lucasSeries(-3). Hit when n is zero-based but the API expects 1-based indexing, or when a computed position underflows.

Common situations: Off-by-one: the caller uses 0-based indexing (common in array contexts) but LucasSeries expects 1-based. Passing a loop variable that starts at 0. Subtracting from n in a recursive or iterative caller that can reach 0.

Related errors


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