TheAlgorithms/Java · error · IllegalArgumentException

Modulus must be positive.

Error message

Modulus must be positive.

What it means

Thrown by FastExponentiation.fastExponentiation when mod <= 0. Modular exponentiation (base^exp) % mod requires a positive modulus; a non-positive modulus makes the `% mod` operation undefined or meaningless and the algorithm's base = base % mod reduction would behave incorrectly. The guard runs before any reduction, so reaching the loop confirms mod is positive.

Source

Thrown at src/main/java/com/thealgorithms/maths/FastExponentiation.java:44

    }

    /**
     * Performs fast exponentiation to calculate (base^exp) % mod using the method
     * of exponentiation by squaring.
     *
     * <p>This method efficiently computes the result by squaring the base and halving
     * the exponent at each step. It multiplies the base to the result when the exponent is odd.
     *
     * @param base the base number to be raised to the power of exp
     * @param exp the exponent to which the base is raised
     * @param mod the modulus to ensure the result does not overflow
     * @return (base^exp) % mod
     * @throws IllegalArgumentException if the modulus is less than or equal to 0
     * @throws ArithmeticException if the exponent is negative (not supported in this implementation)
     */
    public static long fastExponentiation(long base, long exp, long mod) {
        if (mod <= 0) {
            throw new IllegalArgumentException("Modulus must be positive.");
        }

        if (exp < 0) {
            throw new ArithmeticException("Negative exponent is not supported.");
        }

        long result = 1;
        base = base % mod; // Take the modulus of the base to handle large base values

        // Fast exponentiation by squaring algorithm
        while (exp > 0) {
            // If exp is odd, multiply the base to the result
            if ((exp & 1) == 1) { // exp & 1 checks if exp is odd
                result = result * base % mod;
            }
            // Square the base and halve the exponent
            base = base * base % mod; // base^2 % mod to avoid overflow
            exp >>= 1; // Right shift exp to divide it by 2

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a positive modulus such as fastExponentiation(base, exp, 1000000007).
  2. Validate mod > 0 at the caller before invoking.
  3. If mod comes from input, default to a known positive constant when absent.

Example fix

// before
long r = FastExponentiation.fastExponentiation(2, 10, 0);

// after
long r = FastExponentiation.fastExponentiation(2, 10, 1000000007L);
Defensive patterns

Strategy: validation

Validate before calling

if (mod <= 0) {
    throw new IllegalArgumentException("modulus must be > 0");
}
FastExponentiation.fastExponentiation(base, exp, mod);

Type guard

static boolean isPositiveModulus(long mod) { return mod > 0; }

Prevention

When it happens

Trigger: Calling fastExponentiation(base, exp, 0), with a negative mod, or with mod parsed from config that defaulted to 0. Common in cryptographic contexts where the modulus must be a positive prime or composite.

Common situations: Modulus read from a missing config key (parses to 0); modulus computed as a difference that underflowed; passing a sentinel 0; off-by-one in prime generation yielding 0.

Related errors


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