All Tools

Modular Arithmetic Calculator

Compute modular operations with fast exponentiation, find modular inverses using the extended Euclidean algorithm, solve systems of congruences via the Chinese Remainder Theorem, and explore clock arithmetic on an interactive number circle.

Presets:

Inputs

Results

7mod12=7 \bmod 12 =
7
(7+5)mod12=(7 + 5) \bmod 12 =
0
(7×5)mod12=(7 \times 5) \bmod 12 =
11
710mod12=7^{10} \bmod 12 =
1

Fast Exponentiation: 7^10 mod 12

Binary exponentiation computes abmodna^b \bmod n in O(logb)O(\log b) multiplications by repeated squaring.

7101(mod12)7^{10} \equiv 1 \pmod{12}

Binary of 10: 1010 — each bit corresponds to a squaring step.

Clock Diagram (a + b) mod 12

0011223344556677889910101111mod 127 + 5 = 0a = 7result = 0

Reference Guide

Modular Arithmetic

Two integers are congruent modulo nn if they differ by a multiple of nn. We write ab(modn)a \equiv b \pmod{n} when n(ab)n \mid (a - b).

175(mod12)because 175=1217 \equiv 5 \pmod{12} \quad \text{because } 17 - 5 = 12

The four basic operations carry through: addition, subtraction, and multiplication all respect congruences. Fast exponentiation uses binary squaring to compute abmodna^b \bmod n in O(logb)O(\log b) steps.

210=102424(mod1000)2^{10} = 1024 \equiv 24 \pmod{1000}

Modular Inverse

The modular inverse of aa modulo nn is the value a1a^{-1} such that aa11(modn)a \cdot a^{-1} \equiv 1 \pmod{n}. It exists if and only if gcd(a,n)=1\gcd(a, n) = 1.

315(mod7)since 3×5=1513^{-1} \equiv 5 \pmod{7} \quad \text{since } 3 \times 5 = 15 \equiv 1

The extended Euclidean algorithm finds integers x,yx, y such that ax+ny=gcd(a,n)=1ax + ny = \gcd(a,n) = 1, giving xx as the inverse. This runs in O(logn)O(\log n) time.

Chinese Remainder Theorem

If m1,m2,,mkm_1, m_2, \ldots, m_k are pairwise coprime, then the system of congruences xri(modmi)x \equiv r_i \pmod{m_i} has a unique solution modulo M=m1m2mkM = m_1 m_2 \cdots m_k.

{x2(mod3)x3(mod5)x8(mod15)\begin{cases} x \equiv 2 \pmod{3} \\ x \equiv 3 \pmod{5} \end{cases} \Rightarrow x \equiv 8 \pmod{15}

The solution is built iteratively: merge two congruences at a time using the extended Euclidean algorithm, doubling the modulus at each step.

Applications in Cryptography

Modular arithmetic underpins modern cryptography. In RSA, encryption is cme(modn)c \equiv m^e \pmod{n} and decryption uses mcd(modn)m \equiv c^d \pmod{n}, where n=pqn = pq is a product of large primes.

Euler's totient φ(n)\varphi(n) counts integers coprime to nn. For RSA: φ(pq)=(p1)(q1)\varphi(pq) = (p-1)(q-1). The key insight is Euler's theorem: aφ(n)1(modn)a^{\varphi(n)} \equiv 1 \pmod{n} for gcd(a,n)=1\gcd(a,n)=1.

φ(12)=4{1,5,7,11} coprime to 12\varphi(12) = 4 \quad \{1,5,7,11\} \text{ coprime to } 12

Caesar and affine ciphers use Z26\mathbb{Z}_{26} (mod 26). The CRT helps reconstruct large integers from their residues, used in efficient multi-prime RSA.