Math behind key pairs

The number theory cryptography runs on - modular arithmetic, primes, gcds, and the theorems of Fermat and Euler, built up from scratch.

Modular Arithmetic Foundations

Modular Arithmetic

The modulus m defines the wrap-around space. Every integer maps to one output from 0 to m-1.
These outputs are called residues.

MOD

mod 3 has exactly 3 possible residues

Residues set

mod 3 { 0, 1, 2 }

Modulus space

Integers space

Congruence check (mod 3)

A

=/=

B

A and B are not congruent modulo 3, because they map to different residues {0} and {2}.

In the modulus space, they are different numbers.

When divided by modulus 3, they have different remainders {0} and {2}.

n = q * m + r

Euclidean division breaks C into full groups of m plus the leftover residue.

n

The integer being converted (here: C).

m

The modulus (the group size).

q

Quotient: how many full groups of m fit into n.

r (Residue)

Remainder after removing q * m, with 0 <= r < m.

C

Quotient q

17

Full groups of size 3.

Residue r

0

Leftover after grouping, with 0 <= r < 3.

Visual equation

51=17 * 3+0

Step-by-step

1) 51 / 3 = 17 remainder 0

2) Largest multiple below C: 51 = 17 * 3

3) Residue: 51 - 51 = 0

4) Congruence: 51 === 0 (mod 3)

Modular Operations

(A+B) mod m=((A mod m)+(B mod m)) mod m(A + B) \bmod m = ((A \bmod m) + (B \bmod m)) \bmod m

A

+

B

Path A: keep full wraps

A starts at 0, then B starts where A stopped.

01212 + 9mod 3
A steps: 12B steps: 9

A: 12 = 4 * 3 + 0 (clockwise)

B: 9 = 3 * 3 + 0 (clockwise)

Path B: shortcut full wraps

Same chaining, but with full wraps removed first.

0120 + 0mod 3
A mod 3: 0B mod 3: 0

A === 0 (mod 3)

B === 0 (mod 3)

Same final residue

(12 + 9) mod 3 = 0

((12 mod 3) + (9 mod 3)) mod 3 = (0 + 0) mod 3 = 0

Proof for modular addition

We prove:

(A + B) mod C = ((A mod C) + (B mod C)) mod C

Write:

A = C * Q1 + R1
B = C * Q2 + R2
with 0 <= R1, R2 < C.

Key fact: (C * t) mod C = 0
Multiples of C do not affect the remainder.

Add first

(A + B) mod C

= ((C * Q1 + R1) + (C * Q2 + R2)) mod C

= (C * (Q1 + Q2) + (R1 + R2)) mod C

= (R1 + R2) mod C

Reduce first

A mod C = R1
B mod C = R2

((A mod C) + (B mod C)) mod C

= (R1 + R2) mod C

Both sides equal (R1 + R2) mod C. Proof complete.

(A⋅B) mod m=((A mod m)⋅(B mod m)) mod m(A \cdot B) \bmod m = ((A \bmod m) \cdot (B \bmod m)) \bmod m

A

*

B

Same final residue

(8 * 11) mod 3 = 1

((8 mod 3) * (11 mod 3)) mod 3 = (2 * 2) mod 3 = 1

Proof for modular multiplication

We prove:

(A * B) mod C = ((A mod C) * (B mod C)) mod C

Write:

A = C * Q1 + R1
B = C * Q2 + R2
with 0 <= R1, R2 < C.

Key fact: (C * t) mod C = 0
Multiples of C do not affect the remainder.

Multiply first

(A * B) mod C

= ((C * Q1 + R1) * (C * Q2 + R2)) mod C

= (C * C * Q1 * Q2 + C * Q1 * R2 + C * Q2 * R1 + R1 * R2) mod C

= (C * (C * Q1 * Q2 + Q1 * R2 + Q2 * R1) + R1 * R2) mod C

= (R1 * R2) mod C

Reduce first

A mod C = R1
B mod C = R2

((A mod C) * (B mod C)) mod C

= (R1 * R2) mod C

Both sides equal (R1 * R2) mod C. Proof complete.

(AB) mod m=((A mod m)B) mod m(A^B) \bmod m = ((A \bmod m)^B) \bmod m

A

^

B

Same final residue

(5^13) mod 3 = 2

((5 mod 3)^13) mod 3 = (2^13) mod 3 = 2

Fast modular exponentiation (square-and-multiply)

A^B grows very fast, e.g. 2^90 = 1 237 940 039 285 380 274 899 124 224 - computational problem

Large exponent can be split into smaller: 2^90 mod C = (2^45 * 2^45) mod C = ((2^10 * 2^35) * (2^10 * 2^35)) mod C = ...

Binary trick - split and substitute ^2 for fast calculation:

7^1 mod 13 = 7

7^2 mod 13 = (7^1 * 7^1) mod 13 = (7 * 7) mod 13 = 10

7^4 mod 13 = (7^2 * 7^2) mod 13 = (10 * 10) mod 13 = 9

7^8 mod 13 = (7^4 * 7^4) mod 13 = (9 * 9) mod 13 = 3

7^16 mod 13 = (7^8 * 7^8) mod 13 = (3 * 3) mod 13 = 9

7^32 mod 13 = (7^16 * 7^16) mod 13 = (9 * 9) mod 13 = 3

7^64 mod 13 = (7^32 * 7^32) mod 13 = (3 * 3) mod 13 = 9

7^128 mod 13 = (7^64 * 7^64) mod 13 = (9 * 9) mod 13 = 3

7^256 mod 13 = (7^128 * 7^128) mod 13 = (3 * 3) mod 13 = 9

Binary trick for any number:
2^90 mod 13 - 90 is not a power of 2, so we cannot apply fast calculation directly, but we can split 90 (and any number) into a sum of powers of 2, e.g.
90 = 64 + 16 + 8 + 2 = 2^5 + 2^4 + 2^3 + 2^1

And then use the fact that (A * B) mod C = ((A mod C) * (B mod C)) mod C:
2^90 mod 13 = ((2^64 mod 13) * (2^16 mod 13) * (2^8 mod 13) * (2^2 mod 13)) mod 13 = (3 * 3 * 9 * 4) mod 13 = 1296 mod 13 = 10
So, 1 237 940 039 285 380 274 899 124 224 mod 13 = 10.

To fast split any number into a sum of powers of 2, use binary representation of the number:
90 = 1011010 = 2^6 + 2^4 + 2^3 + 2^1 = 64 + 16 + 8 + 2

Modular Arithmetic Examples

Calculation examples

Click on an example to expand it and see the steps.

Step 1 - reduce the base

521≡11≡−6(mod17)521 \equiv 11 \equiv -6 \pmod{17}
521637≡(−6)637(mod17)521^{637} \equiv (-6)^{637} \pmod{17}

Step 2 - expose a square

(−6)637=−6⋅6636=−6⋅(62)318(-6)^{637} = -6 \cdot 6^{636} = -6 \cdot (6^2)^{318}
62=36≡2(mod17)6^2 = 36 \equiv 2 \pmod{17}
⇒−6⋅(62)318≡−6⋅2318(mod17)\Rightarrow -6 \cdot (6^2)^{318} \equiv -6 \cdot 2^{318} \pmod{17}

Step 3 - reduce the exponent, searching for a smaller residue, ideally 1 or -1

318=2+4⋅79⇒2318=22⋅(24)79318 = 2 + 4 \cdot 79 \Rightarrow 2^{318} = 2^2 \cdot (2^4)^{79}
24=16≡−1(mod17)2^4 = 16 \equiv -1 \pmod{17}

Step 4 - evaluate the sign and reduce

−6⋅2318=−6⋅22⋅(24)79≡−6⋅4⋅(−1)79=24-6 \cdot 2^{318} = -6 \cdot 2^2 \cdot (2^4)^{79} \equiv -6 \cdot 4 \cdot (-1)^{79} = 24
24 mod 17=724 \bmod 17 = 7

Final residue

∴ 521637≡7(mod17)\therefore\ 521^{637} \equiv 7 \pmod{17}
Pattern to reuse: reduce to a small residue first, then look for a power that becomes 1 or -1 modulo m.

Step 1 - rewrite the big power

7128=(72)647^{128} = (7^2)^{64}
(72)64≡4964≡1664(mod33)(7^2)^{64} \equiv 49^{64} \equiv 16^{64} \pmod{33}

Step 2 - convert to base 2

1664=(24)64=225616^{64} = (2^4)^{64} = 2^{256}
2256=2⋅2255=2⋅(25)512^{256} = 2 \cdot 2^{255} = 2 \cdot (2^5)^{51}

Step 3 - reduce by using -1

25=32≡−1(mod33)2^5 = 32 \equiv -1 \pmod{33}
2⋅(25)51≡2⋅(−1)51=−2(mod33)2 \cdot (2^5)^{51} \equiv 2 \cdot (-1)^{51} = -2 \pmod{33}

