TheAlgorithms/Java · error · IllegalArgumentException
Given range of values is invalid!
Error message
Given range of values is invalid!
What it means
Thrown by AmicableNumber.findAllInRange when the range is structurally invalid: from<=0 OR to<=0 OR to<from. Amicable numbers are defined on positive integers and the search is a nested loop from i..to, so a non-positive endpoint or an inverted range is rejected before any work.
Source
Thrown at src/main/java/com/thealgorithms/maths/AmicableNumber.java:34
* link: https://en.wikipedia.org/wiki/Amicable_numbers
* <p>
* Simple Example: (220, 284)
* 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110} <-SUM = 284
* 284 is divisible by {1,2,4,71,142} <-SUM = 220.
*/
public final class AmicableNumber {
private AmicableNumber() {
}
/**
* Finds all the amicable numbers in a given range.
*
* @param from range start value
* @param to range end value (inclusive)
* @return list with amicable numbers found in given range.
*/
public static Set<Pair<Integer, Integer>> findAllInRange(int from, int to) {
if (from <= 0 || to <= 0 || to < from) {
throw new IllegalArgumentException("Given range of values is invalid!");
}
Set<Pair<Integer, Integer>> result = new LinkedHashSet<>();
for (int i = from; i < to; i++) {
for (int j = i + 1; j <= to; j++) {
if (isAmicableNumber(i, j)) {
result.add(Pair.of(i, j));
}
}
}
return result;
}
/**
* Checks whether 2 numbers are AmicableNumbers or not.
*/
public static boolean isAmicableNumber(int a, int b) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Supply a valid positive, ordered range: findAllInRange(1, 1000).
- Normalize bounds before calling: lo=min(a,b), hi=max(a,b), then reject if lo<=0.
- Validate user input and show a field error instead of forwarding to the API.
Example fix
// before
Set<?> r = AmicableNumber.findAllInRange(start, end);
// after
int lo = Math.min(start, end), hi = Math.max(start, end);
if (lo <= 0) throw new IllegalArgumentException("range start must be positive");
Set<?> r = AmicableNumber.findAllInRange(lo, hi); Defensive patterns
Strategy: validation
Validate before calling
int lo = Math.min(from, to), hi = Math.max(from, to);
if (lo <= 0) {
throw new IllegalArgumentException("range must be within positive integers");
}
Set<?> r = AmicableNumber.findAllInRange(lo, hi); Type guard
static boolean isValidRange(int from, int to) {
return from > 0 && to > 0 && from <= to;
} Try / catch
try {
Set<?> r = AmicableNumber.findAllInRange(from, to);
} catch (IllegalArgumentException e) {
// bad range; ask the user for corrected bounds
} Prevention
- Normalize min/max before calling so an inverted pair still works.
- Validate range bounds at the UI/controller boundary.
- Remember 'to' is inclusive; from==to is allowed.
When it happens
Trigger: findAllInRange(0, 10); findAllInRange(-3, 5); findAllInRange(10, 5) (inverted); findAllInRange where both bounds come from user input with no ordering check.
Common situations: UI range inputs where the user left 'from' blank (parses to 0); swapping min/max in the caller; inclusive-vs-exclusive bound confusion producing to<from.
Related errors
- Numbers array cannot be empty or null
- Number must be positive.
- Input numbers must be natural!
- 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/e4b453ba9b844916.
Report an issue: GitHub.