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}.
Modular Operations
Modular Arithmetic Examples
Calculation examples
Click on an example to expand it and see the steps.
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
Rule for division by: none
Click a value on the line to see its divisibility rule.
Primes
Numbers decomposition
Prime numbers - the building blocks that combine to build all other numbers.
= 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 =
Greatest common divisor
Divisibility
Intuition: "3 divides 12" means 12 = 3 * 4, or easier to think 12 is a multiple of 3.
Divisibility Definition
Number a divides b if you can build b using all blocks from a.
For example, 12 divides 60:
60 =
GCD Definition
A
B
LCM Definition
LCM includes all blocks from a, and all blocks from b, repeating blocks can be shared between a and b.
A
B
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
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.
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.
Pattern aⁿ-n (mod n)
Fermat's little theorem
Cancellation in modular arithmetic
Let's start with . It is tempting to cancel the shared factor and jump to , but is this always valid?
A counterexample is , where .
In what cases we can be sure that cancellation is valid?
To conclude , we need to be sure that is not having common factors with . That is, .
Pattern aⁿ-n (mod n)
The section above demonstrates that for some numbers, such as , the quantity is always divisible by . For other numbers, such as , the pattern does not hold.
Fermat's little theorem proves that for prime moduli , the quantity (or, in the same pattern form, ) is always divisible by , provided .
Proof for Fermat's little theorem
Residues
Possible non-zero residues modulo are .
Setup
Distinctness
Non-zero
Rearrangement
Permutation Intuition
Each arrow shows r -> a*r (mod p). Multiplying every residue by a rearranges the circle.
prime p
multiplier a
Effective Multiplier
3 (since 3 mod 7 = 3)
Behavior
Permutes the non-zero residues
Cycles
(1 3 2 6 4 5)
Multiplying by a permutes the non-zero residues modulo p.
Product
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 .
The theorem says that if , then powers of cycle in a way controlled by Euler's totient function :
Totient Function
Totient function counts the numbers from up to that are coprime to .
n
Prime Factors of n
Totient
Totient Filter
Integers 1 to n-1
φ1
1
2
3
4
φ2
5
6
φ3
7
8
9
10
φ4
11
Euler Product
Each distinct prime divisor removes a fraction of the residue classes, leaving exactly 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.
Proof for Euler's theorem
Possible residues
The possible residues set is the reduced residue system modulo :
Setup
Distinctness
Still Coprime
Rearrangement
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)
Multiplying by a permutes only the residues coprime to n; the grey residues are excluded from Euler's theorem.
Product
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.