Step 4 - normalize the residue

−2≡31(mod33)-2 \equiv 31 \pmod{33}

Final residue

∴ 7128≡31(mod33)\therefore\ 7^{128} \equiv 31 \pmod{33}
After reducing the base, split exponents to expose a known residue like -1 and collapse quickly.

Step 1 - find a useful power

24=16≡1(mod15)2^4 = 16 \equiv 1 \pmod{15}

Step 2 - split the exponent

2018=4⋅504+22018 = 4 \cdot 504 + 2
22018=(24)504⋅222^{2018} = (2^4)^{504} \cdot 2^2

Step 3 - reduce modulo 15

(24)504⋅22≡1504⋅4≡4(mod15)(2^4)^{504} \cdot 2^2 \equiv 1^{504} \cdot 4 \equiv 4 \pmod{15}

Final residue

∴ 22018≡4(mod15)\therefore\ 2^{2018} \equiv 4 \pmod{15}
When a^k is congruent to 1 (mod m), split the exponent as kq + r. The repeated k-block collapses to 1, so only the small remainder r matters.

Step 1 - find a helpful power

24=16≡−1(mod17)2^4 = 16 \equiv -1 \pmod{17}

Step 2 - split the exponent by that power

2018=4⋅504+22018 = 4 \cdot 504 + 2
22018=(24)504⋅222^{2018} = (2^4)^{504} \cdot 2^2

Step 3 - reduce modulo 17

(24)504⋅22≡(−1)504⋅4≡1⋅4≡4(mod17)(2^4)^{504} \cdot 2^2 \equiv (-1)^{504} \cdot 4 \equiv 1 \cdot 4 \equiv 4 \pmod{17}

Final residue

∴ 22018≡4(mod17)\therefore\ 2^{2018} \equiv 4 \pmod{17}
When a^k is congruent to -1 (mod m), parity does the work: even powers become 1, odd powers stay -1.

Step 1 - reduce the base modulo 6

7≡1(mod6)7 \equiv 1 \pmod{6}

Step 2 - raise both sides to the n-th power

72=7⋅7≡1⋅1≡1(mod6)7^2 = 7 \cdot 7 \equiv 1 \cdot 1 \equiv 1 \pmod{6}
⇒7n≡1n≡1(mod6)\Rightarrow 7^n \equiv 1^n \equiv 1 \pmod{6}

Step 3 - subtract 1

7n−1≡1−1≡0(mod6)7^n - 1 \equiv 1 - 1 \equiv 0 \pmod{6}

Final residue

∴ 7n−1≡0(mod6) for all n≥1\therefore\ 7^n - 1 \equiv 0 \pmod{6}\ \text{for all } n \ge 1
Reusable rule: if a is congruent to 1 modulo m, then a^n - 1 is congruent to 0 modulo m.

Step 1 - reduce 13 modulo 10

13≡3(mod10)13 \equiv 3 \pmod{10}
⇒13n≡3n(mod10)\Rightarrow 13^n \equiv 3^n \pmod{10}

Step 2 - rewrite the full expression modulo 10

13n+3n+2≡3n+3n+2(mod10)13^n + 3^{n+2} \equiv 3^n + 3^{n+2} \pmod{10}

Step 3 - factor out 3^n

3n+3n+2=3n(1+32)=3n(1+9)=3n⋅103^n + 3^{n+2} = 3^n(1 + 3^2) = 3^n(1 + 9) = 3^n \cdot 10

Step 4 - use that 10 is 0 modulo 10

3n⋅10≡0(mod10)3^n \cdot 10 \equiv 0 \pmod{10}

Final residue

∴ 13n+3n+2≡0(mod10) for all n≥0\therefore\ 13^n + 3^{n+2} \equiv 0 \pmod{10}\ \text{for all } n \ge 0
Useful pattern: after reduction, factor common terms and check whether the remaining factor is a multiple of the modulus.

Step 1 - rewrite both powers

7n+2=7n⋅72=49⋅7n7^{n+2} = 7^n \cdot 7^2 = 49 \cdot 7^n
82n+1=8⋅(82)n=8⋅64n8^{2n+1} = 8 \cdot (8^2)^n = 8 \cdot 64^n

Step 2 - reduce 64 modulo 57

64≡7(mod57)⇒64n≡7n(mod57)64 \equiv 7 \pmod{57} \Rightarrow 64^n \equiv 7^n \pmod{57}
⇒82n+1≡8⋅7n(mod57)\Rightarrow 8^{2n+1} \equiv 8 \cdot 7^n \pmod{57}

Step 3 - combine like terms

7n+2+82n+1≡49⋅7n+8⋅7n=(49+8)⋅7n=57⋅7n(mod57)7^{n+2} + 8^{2n+1} \equiv 49 \cdot 7^n + 8 \cdot 7^n = (49 + 8) \cdot 7^n = 57 \cdot 7^n \pmod{57}

Step 4 - conclude modulo 57

57⋅7n≡0(mod57)57 \cdot 7^n \equiv 0 \pmod{57}

Final residue

∴ 7n+2+82n+1≡0(mod57) for all n≥0\therefore\ 7^{n+2} + 8^{2n+1} \equiv 0 \pmod{57}\ \text{for all } n \ge 0
When two terms can be rewritten with the same power factor, factor it out and check if the remaining coefficient is the modulus.

Step 1 - name the modulus

m=2n−3m = 2^n - 3

Step 2 - rewrite the base using m

2n−1=(2n−3)+2=m+22^n - 1 = (2^n - 3) + 2 = m + 2

Step 3 - reduce the expression modulo m

(2n−1)n−3=(m+2)n−3(2^n - 1)^n - 3 = (m + 2)^n - 3
m+2≡2(modm)⇒(m+2)n−3≡2n−3(modm)m + 2 \equiv 2 \pmod{m} \Rightarrow (m + 2)^n - 3 \equiv 2^n - 3 \pmod{m}

Step 4 - substitute m back clearly

2n−3(modm)=2n−3(mod2n−3)≡0(modn)2^n - 3 \pmod{m} = 2^n - 3 \pmod{2^n - 3} \equiv 0 \pmod{n}

Final residue

∴ (2n−1)n−3≡0(mod2n−3) for all n≥2\therefore\ (2^n - 1)^n - 3 \equiv 0 \pmod{2^n - 3}\ \text{for all } n \ge 2
Strategy for variable moduli: set m equal to the modulus, rewrite terms in the form m + c, then reduce exactly as usual.

Divisibility rules

Decimal decomposition of an integer

Any number in decimal system can be represented as a sum of powers of 10.

General form

N=anan−1…a1a0‾N = \overline{a_n a_{n-1}\ldots a_1 a_0}
N=a0+10⋅anan−1…a1‾N = a_0 + 10\cdot \overline{a_n a_{n-1}\ldots a_1}
N=a0+a1⋅10+102⋅anan−1…a2‾N = a_0 + a_1\cdot 10 + 10^2\cdot \overline{a_n a_{n-1}\ldots a_2}
N=a0+a1⋅10+a2⋅102+⋯+an⋅10nN = a_0 + a_1\cdot 10 + a_2\cdot 10^2 + \cdots + a_n\cdot 10^n

N

1234=1234‾1234 = \overline{1234}
1234=4+10⋅123‾1234 = 4 + 10\cdot \overline{123}
1234=4+3⋅10+102⋅12‾1234 = 4 + 3\cdot 10 + 10^2\cdot \overline{12}
1234=4+3⋅10+2⋅102+103⋅1‾1234 = 4 + 3\cdot 10 + 2\cdot 10^2 + 10^3\cdot \overline{1}
1234=4+3⋅10+2⋅102+1⋅1031234 = 4 + 3\cdot 10 + 2\cdot 10^2 + 1\cdot 10^3
1234=4+30+200+10001234 = 4 + 30 + 200 + 1000

Rule for division by: none

Click a value on the line to see its divisibility rule.

Proof

10 is divisible by d∈{2,5,10}10 \text{ is divisible by } d \in \{2,5,10\}
N=anan−1…a1a0‾=10⋅anan−1…a1‾+a0N = \overline{a_n a_{n-1} \ldots a_1 a_0} = 10 \cdot \overline{a_n a_{n-1} \ldots a_1} + a_0
10⋅anan−1…a1‾ is a multiple of 10, so it is also a multiple of d10 \cdot \overline{a_n a_{n-1} \ldots a_1} \text{ is a multiple of } 10, \text{ so it is also a multiple of } d
Since by modulo multiples of d are 0, N≡a0(modd)\text{Since by modulo multiples of } d \text{ are } 0, \text{ } N \equiv a_0 \pmod{d}

Examples

