Number theory & modular arithmetic: practice problems with step-by-step solutions
Also called: modular arithmetic · Euclidean algorithm · RSA
Free Number theory & modular arithmetic practice problems with diagrams, hints and full worked solutions. New values every time, at three levels: gcd and remainders, inverses and congruences, modular powers and RSA.
How to solve Number theory & modular arithmetic problems
Reduce every number modulo n first; remainders lie between 0 and n − 1.
For a gcd, divide repeatedly with Euclid’s algorithm; the last nonzero remainder is the gcd.
For an inverse, run the extended algorithm and keep the coefficient of a; add n if it is negative.
Solve a·x ≡ b by multiplying with a⁻¹; combine two congruences by writing x = r₁ + m₁k.
For powers, write the exponent in binary and square repeatedly; in RSA, d = e⁻¹ mod φ(n) with φ(n) = (p − 1)(q − 1).
Common mistakes
Giving a negative remainder for a negative number.
Stopping Euclid’s algorithm at the first remainder.
Leaving the inverse negative instead of adding n.
Taking the RSA exponent d modulo n instead of φ(n).
Reducing the exponent modulo p instead of p − 1 with Fermat’s theorem.
Key formulas
Division with remainder
a = q·n + r, 0 ≤ r < n
Euclid
gcd(a, b) = gcd(b, a mod b)
Least common multiple
lcm(a, b) = a·b/gcd(a, b)
Inverse
a·a⁻¹ ≡ 1 (mod n), gcd(a, n) = 1
Linear congruence
a·x ≡ b → x ≡ a⁻¹·b (mod n)
Euler’s totient
φ(n) = n·∏(1 − 1/p), φ(pᵏ) = pᵏ − pᵏ⁻¹
Fermat
aᵖ⁻¹ ≡ 1 (mod p)
RSA
n = pq, φ = (p − 1)(q − 1), d = e⁻¹ mod φ, c = mᵉ mod n, m = cᵈ mod n
Learn Number theory & modular arithmetic from scratch
1Remainders and congruences
Dividing a by n gives a quotient q and a remainder r: a = q·n + r with 0 ≤ r < n. The remainder is written a mod n. For a negative number q is negative too: −3 = −1·7 + 4, so −3 mod 7 = 4.
Two numbers are congruent modulo n, a ≡ b (mod n), when they leave the same remainder, that is when n divides a − b.
Remainders can be reduced at every step, which keeps the numbers small:
(a + b) mod n = ((a mod n) + (b mod n)) mod n, and the same for a·b
For example 47·58 mod 9: 47 ≡ 2 and 58 ≡ 4, so 47·58 ≡ 8 (mod 9).
Self-check What is −17 mod 5?
2
3
−2
−3
Answer: 3. −17 = −4·5 + 3, so the remainder is 3. The answer 2 is 17 mod 5; a remainder is never negative.
2Euclid’s algorithm
The greatest common divisor gcd(a, b) is the largest number that divides both. Euclid’s algorithm uses gcd(a, b) = gcd(b, a mod b):
Divide: a = q·b + r.
Replace (a, b) with (b, r).
Repeat until the remainder is 0. The last nonzero remainder is the gcd.
The least common multiple follows from it: lcm(a, b) = a·b / gcd(a, b).
Self-check What is gcd(84, 36)?
12
252
4
6
Answer: 12. 84 = 2·36 + 12 and 36 = 3·12 + 0, so the gcd is 12. 252 is the least common multiple, 84·36/12.
3Inverses and the extended algorithm
The inverse of a modulo n is the number x with a·x ≡ 1 (mod n). It exists exactly when gcd(a, n) = 1.
The extended Euclidean algorithm runs Euclid on (n, a) and keeps a coefficient t for each remainder, so that every remainder r ≡ t·a (mod n). Start with t = 0 for n and t = 1 for a; each new t is the t two rows up minus q times the t one row up. When the remainder 1 appears, its t is the inverse.
With the inverse, a congruence a·x ≡ b (mod n) is solved by x ≡ a⁻¹·b. When d = gcd(a, n) is larger than 1, there are d solutions if d divides b, and none otherwise.
Two congruences with coprime moduli, x ≡ r₁ (mod m₁) and x ≡ r₂ (mod m₂), have exactly one solution modulo m₁·m₂ (the Chinese remainder theorem). Write x = r₁ + m₁k and solve for k.
Repeated squaring computes aᵏ mod n quickly: write k in binary, find a, a², a⁴, a⁸, … mod n by squaring and reducing, and multiply the powers whose bit is 1.
Fermat’s little theorem: for a prime p that does not divide a, aᵖ⁻¹ ≡ 1 (mod p). So the exponent can be reduced modulo p − 1.
RSA builds a key from two primes p and q:
n = p·q and φ(n) = (p − 1)(q − 1).
Choose e with gcd(e, φ(n)) = 1; (n, e) is the public key.
Compute d = e⁻¹ mod φ(n): the private key.
Encrypt c = mᵉ mod n; decrypt m = cᵈ mod n.
Euler’s φ(n) counts the numbers from 1 to n that are coprime to n: φ(p) = p − 1 for a prime, φ(pᵏ) = pᵏ − pᵏ⁻¹, and φ is multiplicative for coprime factors.
Self-check An RSA key uses p = 5, q = 11 and e = 3. What is d?
13
27
37
3
Answer: 27. φ(n) = 4·10 = 40, and 3·27 = 81 = 2·40 + 1, so d = 27. The inverse of 3 modulo n = 55 would be 37, but d is taken modulo φ(n).
5Common mistakes
Giving a negative remainder for a negative number: −17 mod 5 is 3, not −2.
Stopping Euclid at the first remainder instead of the last nonzero one.
Forgetting to add n to a negative coefficient from the extended algorithm.
Computing the RSA exponent d modulo n instead of modulo φ(n).
Reducing an exponent modulo p instead of p − 1 with Fermat’s theorem.
6How exam questions look
Easy: gcd and remainders. You run Euclid’s algorithm for a gcd or lcm, find remainders of negative numbers, and reduce sums and products modulo n.
Medium: inverses and congruences. You find modular inverses with the extended algorithm, solve linear congruences, combine two congruences with the Chinese remainder theorem, and compute Euler’s φ(n).
Hard: modular powers and RSA. You use repeated squaring and Fermat’s theorem, solve congruences with several solutions, and find RSA private exponents, ciphertexts and decrypted messages.
Find (914·953) mod 11 without multiplying out the large numbers: reduce first.
n = 11
Find (914·953) mod 11
Show hints
(x·y) mod n = ((x mod n)·(y mod n)) mod n, and the same holds for sums.
914 mod 11 = 1, 953 mod 11 = 7.
Reduce the product of the remainders modulo 11.
Show the answer and the step-by-step solution
Divisibility and remainders: a mod n is the remainder r with 0 ≤ r < n and a = q·n + r, also when a is negative. Euclid’s algorithm finds gcd(a, b) by repeating a = q·b + r and replacing (a, b) with (b, r); the last nonzero remainder is the gcd. Then lcm(a, b) = a·b/gcd(a, b). Remainders can be reduced at every step: (a + b) mod n and (a·b) mod n only depend on a mod n and b mod n.
Reduce each number
914 ≡ 1, 953 ≡ 7 (mod 11)
Combine
1·7 = 7 ≡ 7 (mod 11)
Answer
(914·953) mod 11 = 7
Example 2Medium
Find Euler’s totient φ(637), the number of integers from 1 to 637 that are coprime to 637.
n = 637 = 72·13
Find φ(637)
Show hints
Factor n into primes: 637 = 72·13.
φ is multiplicative for coprime factors, and φ(pᵏ) = pᵏ − pᵏ⁻¹.
φ(n) = n·∏(1 − 1/p).
Show the answer and the step-by-step solution
Inverses and congruences: a has an inverse modulo n exactly when gcd(a, n) = 1. The extended Euclidean algorithm writes 1 = s·n + t·a, and then a⁻¹ ≡ t (mod n). A congruence a·x ≡ b (mod n) with gcd(a, n) = 1 has the solution x ≡ a⁻¹·b. Two congruences x ≡ r₁ (mod m₁) and x ≡ r₂ (mod m₂) with coprime moduli have one solution modulo m₁·m₂ (Chinese remainder theorem). Euler’s φ(n) counts the numbers from 1 to n that are coprime to n: φ(n) = n·∏(1 − 1/p).
Prime factors
637 = 72·13
Totient
φ(637) = φ(49)·φ(13) = 42·12 = 504
Answer
φ(637) = 504
Example 3Hard
An RSA key uses p = 13, q = 37 and e = 23. Decrypt the ciphertext c = 225: find the message m.
p = 13q = 37e = 23c = 225
Find m
Show hints
n = 481, φ(n) = 432.
d = e⁻¹ mod φ(n) = 263.
m = cd mod n = 225263 mod 481: use repeated squaring.
Show the answer and the step-by-step solution
Modular powers and RSA: compute aᵏ mod n by repeated squaring: write k in binary, square, reduce after every step and multiply the powers whose bit is 1. Fermat: aᵖ⁻¹ ≡ 1 (mod p) for a prime p not dividing a, so the exponent can be reduced modulo p − 1. RSA: choose primes p and q, n = p·q and φ(n) = (p − 1)(q − 1); the public exponent e is coprime to φ(n), and the private exponent is d = e⁻¹ mod φ(n). Encrypt with c = mᵉ mod n and decrypt with m = cᵈ mod n.
Private exponent
n = 481, φ(n) = 432
d = 23−1 mod 432 = 263
Decrypt by repeated squaring
263 = 256 + 4 + 2 + 1
2251 ≡ 225 (mod 481)
2252 ≡ 120 (mod 481)
2254 ≡ 451 (mod 481)
2258 ≡ 419 (mod 481)
22516 ≡ 477 (mod 481)
22532 ≡ 16 (mod 481)
22564 ≡ 256 (mod 481)
225128 ≡ 120 (mod 481)
225256 ≡ 451 (mod 481)
225263 ≡ 225·120·451·451 ≡ 361 (mod 481)
Message
m = 361
check: 36123 mod 481 = 225
Answer
m = 361
Questions and answers
What is Number theory & modular arithmetic?
Dividing a by n gives a quotient q and a remainder r: a = q·n + r with 0 ≤ r < n. The remainder is written a mod n. For a negative number q is negative too: −3 = −1·7 + 4, so −3 mod 7 = 4.
Two numbers are congruent modulo n, a ≡ b (mod n), when they leave the same remainder, that is when n divides a − b.
How do I solve Number theory & modular arithmetic problems step by step?
Reduce every number modulo n first; remainders lie between 0 and n − 1.
For a gcd, divide repeatedly with Euclid’s algorithm; the last nonzero remainder is the gcd.
For an inverse, run the extended algorithm and keep the coefficient of a; add n if it is negative.
Solve a·x ≡ b by multiplying with a⁻¹; combine two congruences by writing x = r₁ + m₁k.
For powers, write the exponent in binary and square repeatedly; in RSA, d = e⁻¹ mod φ(n) with φ(n) = (p − 1)(q − 1).
What are common mistakes in Number theory & modular arithmetic?
Giving a negative remainder for a negative number.
Stopping Euclid’s algorithm at the first remainder.
Leaving the inverse negative instead of adding n.
Taking the RSA exponent d modulo n instead of φ(n).
Reducing the exponent modulo p instead of p − 1 with Fermat’s theorem.
What do the Number theory & modular arithmetic exercises cover?
Easy: gcd and remainders
Medium: inverses and congruences
Hard: modular powers and RSA
Every problem has new values, so you learn the method instead of an answer. Stuck? Open a small hint first, and the full explanation only when you need it.
How practising works
Every problem has new values, so you learn the method instead of an answer.
Stuck? Open a small hint first, and the full explanation only when you need it.
Your progress, mistakes and saved problems are kept, so you can review before the exam.