TheAlgorithms/Java · error · IllegalArgumentException

Number must be positive

Error message

Number must be positive

What it means

Thrown by isKeith(int number) when the input is zero or negative. A Keith number is a number that appears in the Fibonacci-like sequence generated from its own digits (e.g., 14 generates 1,4,5,9,14). The digit-extraction loop (temp % 10 while temp > 0) would produce an empty sequence for non-positive inputs, making the algorithm undefined.

Source

Thrown at src/main/java/com/thealgorithms/maths/KeithNumber.java:48

    /**
     * Checks if a given number is a Keith number.
     *
     * <p>
     * The algorithm works as follows:
     * <ol>
     * <li>Extract all digits of the number and store them in a list</li>
     * <li>Generate subsequent terms by summing the last n digits</li>
     * <li>Continue until a term equals or exceeds the original number</li>
     * <li>If a term equals the number, it is a Keith number</li>
     * </ol>
     *
     * @param number the number to check (must be positive)
     * @return {@code true} if the number is a Keith number, {@code false} otherwise
     * @throws IllegalArgumentException if the number is not positive
     */
    public static boolean isKeith(int number) {
        if (number <= 0) {
            throw new IllegalArgumentException("Number must be positive");
        }

        // Extract digits and store them in the list
        ArrayList<Integer> terms = new ArrayList<>();
        int temp = number;
        int digitCount = 0;

        while (temp > 0) {
            terms.add(temp % 10);
            temp = temp / 10;
            digitCount++;
        }

        // Reverse the list to get digits in correct order
        Collections.reverse(terms);

        // Generate subsequent terms in the sequence
        int nextTerm = 0;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the input is >= 1 before calling isKeith
  2. Start search loops at 1 instead of 0
  3. Use Optional or a default of 1 for parsed inputs that may be empty

Example fix

// before
for (int i = 0; i <= max; i++) {
    if (isKeith(i)) print(i);
}

// after
for (int i = 1; i <= max; i++) {
    if (isKeith(i)) print(i);
}
Defensive patterns

Strategy: validation

Validate before calling

if (number <= 0) {
    throw new IllegalArgumentException("Input must be a positive integer: " + number);
}
boolean result = KeithNumber.isKeith(number);

Type guard

static boolean isValidKeithInput(int number) {
    return number > 0;
}

Try / catch

try {
    boolean result = KeithNumber.isKeith(number);
} catch (IllegalArgumentException e) {
    logger.warn("Invalid Keith number input: {}", number);
}

Prevention

When it happens

Trigger: Calling isKeith(0) or isKeith(-7). Common when iterating over a range that starts at 0 or when parsing fails and defaults to 0.

Common situations: Looping from i=0 in a search-for-Keith-numbers routine. Auto-unboxing an Integer that defaulted to 0. Off-by-one in a range generator that includes zero.

Related errors


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