Math & Geometry
Modulo Calculator: a mod n, Modular Inverse and Modular Power
Calculate remainders in modular arithmetic. The expression a mod n gives the remainder when a is divided by n, always as a number from 0 to n − 1. Modular arithmetic is sometimes called clock arithmetic, because a clock works modulo 12: four hours after 10 o’clock is 2 o’clock, since 14 mod 12 is 2. The same idea appears in the days of the week, which work modulo 7, in check digits for ISBNs, bank account numbers and IBANs, in hash tables and random number generators, and at the heart of modern public-key cryptography such as RSA and Diffie–Hellman. For negative numbers, mathematicians define the result as a non-negative remainder, so −7 mod 3 is 2, but many programming languages return the truncated remainder −1, so the calculator shows both. It also calculates the modular inverse of a, the number x such that a times x leaves a remainder of 1, which exists only when a and n have no common factor, and modular exponentiation, a to a power modulo n, computed efficiently by repeated squaring so that even large exponents are handled exactly. Enter a, the modulus n and, optionally, an exponent. For example, 17 mod 5 is 2, and the inverse of 3 modulo 7 is 5, because 3 × 5 = 15 = 2 × 7 + 1.
How to use this calculator
- Enter the whole number a.
- Enter the modulus n, 1 or more.
- Optionally enter an exponent e.
- Read a mod n, the truncated remainder and the gcd.
- Read the modular inverse and aᵉ mod n.
Negative numbers and programming
Languages differ: Python’s % returns a non-negative result for a positive modulus, while C, Java and JavaScript return a result with the sign of the dividend. To get the mathematical result in those languages, use ((a % n) + n) % n.
Knowing which convention your code uses prevents subtle bugs, for example in calendar calculations with negative offsets.
Modular arithmetic in cryptography
RSA encryption raises a message to a large power modulo the product of two big primes; decryption uses a modular inverse of the exponent. Repeated squaring makes this practical even with numbers hundreds of digits long.
Fermat’s little theorem states that for a prime p and a not divisible by p, a^(p−1) ≡ 1 (mod p), which underlies many primality tests.
Formula & method
a mod n = a − n × ⌊a ÷ n⌋, always between 0 and n − 1Modular inverse x: a × x ≡ 1 (mod n), which exists only if gcd(a, n) = 1Found with the extended Euclidean algorithmaᵉ mod n computed by repeated squaringa mod n = a − n × ⌊a ÷ n⌋, in the range 0 to n − 1 (floor or Euclidean definition). The truncated remainder used by C, Java and JavaScript’s % operator takes the sign of a: −7 % 3 = −1. Two numbers are congruent modulo n, a ≡ b (mod n), if they leave the same remainder. Modular inverse: x with a × x ≡ 1 (mod n); it exists if and only if gcd(a, n) = 1 and is found with the extended Euclidean algorithm. Modular exponentiation aᵉ mod n uses repeated squaring, taking about log₂(e) multiplications. Calculations use exact integer arithmetic (BigInt) for numbers up to 9 × 10¹⁵.
Worked example
17 mod 5 and the inverse of 3 mod 7
- 17 = 5 × 3 + 2, so 17 mod 5 = 2.
- For 3 mod 7: try 3 × 5 = 15 = 2 × 7 + 1, so the inverse is 5.
- 4⁵ mod 13: 4⁵ = 1,024 = 78 × 13 + 10, so the result is 10.
Answer: 17 mod 5 = 2; 3⁻¹ ≡ 5 (mod 7); 4⁵ ≡ 10 (mod 13).
Frequently asked questions
What does mod mean?
The remainder after division by the modulus.
What is −7 mod 3?
2 in mathematics; −1 in languages that truncate.
What is a modular inverse?
A number x such that a × x leaves remainder 1 modulo n.
When does a modular inverse exist?
Only when a and n share no common factor.
What is modular exponentiation?
Raising a number to a power and taking the remainder modulo n.
Where is modulo used?
In clocks, calendars, check digits, hashing and cryptography.