Compute bⁿ mod m in O(log n) multiplications using repeated squaring — crucial for RSA!
Algorithm: write n in binary, square-and-multiply
Example: 3^11 mod 7
11 = (1011)₂
Result built bit-by-bit from MSB
⚡ Modular Exponentiation (Step-by-Step)
Binary Addition
🔬 Binary Adder
🔑 4.3 Primes & Greatest Common Divisors
Prime Numbers
p > 1 is prime if its only divisors are 1 and p. The Fundamental Theorem of Arithmetic guarantees unique prime factorization for every integer > 1.
Every n > 1 = p₁^a₁ · p₂^a₂ · ... · pₖ^aₖ (unique)
🔬 Primality Tester & Factorizer
Sieve of Eratosthenes
Eliminate multiples of each prime starting from 2. What remains are primes.
🧮 Interactive Sieve
GCD — Euclidean Algorithm
gcd(a,b) = gcd(b, a mod b) [repeat until b=0]
Example: gcd(252,198):
252 = 198·1 + 54
198 = 54·3 + 36
54 = 36·1 + 18
36 = 18·2 + 0 → gcd = 18
🔬 Euclidean Algorithm (Step-by-Step)
Extended Euclidean Algorithm (Bézout)
Finds s, t such that: gcd(a,b) = s·a + t·b
🔬 Extended GCD
LCM
lcm(a,b) = |a·b| / gcd(a,b)
🔬 LCM Calculator
⚖️ 4.4 Solving Congruences
Linear Congruences
ax ≡ b (mod m)
Has solution ⟺ gcd(a,m) | b
Number of solutions: d = gcd(a,m)
🔬 Linear Congruence Solver: ax ≡ b (mod m)
Modular Inverse
a⁻¹ mod m exists iff gcd(a,m) = 1. Found via Extended Euclidean.
a · a⁻¹ ≡ 1 (mod m)
🔬 Modular Inverse Finder
Chinese Remainder Theorem (CRT)
x ≡ a₁ (mod m₁)
x ≡ a₂ (mod m₂) [m₁,m₂ coprime]
...
Unique solution mod M = m₁·m₂·...·mₙ
🔬 CRT Solver (2 equations)
Fermat's Little Theorem
p prime, p ∤ a → a^(p-1) ≡ 1 (mod p)
Corollary: a^p ≡ a (mod p)
🔬 Fermat's Little Theorem Verifier
🛠️ 4.5 Applications of Congruences
Hashing
h(k) = k mod m (maps key k to one of m buckets)
🔬 Hash Table Simulator
Pseudorandom Numbers — Linear Congruential Generator
x_{n+1} = (a·xₙ + c) mod m
Parameters: multiplier a, increment c, modulus m, seed x₀
🎲 LCG Simulator
Check Digits — ISBN-10
Validity: Σᵢ₌₁¹⁰ i·dᵢ ≡ 0 (mod 11)
Check digit = (11 - (Σᵢ₌₁⁹ i·dᵢ) mod 11) mod 11
📚 ISBN-10 Check Digit Calculator
Calendar — Day of Week
All calendar math reduces to modular arithmetic mod 7.
📅 Day-of-Week Calculator
🔐 4.6 Cryptography
Caesar / Shift Cipher
Encrypt: C = (P + k) mod 26
Decrypt: P = (C − k + 26) mod 26
🔬 Caesar Cipher
Affine Cipher
Encrypt: C = (a·P + b) mod 26 [need gcd(a,26)=1]
Decrypt: P = a⁻¹·(C − b) mod 26
🔬 Affine Cipher
RSA Cryptosystem Public Key
1. Pick primes p, q → n = p·q
2. φ(n) = (p−1)(q−1)
3. Choose e: 1 < e < φ(n), gcd(e,φ(n))=1
4. Find d: e·d ≡ 1 (mod φ(n))
Public key: (n, e) Private key: (n, d)
Encrypt: C = Mᵉ mod n
Decrypt: M = Cᵈ mod n
🔐 RSA Key Generation
Diffie-Hellman Key Exchange Key Agreement
Public: prime p, generator g
Alice picks secret a → sends A = gᵃ mod p
Bob picks secret b → sends B = gᵇ mod p
Shared secret: K = Bᵃ mod p = Aᵇ mod p = g^{ab} mod p
(Eavesdropper sees A,B,p,g but cannot find K without discrete log)
🤝 Diffie-Hellman Simulator
Euler's Totient φ(n)
φ(n) = count of integers in [1,n] coprime to n
φ(p) = p−1 (p prime)
φ(pq) = (p−1)(q−1) (p,q distinct primes)