To check divisibility by 2,5,10, check the last digit a0.\text{To check divisibility by } 2,5,10, \text{ check the last digit } a_0.
N≡a0(modd)N \equiv a_0 \pmod{d}
348≡8(mod2)⇒348 is divisible by 2348 \equiv 8 \pmod{2} \Rightarrow 348 \text{ is divisible by } 2
735≡5(mod5)⇒735 is divisible by 5735 \equiv 5 \pmod{5} \Rightarrow 735 \text{ is divisible by } 5
1240≡0(mod10)⇒1240 is divisible by 101240 \equiv 0 \pmod{10} \Rightarrow 1240 \text{ is divisible by } 10
1243≡3(mod10)⇒1243 is not divisible by 10, the remainder is 31243 \equiv 3 \pmod{10} \Rightarrow 1243 \text{ is not divisible by } 10 \text{, the remainder is 3}

Proof

100 is divisible by every d∈{1,2,4,5,10,20,25,50,100}100 \text{ is divisible by every } d \in \{1,2,4,5,10,20,25,50,100\}
In this block, we apply it to d∈{4,20,25,50}\text{In this block, we apply it to } d \in \{4,20,25,50\}
N=anan−1…a2a1a0‾=100⋅anan−1…a2‾+a1a0‾N = \overline{a_n a_{n-1} \ldots a_2 a_1 a_0} = 100 \cdot \overline{a_n a_{n-1} \ldots a_2} + \overline{a_1 a_0}
100⋅anan−1…a2‾ is a multiple of 100, so it is also a multiple of d100 \cdot \overline{a_n a_{n-1} \ldots a_2} \text{ is a multiple of } 100, \text{ so it is also a multiple of } d
Since by modulo multiples of d are 0, N≡a1a0‾(modd)\text{Since by modulo multiples of } d \text{ are } 0, \text{ } N \equiv \overline{a_1 a_0} \pmod{d}

Examples

To check divisibility by 4,20,25,50, check the last 2 digits a1a0‾.\text{To check divisibility by } 4,20,25,50, \text{ check the last 2 digits } \overline{a_1 a_0}.
N≡a1a0‾(modd)N \equiv \overline{a_1 a_0} \pmod{d}
316≡16(mod4)⇒16 is divisible by 4⇒316 is divisible by 4316 \equiv 16 \pmod{4} \Rightarrow 16 \text{ is divisible by } 4 \Rightarrow 316 \text{ is divisible by } 4
1340≡40(mod20)⇒40 is divisible by 20⇒1340 is divisible by 201340 \equiv 40 \pmod{20} \Rightarrow 40 \text{ is divisible by } 20 \Rightarrow 1340 \text{ is divisible by } 20
1325≡25(mod25)⇒25 is divisible by 25⇒1325 is divisible by 251325 \equiv 25 \pmod{25} \Rightarrow 25 \text{ is divisible by } 25 \Rightarrow 1325 \text{ is divisible by } 25
7450≡50(mod50)⇒50 is divisible by 50⇒7450 is divisible by 507450 \equiv 50 \pmod{50} \Rightarrow 50 \text{ is divisible by } 50 \Rightarrow 7450 \text{ is divisible by } 50
1274≡74(mod50)⇒74 is not divisible by 50⇒1274 is not divisible by 50, the remainder is 241274 \equiv 74 \pmod{50} \Rightarrow 74 \text{ is not divisible by } 50 \Rightarrow 1274 \text{ is not divisible by } 50 \text{, the remainder is } 24

Proof

1000 is divisible by every d∈{1,2,4,5,8,10,20,25,40,50,100,125,200,250,500,1000}1000 \text{ is divisible by every } d \in \{1,2,4,5,8,10,20,25,40,50,100,125,200,250,500,1000\}
In this block, we apply it to d∈{8,40}\text{In this block, we apply it to } d \in \{8,40\}
N=anan−1…a3a2a1a0‾=1000⋅anan−1…a3‾+a2a1a0‾N = \overline{a_n a_{n-1} \ldots a_3 a_2 a_1 a_0} = 1000 \cdot \overline{a_n a_{n-1} \ldots a_3} + \overline{a_2 a_1 a_0}
1000⋅anan−1…a3‾ is a multiple of 1000, so it is also a multiple of d1000 \cdot \overline{a_n a_{n-1} \ldots a_3} \text{ is a multiple of } 1000, \text{ so it is also a multiple of } d
Since by modulo multiples of d are 0, N≡a2a1a0‾(modd)\text{Since by modulo multiples of } d \text{ are } 0, \text{ } N \equiv \overline{a_2 a_1 a_0} \pmod{d}

Examples

To check divisibility by 8,40, check the last 3 digits a2a1a0‾.\text{To check divisibility by } 8,40, \text{ check the last 3 digits } \overline{a_2 a_1 a_0}.
N≡a2a1a0‾(modd)N \equiv \overline{a_2 a_1 a_0} \pmod{d}
1232≡232(mod8)⇒232 is divisible by 8⇒1232 is divisible by 81232 \equiv 232 \pmod{8} \Rightarrow 232 \text{ is divisible by } 8 \Rightarrow 1232 \text{ is divisible by } 8
7640≡640(mod40)⇒640 is divisible by 40⇒7640 is divisible by 407640 \equiv 640 \pmod{40} \Rightarrow 640 \text{ is divisible by } 40 \Rightarrow 7640 \text{ is divisible by } 40
7314≡314(mod40)⇒314 is not divisible by 40⇒7314 is not divisible by 40, the remainder is 347314 \equiv 314 \pmod{40} \Rightarrow 314 \text{ is not divisible by } 40 \Rightarrow 7314 \text{ is not divisible by } 40 \text{, the remainder is } 34

Proof

d∈{3,9}d \in \{3,9\}
10≡1(modd)⇒10k≡1k≡1(modd)10 \equiv 1 \pmod{d} \Rightarrow 10^k \equiv 1^k \equiv 1 \pmod{d}
N=a0+10a1+102a2+⋯+10nan≡a0+1⋅a1+1⋅a2+⋯+1⋅an(modd)=a0+a1+a2+⋯+anN = a_0 + 10a_1 + 10^2a_2 + \cdots + 10^n a_n \equiv a_0 + 1\cdot a_1 + 1\cdot a_2 + \cdots + 1\cdot a_n \pmod{d} = a_0 + a_1 + a_2 + \cdots + a_n
So, number N is divisible by d if sum of digits a0+a1+a2+⋯+an is divisible by d\text{So, number } N \text{ is divisible by } d \text{ if sum of digits } a_0 + a_1 + a_2 + \cdots + a_n \text{ is divisible by } d

Examples

To check divisibility by 3,9, check the sum of digits.\text{To check divisibility by } 3,9, \text{ check the sum of digits.}
N≡a0+a1+⋯+an(modd)N \equiv a_0 + a_1 + \cdots + a_n \pmod{d}
123≡1+2+3=6(mod3)⇒6 is divisible by 3⇒123 is divisible by 3123 \equiv 1+2+3 = 6 \pmod{3} \Rightarrow 6 \text{ is divisible by } 3 \Rightarrow 123 \text{ is divisible by } 3
729≡7+2+9=18(mod9)⇒18 is divisible by 9⇒729 is divisible by 9729 \equiv 7+2+9 = 18 \pmod{9} \Rightarrow 18 \text{ is divisible by } 9 \Rightarrow 729 \text{ is divisible by } 9
124≡1+2+4=7(mod3)⇒7 is not divisible by 3⇒124 is not divisible by 3, the remainder is 1124 \equiv 1+2+4 = 7 \pmod{3} \Rightarrow 7 \text{ is not divisible by } 3 \Rightarrow 124 \text{ is not divisible by } 3 \text{, the remainder is } 1
1237≡1+2+3+7=13≡4(mod9)⇒13 is not divisible by 9⇒1237 is not divisible by 9, the remainder is 41237 \equiv 1+2+3+7 = 13 \equiv 4 \pmod{9} \Rightarrow 13 \text{ is not divisible by } 9 \Rightarrow 1237 \text{ is not divisible by } 9 \text{, the remainder is } 4

Proof

d=11d = 11
10≡−1(mod11)⇒10k≡(−1)k(mod11)10 \equiv -1 \pmod{11} \Rightarrow 10^k \equiv (-1)^k \pmod{11}
N=a0+10a1+102a2+⋯+10nan≡a0−a1+a2−a3+⋯+(−1)nan(mod11)N = a_0 + 10a_1 + 10^2a_2 + \cdots + 10^n a_n \equiv a_0 - a_1 + a_2 - a_3 + \cdots + (-1)^n a_n \pmod{11}
So, number N is divisible by 11 if the alternating digit sum a0−a1+a2−a3+⋯+(−1)nan is divisible by 11\text{So, number } N \text{ is divisible by } 11 \text{ if the alternating digit sum } a_0 - a_1 + a_2 - a_3 + \cdots + (-1)^n a_n \text{ is divisible by } 11

Examples

