TheAlgorithms/Java · error · IllegalArgumentException

Number must be greater than zero.

Error message

Number must be greater than zero.

What it means

Thrown by SquareFreeInteger.isSquareFreeInteger(int number) when number <= 0. Square-free integers are defined for positive integers (no prime squared divides them), so zero and negatives are out of domain. The guard fires before delegating to PrimeFactorization.pfactors and comparing list/set sizes.

Source

Thrown at src/main/java/com/thealgorithms/maths/Prime/SquareFreeInteger.java:32

import java.util.HashSet;
import java.util.List;

public final class SquareFreeInteger {
    private SquareFreeInteger() {
    }
    /**
     * This method returns whether an integer is square free
     *
     * @param number Integer value which is to be checked
     * @return false when number has repeated prime factors
     *         true when number has non repeated prime factors
     * @throws IllegalArgumentException when number is negative or zero
     */
    public static boolean isSquareFreeInteger(int number) {

        if (number <= 0) {
            // throw exception when number is less than or is zero
            throw new IllegalArgumentException("Number must be greater than zero.");
        }

        // Store prime factors of number which is passed as argument
        // in a list
        List<Integer> primeFactorsList = PrimeFactorization.pfactors(number);

        // Create set from list of prime factors of integer number
        // if size of list and set is equal then the argument passed to this method is square free
        // if size of list and set is not equal then the argument passed to this method is not
        // square free
        return primeFactorsList.size() == new HashSet<>(primeFactorsList).size();
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate number > 0 at the caller and reject or clamp before calling isSquareFreeInteger.
  2. Fix upstream arithmetic producing non-positive values.
  3. Define domain-specific semantics for 0/negatives (typically 'not square-free') before calling.

Example fix

// before
boolean sf = SquareFreeInteger.isSquareFreeInteger(n);

// after
if (n <= 0) {
    throw new IllegalArgumentException("n must be > 0: " + n);
}
boolean sf = SquareFreeInteger.isSquareFreeInteger(n);
Defensive patterns

Strategy: validation

Validate before calling

if (number <= 0) {
    throw new IllegalArgumentException("number must be > 0: " + number);
}
boolean sf = SquareFreeInteger.isSquareFreeInteger(number);

Prevention

When it happens

Trigger: Calling isSquareFreeInteger(0), isSquareFreeInteger(-6), or any isSquareFreeInteger(number) where number <= 0.

Common situations: User-supplied integer not validated for positivity; arithmetic expression crossing zero; loop bound off-by-one; deserialized field accepting non-positive values; calling on a difference that can be zero or negative.

Related errors


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