TheAlgorithms/Java · error · IllegalArgumentException
Please input Integer Number between 0 and 500
Error message
Please input Integer Number between 0 and 500
What it means
Thrown by PiNilakantha.calculatePi(int iterations) when iterations is outside [0, 500]. The Nilakantha series approximation is bounded because double precision is insufficient for more than ~500 iterations (terms vanish into rounding noise), and negative iterations are meaningless. The guard enforces both bounds before the summation loop.
Source
Thrown at src/main/java/com/thealgorithms/maths/PiNilakantha.java:27
// https://en.scratch-wiki.info/wiki/Calculating_Pi
public static void main(String[] args) {
assert calculatePi(0) == 3.0;
assert calculatePi(10) > 3.0;
assert calculatePi(100) < 4.0;
System.out.println(calculatePi(500));
}
/**
* @param iterations number of times the infinite series gets repeated Pi
* get more accurate the higher the value of iterations is Values from 0 up
* to 500 are allowed since double precision is not sufficient for more than
* about 500 repetitions of this algorithm
* @return the pi value of the calculation with a precision of x iteration
*/
public static double calculatePi(int iterations) {
if (iterations < 0 || iterations > 500) {
throw new IllegalArgumentException("Please input Integer Number between 0 and 500");
}
double pi = 3;
int divCounter = 2;
for (int i = 0; i < iterations; i++) {
if (i % 2 == 0) {
pi = pi + 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2));
} else {
pi = pi - 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2));
}
divCounter += 2;
}
return pi;
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Clamp iterations to [0, 500] before calling: iterations = Math.max(0, Math.min(500, requested)).
- Validate at the input boundary and surface a domain-specific error for out-of-range requests.
- If higher precision is genuinely needed, switch to a BigDecimal-based or arbitrary-precision pi estimator.
Example fix
// before double pi = PiNilakantha.calculatePi(requestedIterations); // after int iters = Math.max(0, Math.min(500, requestedIterations)); double pi = PiNilakantha.calculatePi(iters);
Defensive patterns
Strategy: validation
Validate before calling
int iters = Math.max(0, Math.min(500, requestedIterations)); double pi = PiNilakantha.calculatePi(iters);
Prevention
- Clamp iteration counts to the documented [0, 500] range before calling.
- Validate user-supplied iteration counts at the input boundary.
- Switch to arbitrary-precision estimators if more than 500 iterations are genuinely needed.
When it happens
Trigger: Calling calculatePi(-1), calculatePi(0) (allowed), calculatePi(500) (allowed), or calculatePi(501) — any value < 0 or > 500.
Common situations: User-supplied iteration count from a CLI or request payload not validated against the bound; caller assumed more iterations = more accuracy and exceeded 500; default value of -1 used as a sentinel but not handled; auto-scaling logic (e.g., iterations = precision * 100) overflowing the cap.
Related errors
- Input x-coordinates must be unique.
- Array should contain an even number of elements
- baseNumbers must be non-empty.
- n must be non-negative.
- multiplicativePersistence() does not accept negative values
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/254872a4867ca910.
Report an issue: GitHub.