TheAlgorithms/Java · error · IllegalArgumentException

Number must be positive.

Error message

Number must be positive.

What it means

Thrown by AbundantNumber.validatePositiveNumber when 'number' is <= 0. Abundant-number logic is defined only over positive integers (the divisor sum from 1..n/2 is meaningless for 0 or negatives), so isAbundant rejects anything non-positive up front via this shared validator.

Source

Thrown at src/main/java/com/thealgorithms/maths/AbundantNumber.java:31

    private AbundantNumber() {
    }

    // Function to calculate sum of all divisors including n
    private static int sumOfDivisors(int n) {
        int sum = 1 + n; // 1 and n are always divisors
        for (int i = 2; i <= n / 2; i++) {
            if (n % i == 0) {
                sum += i; // adding divisor to sum
            }
        }
        return sum;
    }

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

    /**
     * Check if {@code number} is an Abundant number or not by checking sum of divisors > 2n
     *
     * @param number the number
     * @return {@code true} if {@code number} is an Abundant number, otherwise false
     */
    public static boolean isAbundant(int number) {
        validatePositiveNumber(number);

        return sumOfDivisors(number) > 2 * number;
    }

    /**
     * Check if {@code number} is an Abundant number or not by checking Aliquot Sum > n
     *

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a positive integer: isAbundant(12).
  2. Validate before calling: if (n > 0) isAbundant(n) else report invalid.
  3. Treat <= 0 as 'not applicable' and branch around the call instead of forwarding it.

Example fix

// before
boolean ab = AbundantNumber.isAbundant(count);
// after
if (count <= 0) throw new IllegalArgumentException("count must be > 0");
boolean ab = AbundantNumber.isAbundant(count);
Defensive patterns

Strategy: validation

Validate before calling

if (n <= 0) {
    throw new IllegalArgumentException("n must be a positive integer");
}
boolean abundant = AbundantNumber.isAbundant(n);

Type guard

static boolean isPositiveInt(int n) {
    return n > 0;
}

Try / catch

try {
    boolean ab = AbundantNumber.isAbundant(n);
} catch (IllegalArgumentException e) {
    // n was <= 0; report invalid input
}

Prevention

When it happens

Trigger: isAbundant(0); isAbundant(-5); calling isAbundant with a value parsed from empty/invalid user input that defaulted to 0; calling the internal sumOfDivisors path with a zero default.

Common situations: Parsing 'how many' inputs where 0 is a natural sentinel; off-by-one loops that reach 0; tests using 0 as a boundary value; negative inputs from subtraction that underflows past the domain.

Related errors


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