To check divisibility by 11, compute the alternating sum of digits.\text{To check divisibility by } 11, \text{ compute the alternating sum of digits.}
N≡a0−a1+a2−a3+⋯+(−1)nan(mod11)N \equiv a_0 - a_1 + a_2 - a_3 + \cdots + (-1)^n a_n \pmod{11}
121≡1−2+1=0(mod11)⇒121 is divisible by 11121 \equiv 1-2+1 = 0 \pmod{11} \Rightarrow 121 \text{ is divisible by } 11
462≡2−6+4=0(mod11)⇒462 is divisible by 11462 \equiv 2-6+4 = 0 \pmod{11} \Rightarrow 462 \text{ is divisible by } 11
1234≡4−3+2−1=2(mod11)⇒1234 is not divisible by 11, the remainder is 21234 \equiv 4-3+2-1 = 2 \pmod{11} \Rightarrow 1234 \text{ is not divisible by } 11 \text{, the remainder is } 2

Proof

d∈{7,13}d \in \{7,13\}
1001=7⋅11⋅131001 = 7 \cdot 11 \cdot 13
1001≡0(modd)⇒1000≡−1(modd)1001 \equiv 0 \pmod{d} \Rightarrow 1000 \equiv -1 \pmod{d}
N=a3m+2a3m+1a3m…a2a1a0‾=a2a1a0‾+1000⋅a5a4a3‾+10002⋅a8a7a6‾+⋯+1000m⋅a3m+2a3m+1a3m‾N = \overline{a_{3m+2} a_{3m+1} a_{3m} \ldots a_2 a_1 a_0} = \overline{a_2 a_1 a_0} + 1000\cdot\overline{a_5 a_4 a_3} + 1000^2\cdot\overline{a_8 a_7 a_6} + \cdots + 1000^m\cdot\overline{a_{3m+2} a_{3m+1} a_{3m}}
(Leftmost block may contain leading zeros)\text{(Leftmost block may contain leading zeros)}
N≡a2a1a0‾+(−1)⋅a5a4a3‾+(−1)2⋅a8a7a6‾−⋯+(−1)ma3m+2a3m+1a3m‾(modd)N \equiv \overline{a_2 a_1 a_0} + (-1)\cdot \overline{a_5 a_4 a_3} + (-1)^2\cdot \overline{a_8 a_7 a_6} - \cdots + (-1)^m\overline{a_{3m+2} a_{3m+1} a_{3m}} \pmod{d}
So, number N is divisible by d∈{7,13} if the alternating sum of 3-digit blocks is divisible by d\text{So, number } N \text{ is divisible by } d \in \{7,13\} \text{ if the alternating sum of 3-digit blocks is divisible by } d

Examples

To check divisibility by 7 or 13, split into 3-digit blocks and alternate signs.\text{To check divisibility by } 7 \text{ or } 13, \text{ split into 3-digit blocks and alternate signs.}
N≡a2a1a0‾−a5a4a3‾+a8a7a6‾−⋯+(−1)ma3m+2a3m+1a3m‾(modd)N \equiv \overline{a_2 a_1 a_0} - \overline{a_5 a_4 a_3} + \overline{a_8 a_7 a_6} - \cdots + (-1)^m\overline{a_{3m+2} a_{3m+1} a_{3m}} \pmod{d}
20384=020 ∣ 384⇒20384≡384−020=364≡0(mod7)⇒20384 is divisible by 720384 = 020\,|\,384 \Rightarrow 20384 \equiv 384-020 = 364 \equiv 0 \pmod{7} \Rightarrow 20384 \text{ is divisible by } 7
2704=002 ∣ 704⇒2704≡704−002=702≡0(mod13)⇒2704 is divisible by 132704 = 002\,|\,704 \Rightarrow 2704 \equiv 704-002 = 702 \equiv 0 \pmod{13} \Rightarrow 2704 \text{ is divisible by } 13
12345=012 ∣ 345⇒12345≡345−012=333≡4(mod7)⇒12345 is not divisible by 7, the remainder is 412345 = 012\,|\,345 \Rightarrow 12345 \equiv 345-012 = 333 \equiv 4 \pmod{7} \Rightarrow 12345 \text{ is not divisible by } 7 \text{, the remainder is } 4
12345=012 ∣ 345⇒12345≡333≡8(mod13)⇒12345 is not divisible by 13, the remainder is 812345 = 012\,|\,345 \Rightarrow 12345 \equiv 333 \equiv 8 \pmod{13} \Rightarrow 12345 \text{ is not divisible by } 13 \text{, the remainder is } 8

Primes

Numbers decomposition

Prime numbers - the building blocks that combine to build all other numbers.

3
3
·
5
·
11

= 495

Primes lie rather unpredictably on the number line.

Every integer greater than 1 can be broken down as a product of primes. This prime set is unique for each number, like a fingerprint.

6 =

2
·
3

Greatest common divisor

Divisibility

Intuition: "3 divides 12" means 12 = 3 * 4, or easier to think 12 is a multiple of 3.

Divisibility Definition

Let a,b∈Z. We say a∣b if there exists c∈Z such that ac=b.\text{Let } a,b \in \mathbb{Z}.\ \text{We say } a \mid b \text{ if there exists } c \in \mathbb{Z} \text{ such that } ac = b.
Notation: \text{Notation: }
a∣b - "a divides b" - a statementa \mid b \text{ - "a divides b" - a statement}
ab - "a divided by b" - a number\frac{a}{b} \text{ - "a divided by b" - a number}

Number a divides b if you can build b using all blocks from a.
For example, 12 divides 60:

60 =

12
2
·
2
·
3
·
5

GCD Definition

Let a,b∈Z . Then gdc of a and b, denoted gcd⁡(a,b) is the largest integer g such that g∣a and g∣b.\text{Let } a,b \in \mathbb{Z}\ \text{. Then gdc of a and b, denoted } \gcd(a,b) \text{ is the largest integer } g \text{ such that } g \mid a \text{ and } g \mid b.
a=p1α1p2α2⋯pnαn,a = p_1^{\alpha_1} p_2^{\alpha_2} \cdots p_n^{\alpha_n},
b=p1β1p2β2⋯pnβn,b = p_1^{\beta_1} p_2^{\beta_2} \cdots p_n^{\beta_n},
gcd⁡(a,b)=p1min⁡(α1,β1)p2min⁡(α2,β2)⋯pnmin⁡(αn,βn)\gcd(a,b) = p_1^{\min(\alpha_1,\beta_1)} p_2^{\min(\alpha_2,\beta_2)} \cdots p_n^{\min(\alpha_n,\beta_n)}

A

B

12 =
18 =
2
·
2
·
3
2
·
3
·
3
GCD(12, 18) =
2
·
3
=6

LCM Definition

Let a,b∈Z . Then the lcm of a and b, denoted lcm⁡(a,b), is the smallest positive integer m such that a∣m and b∣m.\text{Let } a,b \in \mathbb{Z}\ \text{. Then the lcm of } a \text{ and } b, \text{ denoted } \operatorname{lcm}(a,b), \text{ is the smallest positive integer } m \text{ such that } a \mid m \text{ and } b \mid m.
a=p1α1p2α2⋯pnαn,a = p_1^{\alpha_1} p_2^{\alpha_2} \cdots p_n^{\alpha_n},
b=p1β1p2β2⋯pnβn,b = p_1^{\beta_1} p_2^{\beta_2} \cdots p_n^{\beta_n},
lcm⁡(a,b)=p1max⁡(α1,β1)p2max⁡(α2,β2)⋯pnmax⁡(αn,βn)\operatorname{lcm}(a,b) = p_1^{\max(\alpha_1,\beta_1)} p_2^{\max(\alpha_2,\beta_2)} \cdots p_n^{\max(\alpha_n,\beta_n)}

LCM includes all blocks from a, and all blocks from b, repeating blocks can be shared between a and b.

A

B

12 =
18 =
2
·
2
·
3
2
·
3
·
3
LCM(12, 18) =
2
·
2
·
3
·
3
=36

Euclidean algorithm

Find gcd(A, B) by playing with piles of pebbles

Having two piles of pebbles, what happens if you remove smaller pile from the larger one, and repeat until piles are equal?
Turns out, the final pile size is the greatest common divisor of the original piles.

A

B

Proof for the Euclidean algorithm

