number theory calculators

Modular Arithmetic Calculator

Computes modular addition, multiplication, and exponentiation of integers under a chosen modulus. Essential for cryptography, clock arithmetic, hash functions, and competitive programming problems.

About this calculator

Modular arithmetic treats numbers on a 'clock' of size m: the expression a mod m gives the remainder when a is divided by m, always yielding a value in {0, 1, …, m−1}. The key operations are: addition — (a + b) mod m; multiplication — (a × b) mod m; and modular exponentiation — aᵇ mod m. For modular exponentiation the calculator uses repeated multiplication: result = (result × base) mod m iterated b times, ensuring the intermediate values never exceed m². The notation a ≡ r (mod m) means a and r differ by a multiple of m. These operations preserve the ring structure of integers, so (a + b) mod m = ((a mod m) + (b mod m)) mod m — a property exploited to keep numbers small during computation.

How to use

Calculate 7³ mod 5 using the power operation. Enter base = 7, exponent = 3, modulus = 5, and operation = 'power'. The algorithm runs: step 1 — result = (1 × 7) mod 5 = 7 mod 5 = 2; step 2 — result = (2 × 7) mod 5 = 14 mod 5 = 4; step 3 — result = (4 × 7) mod 5 = 28 mod 5 = 3. Output: 3. Verify directly: 7³ = 343, and 343 ÷ 5 = 68 remainder 3. ✓ Now try addition: (13 + 9) mod 7 = 22 mod 7 = 1, entered as base = 13, exponent = 9, modulus = 7, operation = 'add'.

Frequently asked questions

How does modular exponentiation work and why is it important in cryptography?

Modular exponentiation computes aᵇ mod m efficiently. The naïve method multiplies a by itself b times while reducing mod m at each step to keep numbers small. A faster approach — fast exponentiation or square-and-multiply — does it in O(log b) multiplications by repeatedly squaring the base. In RSA encryption, security relies on raising large numbers to huge exponents mod n, where n is a product of two large primes; fast modular exponentiation makes encryption and decryption feasible while factoring n remains computationally hard. Without modular reduction, intermediate values would have billions of digits.

What is the difference between the remainder operator and true modular arithmetic for negative numbers?

In most programming languages, the % operator returns the remainder with the sign of the dividend: −7 % 3 = −1 in JavaScript. True mathematical modular arithmetic always returns a non-negative result: −7 mod 3 = 2, because −7 = (−3)·3 + 2. This distinction matters in cryptographic code and when computing modular inverses. To get the mathematical result in code, use ((a % m) + m) % m. This calculator follows the mathematical convention, so results are always in {0, …, m−1}.

When would I use modular arithmetic outside of cryptography?

Modular arithmetic appears whenever quantities wrap around. Clock time is mod 12 (or 24): 11 hours after 3 o'clock is 2 o'clock, not 14. Day-of-week calculations are mod 7, and calendar algorithms rely heavily on modular arithmetic. In computer science, hash tables map keys to bucket indices using key mod tableSize. In music theory, interval arithmetic is mod 12 (semitones). Competitive programmers use it to keep large intermediate results manageable, typically computing answers mod 10⁹ + 7 to avoid integer overflow.