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
- Pass a positive integer: isAbundant(12).
- Validate before calling: if (n > 0) isAbundant(n) else report invalid.
- 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
- Reject 0/negative at the input boundary (parser/UI), not at the math call.
- Use 1, not 0, as the start value for search loops feeding isAbundant.
- Add a unit test for the 0 and negative boundaries.
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
- Input numbers must be natural!
- Numbers array cannot be empty or null
- Given range of values is invalid!
- Side length must be greater than 0
- Length must be greater than 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ca56d8f949defa9b.
Report an issue: GitHub.