Proposition. Let a,b∈Z>0 with a≤b. Then gcd⁡(a,b)=gcd⁡(a,b−a).\text{Proposition. Let } a,b \in \mathbb{Z}_{>0} \text{ with } a \le b. \text{ Then } \gcd(a,b) = \gcd(a,b-a).
Suppose g=gcd⁡(a,b).\text{Suppose } g = \gcd(a,b).
g∣a and g∣b.g \mid a \text{ and } g \mid b.
So there exist α,β∈Z such that a=αg, b=βg.\text{So there exist } \alpha,\beta \in \mathbb{Z} \text{ such that } a = \alpha g,\ b = \beta g.
b−a=βg−αg=g(β−α),b-a = \beta g - \alpha g = g(\beta-\alpha),
hence g∣(b−a).\text{hence } g \mid (b-a).
Therefore g is a common divisor of a and b−a, so g≤gcd⁡(a,b−a).\text{Therefore } g \text{ is a common divisor of } a \text{ and } b-a, \text{ so } g \le \gcd(a,b-a).
Conversely, suppose h=gcd⁡(a,b−a).\text{Conversely, suppose } h = \gcd(a,b-a).
h∣a and h∣(b−a).h \mid a \text{ and } h \mid (b-a).
So there exist α,β∈Z such that a=αh, b−a=βh.\text{So there exist } \alpha,\beta \in \mathbb{Z} \text{ such that } a = \alpha h,\ b-a = \beta h.
Trick: rewrite b as (b−a)+a.\text{Trick: rewrite } b \text{ as } (b-a)+a.
b=(b−a)+a=βh+αh=h(β+α),b = (b-a)+a = \beta h + \alpha h = h(\beta+\alpha),
hence h∣b.\text{hence } h \mid b.
Therefore h is a common divisor of a and b, so h≤gcd⁡(a,b).\text{Therefore } h \text{ is a common divisor of } a \text{ and } b, \text{ so } h \le \gcd(a,b).
gcd⁡(a,b)≤gcd⁡(a,b−a)andgcd⁡(a,b−a)≤gcd⁡(a,b).\gcd(a,b) \le \gcd(a,b-a) \quad \text{and} \quad \gcd(a,b-a) \le \gcd(a,b).
Both≤ and ≥ are true only when the two numbers are equal.\text{Both} \le \text{ and } \ge \text{ are true only when the two numbers are equal.}
Hence gcd⁡(a,b)=gcd⁡(a,b−a).\text{Hence } \gcd(a,b) = \gcd(a,b-a).
Base case: gcd⁡(n,0)=n.\text{Base case: } \gcd(n,0) = n.
Reason: n∣n and n∣0 since 0=n⋅0.\text{Reason: } n \mid n \text{ and } n \mid 0 \text{ since } 0 = n \cdot 0.

Faster Euclidean algorithm

Subtraction removes one copy of the smaller number at a time. Division removes all full copies at once, reducing the number of steps.

If b=qa+r with 0≤r<a, then gcd⁡(a,b)=gcd⁡(a,r).\text{If } b = qa + r \text{ with } 0 \le r < a,\ \text{then } \gcd(a,b) = \gcd(a,r).

Calculate gcd(12, 18):

1. 18 = 1 * 12 + 6, so gcd(12, 18) = gcd(12, 6). Quotient 1 means removing 1 full copy of 12 in one jump.

2. 12 = 2 * 6 + 0, so gcd(12, 6) = gcd(6, 0). Quotient 2 means removing 2 full copies of 6 in one jump.

3. The last non-zero remainder is 6, so gcd(12, 18) = 6.

Extended Euclidean algorithm

Enhancing Euclidean algorithm to solve more problems

Add bookkeeping - track how each remainder is build from the original inputs, that is keep a "recipe chain" for a remainder r in terms of ingredients a and b. This tiny extra piece of information upgrades an algorithm for gcd into a tool for linear Diophantine equations, multiplicative inverses, and the Chinese remainder theorem.

A Diophantine equation is a polynomial equation with integer coefficients for which only integer solutions are of interest. The name Diophantine refers to the Greek mathematician of the 3rd century, Diophantus of Alexandria, who published "Arithmetica", a collection of algebraic problems and the earliest known book on algebra.

There are many types of Diophantine equations, e.g. linear, exponential, quadratic, cubic, etc., here we focus on the linear case.

Linear Diophantine equations are of the form: ax + by = c.

Bezout's identity is a theorem that proves that gcd(a, b) can be expressed as a linear combination of a and b, which is exactly the form of a linear Diophantine equation ax + by = c. And, if we find one solution, we can take multiples of it to get other solutions for 2c, 3c, 4c, etc.

Bezout's Identity Proof

