TheAlgorithms/Java · error · IllegalArgumentException

Number must be greater than zero.

Error message

Number must be greater than zero.

What it means

Thrown by LiouvilleLambdaFunction.liouvilleLambda(int number) when number <= 0. The Liouville function λ(n) is defined only for positive integers (it counts the parity of prime factors with multiplicity), so zero and negatives are out of domain. The guard fires before delegating to PrimeFactorization.pfactors.

Source

Thrown at src/main/java/com/thealgorithms/maths/Prime/LiouvilleLambdaFunction.java:30

 *
 * */

public final class LiouvilleLambdaFunction {
    private LiouvilleLambdaFunction() {
    }

    /**
     * This method returns λ(n) of given number n
     *
     * @param number Integer value which λ(n) is to be calculated
     * @return  1 when number has even number of prime factors
     *         -1 when number has odd number of prime factors
     * @throws IllegalArgumentException when number is negative
     */
    public static int liouvilleLambda(int number) {
        if (number <= 0) {
            // throw exception when number is less than or is zero
            throw new IllegalArgumentException("Number must be greater than zero.");
        }

        // return 1 if size of prime factor list is even, -1 otherwise
        return PrimeFactorization.pfactors(number).size() % 2 == 0 ? 1 : -1;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate number > 0 at the caller and reject or clamp before calling liouvilleLambda.
  2. Fix upstream arithmetic (loop bounds, subtractions) producing non-positive values.
  3. If 0 or negatives are meaningful in your domain, define your own handling before calling.

Example fix

// before
int lambda = LiouvilleLambdaFunction.liouvilleLambda(n);

// after
if (n <= 0) {
    throw new IllegalArgumentException("n must be > 0: " + n);
}
int lambda = LiouvilleLambdaFunction.liouvilleLambda(n);
Defensive patterns

Strategy: validation

Validate before calling

if (number <= 0) {
    throw new IllegalArgumentException("number must be > 0: " + number);
}
int lambda = LiouvilleLambdaFunction.liouvilleLambda(number);

Prevention

When it happens

Trigger: Calling liouvilleLambda(0), liouvilleLambda(-5), or any liouvilleLambda(number) where number <= 0.

Common situations: User-supplied integer not validated for positivity; result of an arithmetic expression (subtraction, decrement) that crossed zero; loop index off-by-one hitting 0; deserialized field that accepted non-positive values.

Related errors


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