TheAlgorithms/Java · error · IllegalArgumentException

Number must be positive.

Error message

Number must be positive.

What it means

Thrown by the private validatePositiveNumber(int number) method, invoked by both isLuckyNumber(int) and isLucky(int). Lucky numbers are defined only for natural numbers (positive integers >= 1). The sieve-based algorithm relies on positional elimination starting from position 2, which is meaningless for non-positive inputs.

Source

Thrown at src/main/java/com/thealgorithms/maths/LuckyNumber.java:19

package com.thealgorithms.maths;

/**
 * In number theory, a lucky number is a natural number in a set which is generated by a certain "sieve".
 * This sieve is similar to the sieve of Eratosthenes that generates the primes,
 * but it eliminates numbers based on their position in the remaining set,
 * instead of their value (or position in the initial set of natural numbers).
 *
 * Wiki: https://en.wikipedia.org/wiki/Lucky_number
 */
public final class LuckyNumber {

    private LuckyNumber() {
    }

    // Common validation method
    private static void validatePositiveNumber(int number) {
        if (number <= 0) {
            throw new IllegalArgumentException("Number must be positive.");
        }
    }

    // Function to check recursively for Lucky Number
    private static boolean isLuckyRecursiveApproach(int n, int counter) {
        // Base case: If counter exceeds n, number is lucky
        if (counter > n) {
            return true;
        }

        // If number is eliminated in this step, it's not lucky
        if (n % counter == 0) {
            return false;
        }

        // Calculate new position after removing every counter-th number
        int newNumber = n - (n / counter);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the input is >= 1 before calling isLucky or isLuckyNumber
  2. Start search loops at 1 instead of 0
  3. Validate parsed integers before passing to the algorithm

Example fix

// before
for (int i = 0; i < 1000; i++) {
    if (LuckyNumber.isLucky(i)) System.out.println(i);
}

// after
for (int i = 1; i < 1000; i++) {
    if (LuckyNumber.isLucky(i)) System.out.println(i);
}
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try {
    boolean result = LuckyNumber.isLucky(number);
} catch (IllegalArgumentException e) {
    logger.warn("Invalid lucky number input: {}", number);
}

Prevention

When it happens

Trigger: Calling LuckyNumber.isLucky(0), isLucky(-5), isLuckyNumber(0), or isLuckyNumber(-1). Any non-positive integer passed to either public method.

Common situations: Iterating from 0 in a lucky-number search. Passing a default int value of 0 from unconfigured state. Parsing failure that yields 0.

Related errors


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