If a,b∈Z are not both 0, then there exist x,y∈Z such that gcd⁡(a,b)=ax+by.\text{If } a,b \in \mathbb{Z} \text{ are not both } 0,\ \text{then there exist } x,y \in \mathbb{Z} \text{ such that } \gcd(a,b)=ax+by.
Well-ordering principle: if set is non-empty and is a subset of natural numbers ∅≠S⊆N, then set S has a smallest element.\text{Well-ordering principle: if set is non-empty and is a subset of natural numbers } \emptyset \ne S \subseteq \mathbb{N},\ \text{then set } S \text{ has a smallest element.}
Division algorithm: for a∈Z, d∈N, ∃q,r∈Z such that a=dq+r, 0≤r<d.\text{Division algorithm: for } a \in \mathbb{Z},\ d \in \mathbb{N},\ \exists q,r \in \mathbb{Z} \text{ such that } a = dq + r,\ 0 \le r < d.
Let S={as+bt∣s,t∈Z, as+bt>0}⊆N. Since a and b are not both 0, S≠∅.\text{Let } S = \{ as + bt \mid s,t \in \mathbb{Z},\ as+bt > 0 \} \subseteq \mathbb{N}. \text{ Since } a \text{ and } b \text{ are not both } 0,\ S \ne \emptyset.
By well-ordering, S has a smallest element, say ax+by, for some x,y∈Z.\text{By well-ordering, } S \text{ has a smallest element, say } ax+by,\ \text{for some } x,y \in \mathbb{Z}.
gcd⁡(a,b)∣a and gcd⁡(a,b)∣b⇒gcd⁡(a,b)∣(ax+by).\gcd(a,b) \mid a \text{ and } \gcd(a,b) \mid b \Rightarrow \gcd(a,b) \mid (ax+by).
Since gcd⁡(a,b) divides ax+by, so gcd⁡(a,b)≤ax+by.\text{Since } \gcd(a,b) \text{ divides } ax+by,\ \text{so } \textcolor{blue}{\gcd(a,b) \le ax+by}.
Let’s represent a as a multiple of ax+by plus a remainder: a=(ax+by)q+r, 0≤r<ax+by.\text{Let's represent } a \text{ as a multiple of } ax+by \text{ plus a remainder: } a = (ax+by)q + r,\ 0 \le r < ax+by.
Rearranging, we get r=a−(ax+by)q=a(1−xq)+b(−yq).\text{Rearranging, we get } r = a - (ax+by)q = a(1-xq)+b(-yq).
So we see that r is another integer combination of a and b.\text{So we see that } r \text{ is another integer combination of } a \text{ and } b.
Plugging r back into the inequality, we get 0≤r=a(1−xq)+b(−yq)<ax+by.\text{Plugging } r \text{ back into the inequality, we get } 0 \le r = a(1-xq)+b(-yq) < ax+by.
If r>0, then r∈S and r<ax+by, which contradicts the fact that ax+by is the smallest element of S. Since 0≤r, we must have r=0.\text{If } r > 0,\ \text{then } r \in S \text{ and } r < ax+by,\ \text{which contradicts the fact that } ax+by \text{ is the smallest element of } S. \text{ Since } 0 \le r,\ \text{we must have } r = 0.
Remainder is 0, which means ax+by∣a.\text{Remainder is } 0,\ \text{which means } ax+by \mid a.
Similarly, we can follow the same steps to show that ax+by∣b.\text{Similarly, we can follow the same steps to show that } ax+by \mid b.
So, ax + by divides both a and b, it is a common divisor of a and b, and ax+by≤gcd⁡(a,b).\text{So, ax + by divides both a and b, it is a common divisor of a and b, and } \textcolor{green}{ax+by \le \gcd(a,b)}.
Combining gcd⁡(a,b)≤ax+by and ax+by≤gcd⁡(a,b), we get gcd⁡(a,b)=ax+by.\text{Combining } \textcolor{blue}{\gcd(a,b) \le ax+by} \text{ and } \textcolor{green}{ax+by \le \gcd(a,b)},\ \text{we get } \gcd(a,b) = ax+by.
∴gcd⁡(a,b)=ax+by.\therefore \gcd(a,b) = ax+by.

Question: Solve the linear Diophantine equation 196x + 180y = 4

Go through the steps of the Euclidean algorithm, but keep track of the recipes for remainders and quotients in terms of original inputs a (196) and b (180).

a

b

c = gcd(a, b)

Step 1

a

196

x1
y0
=
1 ·

b

180

x0
y1
+

r1

16

x1
y-1

Step 2

b

180

x0
y1
=
11 ·

r1

16

x1
y-1
+

r2

4

x-11
y12

Step 3

r1

16

x1
y-1
=
4 ·

r2

4

x-11
y12
+

0

0

x45
y-49
Solution: 4 = 196 * (-11) + 180 * (12)We know how to make 4 from 196 and 180.

General solution

Once one integer solution is known, we can use a trick: add a(−b)+b(a)=0a(-b) + b(a) = 0 to the equation. That shifts the point along the solution line.

Solution line

-250-200-150-100-5050100150200-250-200-150-100-5050100150200250t = -4, (169, -184), 4 = 196 * (169) + 180 * (-184)t = -3, (124, -135), 4 = 196 * (124) + 180 * (-135)t = -2, (79, -86), 4 = 196 * (79) + 180 * (-86)t = -1, (34, -37), 4 = 196 * (34) + 180 * (-37)t = 0, (-11, 12), 4 = 196 * (-11) + 180 * (12)t = 1, (-56, 61), 4 = 196 * (-56) + 180 * (61)t = 2, (-101, 110), 4 = 196 * (-101) + 180 * (110)t = 3, (-146, 159), 4 = 196 * (-146) + 180 * (159)t = 4, (-191, 208), 4 = 196 * (-191) + 180 * (208)196x + 180y = 4Solution t = 04 = 196 * (-11) + 180 * (12)xy
a(−b)+b(a)=0a(-b) + b(a) = 0

Add zero: next solution

ax+by+(a(−b)+b(a))=cax + by + \left(a(-b) + b(a)\right) = c
a(x−b)+b(y+a)=ca(x - b) + b(y + a) = c

Remove zero: previous solution

ax+by−(a(−b)+b(a))=cax + by - \left(a(-b) + b(a)\right) = c
a(x+b)+b(y−a)=ca(x + b) + b(y - a) = c

General solution

x=−11−45t,y=12+49t,t∈Zx = -11 - 45t, \quad y = 12 + 49t, \quad t \in \mathbb{Z}

How to find a 'half' in a modular space? In ordinary arithmetic, "half of 1" sits somewhere between 0 and 1, at 0.5 to be exact. In modular arithmetic the possible residues set includes only whole numbers, nothing in between, so division in usual sense is not working.

Instead, think in multiplicative terms: which value becomes 1 when doubled? That is the modular version of one half.

01234
012343 * 2= 1 (mod 5)

Question: Find the multiplicative inverse of 7 modulo 26

a

modulus b

gcd(a, b)

Rewrite the congruence as a linear equation:

7x≡1(mod26)=>7x=1+26y7x \equiv 1\pmod{26} => 7x = 1 + 26y

Display as a linear Diophantine equation:

7x−26y=17x - 26y = 1

So finding the multiplicative inverse is the same as finding integer coefficients recipes that make up number 1 with the ingredients 7 and 26.

A multiplicative inverse exists only when gcd(a, b) = 1

The equation ax + by = c has an integer solution only when gcd⁡(a,b)∣c\gcd(a, b) \mid c.

g=gcd⁡(a,b),a=a0g,b=b0gg = \gcd(a, b), \quad a = a_0 g, \quad b = b_0 g
ax+by=a0gx+b0gy=(a0x+b0y)gax + by = a_0 g x + b_0 g y = \left(a_0 x + b_0 y\right)g

The left-hand side is always a multiple of g = gcd(a, b), so c must also be a multiple of g. This is the same condition that comes from Bézout's identity.

For multiplicative inverses we solve ax + by = 1. Since the left-hand side is divisible by g = gcd(a, b), the right-hand side must also be divisible by g. The only possibility is g = 1 (since 1 is the only positive integer that divides 1).

Solve the equation 7x - 26y = 1 using the extended Euclidean algorithm. When the final gcd is 1, the coefficient of a is the multiplicative inverse modulo b.

Step 1

b

26

x0
y1
=
3 ·

a

7

x1
y0
+

r1

5

x-3
y1

Step 2

a

7

x1
y0
=
1 ·

r1

5

x-3
y1
+

r2

2

x4
y-1

Step 3

r1

5

x-3
y1
=
2 ·

r2

2

x4
y-1
+

r3

1

x-11
y3

Step 4

r2

2

x4
y-1
=
2 ·

r3

1

x-11
y3
+

0

0

x26
y-7

Solution

Because gcd(7, 26) = 1, the equation has a solution and the x-coefficient gives the inverse.

1=7⋅(−11)+26⋅(3)1 = 7 \cdot (-11) + 26 \cdot (3)
x=−11≡15(mod26)x = -11 \equiv 15 \pmod{26}
7−1(mod26)=157^{-1} \pmod{26} = 15
7⋅15=105≡1(mod26)7 \cdot 15 = 105 \equiv 1 \pmod{26}

Pattern aⁿ-n (mod n)

Residues

Possible residues are {0, 1, 2}.

n (mod 3)n³ (mod 3)
00³ = 0
11³ = 1
22³ = 8 ≡ 2

Whatever the residue of n modulo 3 is, n³ has the same residue. So n³ - n is divisible by 3, equivalently:

n3−n≡0(mod3)n³ - n \equiv 0 \pmod{3}

Residues

Possible residues are {0, 1, 2, 3, 4}.

n (mod 5)n⁵ (mod 5)
00⁵ = 0
11⁵ = 1
22⁵ = 32 ≡ 2
33⁵ = 243 ≡ 3
44⁵ = 1024 ≡ 4

Whatever the residue of n modulo 5 is, n⁵ has the same residue. So n⁵ - n is divisible by 5, equivalently:

n5−n≡0(mod5)n⁵ - n \equiv 0 \pmod{5}

Residues

Possible residues are {0, 1, 2, 3, 4, 5, 6}.

n (mod 7)n⁷ (mod 7)
00⁷ = 0
11⁷ = 1
22⁷ = 2·(2³)² ≡ 2·1² ≡ 2
33⁷ = 3·(3²)³ ≡ 3·2³ ≡ 3·1 ≡ 3
44⁷ = 4·(4²)³ ≡ 4·2³ ≡ 4·1 ≡ 4
55⁷ = 5·(5³)² ≡ 5·6² ≡ 5·1 ≡ 5
66⁷ = 6·(6²)³ ≡ 6·1³ ≡ 6

Whatever the residue of n modulo 7 is, n⁷ has the same residue. So n⁷ - n is divisible by 7, equivalently:

n7−n≡0(mod7)n⁷ - n \equiv 0 \pmod{7}

Residues

Possible residues are {0, 1, 2, 3, 4, 5, 6, 7, 8}.

n (mod 9)n⁹ (mod 9)n⁹ - n
00⁹ = 00
11⁹ = 10
22⁹ = (2³)³ ≡ 8³ ≡ 88 - 2 = 6
33⁹ ≡ 00 - 3 = -3 ≡ 6
44⁹ = (4³)³ ≡ 1³ ≡ 11 - 4 = -3 ≡ 6
55⁹ = (5³)³ ≡ 8³ ≡ 88 - 5 = 3
66⁹ ≡ 00 - 6 = -6 ≡ 3
77⁹ = (7³)³ ≡ 1³ ≡ 11 - 7 = -6 ≡ 3
88⁹ = 8·(8²)⁴ ≡ 8·1⁴ ≡ 88 - 8 = 0

Does not work in general: n⁹ - n is not always divisible by 9, e.g. 2⁹ - 2 = 510 ≡ 6 (mod 9).

Residues

Possible residues are {0, 1, 2, ..., 36}.

n (mod 37)n³⁷ (mod 37)
00³⁷ ≡ 0
11³⁷ ≡ 1
22³⁷ = 137438953472 ≡ 2
33³⁷ = (-34)³⁷ = -34³⁷ = -34 ≡ 3
44³⁷ = (2²)³⁷ = 2³⁷ * 2³⁷ = 2 * 2 = 4
55³⁷ = (-32)³⁷ = -32³⁷ = -32 ≡ 5
66³⁷ = (2·3)³⁷ = 2³⁷ · 3³⁷ = 2 · 3 = 6
77³⁷ = (-30)³⁷ = -30³⁷ = -30 ≡ 7
88³⁷ = (2³)³⁷ = 2³⁷ * 2³⁷ * 2³⁷ = 2 * 2 * 2 = 8
99³⁷ = (3²)³⁷ = 3³⁷ * 3³⁷ = 3 * 3 = 9
1010³⁷ = (2·5)³⁷ = 2³⁷ · 5³⁷ = 2 · 5 = 10
1111³⁷ = (-26)³⁷ = -26³⁷ = -26 ≡ 11
1212³⁷ = (2·2·3)³⁷ = 2³⁷ · 2³⁷ · 3³⁷ = 2 · 2 · 3 = 12
1313³⁷ = (-24)³⁷ = -24³⁷ = -24 ≡ 13
1414³⁷ = (2·7)³⁷ = 2³⁷ · 7³⁷ = 2 · 7 = 14
1515³⁷ = (3·5)³⁷ = 3³⁷ · 5³⁷ = 3 · 5 = 15
1616³⁷ = (2⁴)³⁷ = 2³⁷ * 2³⁷ * 2³⁷ * 2³⁷ = 2 * 2 * 2 * 2 = 16
1717³⁷ = (-20)³⁷ = -20³⁷ = -20 ≡ 17
1818³⁷ = (2·3·3)³⁷ = 2³⁷ · 3³⁷ · 3³⁷ = 2 · 3 · 3 = 18
1919³⁷ = (-18)³⁷ = -18³⁷ = -18 ≡ 19
2020³⁷ = (2·10)³⁷ = 2³⁷ · 10³⁷ = 2 · 10 = 20 ≡ -17
2121³⁷ = (3·7)³⁷ = 3³⁷ · 7³⁷ = 3 · 7 = 21
2222³⁷ = (2·11)³⁷ = 2³⁷ · 11³⁷ = 2 · 11 = 22
2323³⁷ = (-14)³⁷ = -14³⁷ = -14 ≡ 23
2424³⁷ = (2·2·2·3)³⁷ = 2³⁷ · 2³⁷ · 2³⁷ · 3³⁷ = 2 · 2 · 2 · 3 = 24
2525³⁷ = (5²)³⁷ = 5³⁷ * 5³⁷ = 5 * 5 = 25
2626³⁷ = (2·13)³⁷ = 2³⁷ · 13³⁷ = 2 · 13 = 26
2727³⁷ = (3³)³⁷ = 3³⁷ * 3³⁷ * 3³⁷ = 3 * 3 * 3 = 27
2828³⁷ = (4·7)³⁷ = 4³⁷ · 7³⁷ = 4 · 7 = 28
2929³⁷ = (-8)³⁷ = -8³⁷ = -8 ≡ 29
3030³⁷ = (2·10)³⁷ = 2³⁷ · 10³⁷ = 2 · 10 = 30
3131³⁷ = (-6)³⁷ = -6³⁷ = -6 ≡ 31
3232³⁷ = (2⁵)³⁷ = 2³⁷ * 2³⁷ * 2³⁷ * 2³⁷ * 2³⁷ = 2 * 2 * 2 * 2 * 2 = 32
3333³⁷ = (-4)³⁷ = -(2²)³⁷ = -2³⁷ * 2³⁷ = -2 * 2 = -4 ≡ 33
3434³⁷ = (17·2)³⁷ = 17³⁷ · 2³⁷ = 17 · 2 = 34
3535³⁷ = (-2)³⁷ = -2³⁷ = -2 ≡ 35
3636³⁷ = (-1)³⁷= -1 ≡ 36

Whatever the residue of n modulo 37 is, n³⁷ has the same residue. So n³⁷ - n is divisible by 37, equivalently:

n37−n≡0(mod37)n ^{37} - n \equiv 0 \pmod{37}

Fermat's little theorem

Cancellation in modular arithmetic

Let's start with ac≡bc(modm)ac \equiv bc \pmod{m}. It is tempting to cancel the shared factor and jump to a≡b(modm)a \equiv b \pmod{m}, but is this always valid?

A counterexample is 2⋅3≡2⋅6(mod6)2\cdot 3 \equiv 2\cdot 6 \pmod{6}, where 3≢6(mod6)3 \not\equiv 6 \pmod{6}.

In what cases we can be sure that cancellation is valid?

ac≡bc(modm)⇒ac−bc≡0(modm)ac \equiv bc \pmod{m} \Rightarrow ac-bc \equiv 0 \pmod{m}
⇒c(a−b)≡0(modm), here either c≡0 or a−b≡0 or a combination of factors from both a−b and c\Rightarrow c(a-b) \equiv 0 \pmod{m}\text{, here either }c \equiv 0 \text{ or } a-b\equiv 0\text{ or a combination of factors from both } a-b \text{ and } c

To conclude a−b≡0(modm)a-b \equiv 0 \pmod{m}, we need to be sure that cc is not having common factors with mm. That is, gcd⁡(c,m)=1\gcd(c,m)=1.

{ac≡bc(modm)gcd⁡(c,m)=1  ⟹  a≡b(modm)\left\{\begin{array}{l}ac \equiv bc \pmod{m}\\ \gcd(c,m)=1\end{array}\right.\;\Longrightarrow\; a \equiv b \pmod{m}

Pattern aⁿ-n (mod n)

The section above demonstrates that for some numbers, such as m=3,5,7,37m=3,5,7,37, the quantity am−1−1a^{m-1}-1 is always divisible by mm. For other numbers, such as m=9m=9, the pattern does not hold.

Fermat's little theorem proves that for prime moduli pp, the quantity ap−1−1a^{p-1}-1 (or, in the same pattern form, ap−aa^p-a) is always divisible by pp, provided p∤ap \nmid a.

Proof for Fermat's little theorem

If p is prime and p∤a, then ap−1≡1(modp).\text{If } p \text{ is prime and } p \nmid a,\ \text{then } a^{p-1} \equiv 1 \pmod{p}.

Residues

Possible non-zero residues modulo pp are {1,2,…,p−1}\{1,2,\dots,p-1\}.

r(modp)r \pmod{p}a⋅r(modp)a \cdot r \pmod{p}≡r1,r2,…,rp−1(modp)\equiv r_1, r_2, \dots, r_{p-1} \pmod{p}
11aa≡r1\equiv r_1
222a2a≡r2\equiv r_2
333a3a≡r3\equiv r_3
⋮\vdots⋮\vdots⋮\vdots
p−1p-1(p−1)a(p-1)a≡rp−1\equiv r_{p-1}

Setup

The non-zero residues modulo p are 1,2,…,p−1.\text{The non-zero residues modulo } p \text{ are } 1,2,\dots,p-1.
Multiply each of them by a. Let rk≡ka(modp) for k=1,2,…,p−1.\text{Multiply each of them by } a. \text{ Let } r_k \equiv ka \pmod{p} \text{ for } k=1,2,\dots,p-1.

Distinctness

Suppose two of the new residues are equal: rl≡rk(modp) with l≠k.\text{Suppose two of the new residues are equal: } r_l \equiv r_k \pmod{p} \text{ with } l \ne k.
Then la≡ka(modp).\text{Then } la \equiv ka \pmod{p}.
Since p∤a, gcd⁡(a,p)=1, we can cancel a and obtain l≡k(modp).\text{Since } p \nmid a,\ \gcd(a,p)=1,\ \text{we can cancel } a \text{ and obtain } l \equiv k \pmod{p}.
But l,k∈{1,2,…,p−1}, so this implies l=k, a contradiction.\text{But } l,k \in \{1,2,\dots,p-1\},\ \text{so this implies } l=k, \text{ a contradiction.}
Therefore r1,r2,…,rp−1 are all distinct.\text{Therefore } r_1,r_2,\dots,r_{p-1} \text{ are all distinct.}

Non-zero

None of them is 0(modp).\text{None of them is } 0 \pmod{p}.
If rk≡ka≡0(modp), then p∣ka.\text{If } r_k \equiv ka \equiv 0 \pmod{p},\ \text{then } p \mid ka.
This is impossible because p∤a and p∤k for 1≤k≤p−1.\text{This is impossible because } p \nmid a \text{ and } p \nmid k \text{ for } 1 \le k \le p-1.

Rearrangement

Thus r1,r2,…,rp−1 are exactly the numbers 1,2,…,p−1 in some order modulo p.\text{Thus } r_1,r_2,\dots,r_{p-1} \text{ are exactly the numbers } 1,2,\dots,p-1 \text{ in some order modulo } p.

Permutation Intuition

Each arrow shows r -> a*r (mod p). Multiplying every residue by a rearranges the circle.

prime p

multiplier a

Every arrow lands on a different non-zero residue. Changing a rewires the same circle into a new permutation.

Effective Multiplier

3 (since 3 mod 7 = 3)

Behavior

Permutes the non-zero residues

Cycles

(1 3 2 6 4 5)

123456

Multiplying by a permutes the non-zero residues modulo p.

Product

Multiply the congruences ka≡rk(modp) for k=1,2,…,p−1.\text{Multiply the congruences } ka \equiv r_k \pmod{p} \text{ for } k=1,2,\dots,p-1.
The left side becomes (1a)(2a)⋯((p−1)a)=ap−1(1⋅2⋯(p−1)).\text{The left side becomes } (1a)(2a)\cdots((p-1)a) = a^{p-1}(1\cdot2\cdots(p-1)).
So ap−1(1⋅2⋯(p−1))≡r1r2⋯rp−1(modp).\text{So } a^{p-1}(1\cdot2\cdots(p-1)) \equiv r_1 r_2 \cdots r_{p-1} \pmod{p}.
Because r1,r2,…,rp−1 is the same residue set as 1,2,…,p−1, when multiplied together it is the same as r1r2⋯rp−1≡1⋅2⋯(p−1)(modp).\text{Because } r_1,r_2,\dots,r_{p-1} \text{ is the same residue set as } 1,2,\dots,p-1,\ \text{when multiplied together it is the same as } r_1 r_2 \cdots r_{p-1} \equiv 1\cdot2\cdots(p-1) \pmod{p}.
Rewriting 1⋅2⋯(p−1) as factorial(p−1)!, we get ap−1(p−1)!≡(p−1)!(modp).\text{Rewriting } 1\cdot2\cdots(p-1) \text{ as factorial} (p-1)!,\ \text{we get } a^{p-1}(p-1)! \equiv (p-1)! \pmod{p}.
ap−1(p−1)!≡(p−1)!(modp)a^{p-1}(p-1)! \equiv (p-1)! \pmod{p}
gcd⁡((p−1)!,p)=1 because p is prime and none of 1,2,…,p−1 is divisible by p, so we can cancel (p−1)! from both sides\gcd((p-1)!,p)=1 \text{ because } p \text{ is prime and none of } 1,2,\dots,p-1 \text{ is divisible by } p \text{, so we can cancel } (p-1)! \text{ from both sides}
∴ap−1≡1(modp)\therefore a^{p-1} \equiv 1 \pmod{p}
Multiplying both sides by a gives another form: ap−a≡0(modp) or ap≡a(modp)\text{Multiplying both sides by } a \text{ gives another form: } a^p-a \equiv 0 \pmod{p} \text{ or } a^p \equiv a \pmod{p}

Euler's theorem

Generalizing Fermat's little theorem

Euler extended the idea from the Fermat's little theorem from primes (mod p) to modulus (mod n), using the totient function φ(n)\varphi(n).

The theorem says that if gcd⁡(a,n)=1\gcd(a,n)=1, then powers of aa cycle in a way controlled by Euler's totient function φ(n)\varphi(n):

aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod{n}

Totient Function

Totient function φ(n)\varphi(n) counts the numbers from 11 up to n−1n-1 that are coprime to nn.

n

Prime Factors of n

Totient

Totient Filter

Integers 1 to n-1

φ1

1

1

2

2

3

3

4

4

φ2

5

5

6

6

φ3

7

7

8

8

9

9

10

10

φ4

11

11

Euler Product

Each distinct prime divisor removes a fraction of the residue classes, leaving exactly φ(n)\varphi(n) survivors.

These surviving residues are the ones Euler's theorem multiplies and permutes, just like Fermat permutes the full non-zero set when the modulus is prime.

φ(12)=12\varphi(12) = 12(1−12)\left(1-\frac{1}{\textcolor{deeppink}{2}}\right)(1−13)\left(1-\frac{1}{\textcolor{firebrick}{3}}\right)=4= 4

Proof for Euler's theorem

If gcd⁡(a,n)=1, then aφ(n)≡1(modn).\text{If } \gcd(a,n)=1,\ \text{then } a^{\varphi(n)} \equiv 1 \pmod{n}.

Possible residues

The possible residues set is the reduced residue system modulo nn:

{α1,α2,…,αφ(n)}, where gcd⁡(αi,n)=1 for every i\{\alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)}\},\ \text{where } \gcd(\alpha_i,n)=1 \text{ for every } i
αi\alpha_ia⋅αia \cdot \alpha_i≡ri(modn)\equiv r_i \pmod{n}
α1\alpha_1aα1a\alpha_1≡r1\equiv r_1
α2\alpha_2aα2a\alpha_2≡r2\equiv r_2
α3\alpha_3aα3a\alpha_3≡r3\equiv r_3
⋮\vdots⋮\vdots⋮\vdots
αφ(n)\alpha_{\varphi(n)}aαφ(n)a\alpha_{\varphi(n)}≡rφ(n)\equiv r_{\varphi(n)}

Setup

Let {α1,α2,…,αφ(n)} be the reduced residue system modulo n.\text{Let } \{\alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)}\} \text{ be the reduced residue system modulo } n.
Each αi satisfies 1≤αi<n and gcd⁡(αi,n)=1.\text{Each } \alpha_i \text{ satisfies } 1 \le \alpha_i < n \text{ and } \gcd(\alpha_i,n)=1.
Multiply each of them by a. Let ri≡aαi(modn) for i=1,2,…,φ(n).\text{Multiply each of them by } a. \text{ Let } r_i \equiv a\alpha_i \pmod{n} \text{ for } i=1,2,\dots,\varphi(n).

Distinctness

Suppose two new residues are equal: ri≡rj(modn) with i≠j.\text{Suppose two new residues are equal: } r_i \equiv r_j \pmod{n} \text{ with } i \ne j.
Then aαi≡aαj(modn).\text{Then } a\alpha_i \equiv a\alpha_j \pmod{n}.
Since gcd⁡(a,n)=1, we may cancel a and obtain αi≡αj(modn).\text{Since } \gcd(a,n)=1,\ \text{we may cancel } a \text{ and obtain } \alpha_i \equiv \alpha_j \pmod{n}.
But α1,α2,…,αφ(n) are distinct residues modulo n, so this is impossible.\text{But } \alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)} \text{ are distinct residues modulo } n, \text{ so this is impossible.}
Therefore r1,r2,…,rφ(n) are all different.\text{Therefore } r_1,r_2,\dots,r_{\varphi(n)} \text{ are all different.}

Still Coprime

Each ri is also coprime to n.\text{Each } r_i \text{ is also coprime to } n.
gcd⁡(ri,n)=gcd⁡(aαi,n)=1 because gcd⁡(a,n)=1 and gcd⁡(αi,n)=1.\gcd(r_i,n)=\gcd(a\alpha_i,n)=1 \text{ because } \gcd(a,n)=1 \text{ and } \gcd(\alpha_i,n)=1.
So every ri is another residue from the same reduced residue system modulo n.\text{So every } r_i \text{ is another residue from the same reduced residue system modulo } n.

Rearrangement

There are φ(n) of these residues, they are all different, and each is coprime to n.\text{There are } \varphi(n) \text{ of these residues, they are all different, and each is coprime to } n.
Hence r1,r2,…,rφ(n) are exactly α1,α2,…,αφ(n) in some order modulo n.\text{Hence } r_1,r_2,\dots,r_{\varphi(n)} \text{ are exactly } \alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)} \text{ in some order modulo } n.

Permutation Intuition

Each arrow shows r -> a*r (mod n), but only on residues coprime to n. Grey nodes are filtered out by Euler's totient.

modulus n

multiplier a

Effective Multiplier

5 (since 5 mod 12 = 5)

Behavior

Permutes the reduced residues

Reduced Set

4 residues, cycles (1 5) (7 11)

1234567891011

Multiplying by a permutes only the residues coprime to n; the grey residues are excluded from Euler's theorem.

Product

Multiply the congruences aαi≡ri(modn) for i=1,2,…,φ(n).\text{Multiply the congruences } a\alpha_i \equiv r_i \pmod{n} \text{ for } i=1,2,\dots,\varphi(n).
The left side becomes (aα1)(aα2)⋯(aαφ(n))=aφ(n)α1α2⋯αφ(n).\text{The left side becomes } (a\alpha_1)(a\alpha_2)\cdots(a\alpha_{\varphi(n)}) = a^{\varphi(n)}\alpha_1\alpha_2\cdots\alpha_{\varphi(n)}.
Thus aφ(n)α1α2⋯αφ(n)≡r1r2⋯rφ(n)(modn).\text{Thus } a^{\varphi(n)}\alpha_1\alpha_2\cdots\alpha_{\varphi(n)} \equiv r_1r_2\cdots r_{\varphi(n)} \pmod{n}.
Because r1,r2,…,rφ(n) is the same residue set as α1,α2,…,αφ(n),\text{Because } r_1,r_2,\dots,r_{\varphi(n)} \text{ is the same residue set as } \alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)},
r1r2⋯rφ(n)≡α1α2⋯αφ(n)(modn).r_1r_2\cdots r_{\varphi(n)} \equiv \alpha_1\alpha_2\cdots\alpha_{\varphi(n)} \pmod{n}.
aφ(n)α1α2⋯αφ(n)≡α1α2⋯αφ(n)(modn)a^{\varphi(n)}\alpha_1\alpha_2\cdots\alpha_{\varphi(n)} \equiv \alpha_1\alpha_2\cdots\alpha_{\varphi(n)} \pmod{n}
gcd⁡(α1α2⋯αφ(n),n)=1 because every αi is coprime to n, so we can cancel the product\gcd(\alpha_1\alpha_2\cdots\alpha_{\varphi(n)},n)=1 \text{ because every } \alpha_i \text{ is coprime to } n \text{, so we can cancel the product}
∴aφ(n)≡1(modn)\therefore a^{\varphi(n)} \equiv 1 \pmod{n}
When n=p is prime, the reduced residue system is 1,2,…,p−1, so this becomes Fermat’s little theorem.\text{When } n=p \text{ is prime, the reduced residue system is } 1,2,\dots,p-1 \text{, so this becomes Fermat's little theorem.}

Cryptography

With the number theory in place, the algorithms it was built for become readable - key exchange, public-key encryption, signatures, symmetric ciphers, and hash functions.