ata# DM 08 Number theory and modular arithmetic

Previous: DM 07 Counting | DM 00 Course Map | Next: DM 09 Recurrences and Asymptotics

One sentence

Number theory studies how integers divide one another, and modular arithmetic, the arithmetic of remainders, turns that study into a number system where cryptography and hashing live.

Divisibility

For integers and , divides , written , when for some integer .

, , and for every .

Basic facts, each a one-line proof from the definition:

  • If and then .
  • If and then for any integers .

The second says a common divisor of two numbers divides every integer combination of them. It powers everything below.

Division with remainder

Division algorithm. For any integer and positive integer , there are unique integers (quotient) and (remainder) with

, so . For negatives, the remainder is still non-negative: , so .

Negative remainders in code

Python’s % follows the mathematical convention (-7 % 5 == 3). C, C++ and Java return -2. Know which your language does.

Primes

A prime is an integer greater than 1 whose only positive divisors are 1 and itself: Other integers greater than 1 are composite.

Fundamental theorem of arithmetic. Every integer greater than 1 is a product of primes in exactly one way, apart from the order of the factors.

Existence was proved by strong induction in DM 06 Induction and Recursion. Primes are the atoms of multiplication. There are infinitely many of them (DM 03 Proof Techniques).

Testing primality by hand. If is composite it has a factor at most , so check divisibility by primes up to . Is 97 prime? ; it is not divisible by 2, 3, 5 or 7. Yes.

Greatest common divisor

is the largest integer dividing both. . If the numbers are coprime: they share no prime factor.

The least common multiple satisfies for positive integers. .

The Euclidean algorithm

Factoring large numbers is hard, but the gcd can be found quickly without factoring.

Key fact. .

Why. Write . Any common divisor of and divides . Any common divisor of and divides . So the pairs and have exactly the same common divisors.

Repeat until the remainder is 0. The last nonzero remainder is the gcd.

Worked example. :

So .

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

The remainder at least halves every two steps, so the number of steps is proportional to the number of digits. It is one of the oldest algorithms still in daily use.

Bézout’s identity

For any integers there exist integers with

Find them by running the Euclidean algorithm backwards (the extended Euclidean algorithm). From the example:

Check: .

Consequence (Euclid’s lemma): if a prime divides , then or . This is what makes prime factorization unique.

Modular arithmetic

and are congruent modulo when they leave the same remainder on division by , equivalently when :

. . : 38 hours after 12 o’clock the clock shows 2.

Congruence mod is an equivalence relation (DM 05 Functions and Relations) with classes, represented by the remainders .

Arithmetic respects congruence. If and then

So you may reduce at any stage of a calculation. Numbers never need to get large.

Worked example. Last digit of , that is . Powers of 7 mod 10: , repeating with period 4. Since , . The last digit is 1.

Worked example. Is 123456 divisible by 9? Since , every power of 10 is , so a number is congruent to its digit sum mod 9. . Not divisible; the remainder is 3. This is the old trick of “casting out nines”.

Division: modular inverses

Addition, subtraction and multiplication work freely. Division does not: , but . You cannot cancel the 2.

An inverse of modulo is a number with .

When inverses exist

has an inverse modulo exactly when .

Why. By Bézout, gives , so and is the inverse. The extended Euclidean algorithm computes it.

Example. Inverse of 3 mod 7: , so . To solve , multiply by 5: . Check: .

If is prime, every nonzero residue has an inverse. The integers mod then form a field: a number system where all four operations work, like the rationals or the reals, but finite.

Fast exponentiation

To compute for huge , use repeated squaring. Example: , with :

About squarings instead of multiplications. In Python: pow(3, 13, 7).

Fermat’s little theorem

If is prime and :

Check: . A consequence is that is the inverse of mod , and exponents can be reduced mod : , matching the calculation above.

Why this matters: public-key cryptography

RSA in outline:

  1. Pick two large primes . Publish . Keep and secret.
  2. Pick a public exponent coprime to . Compute the private as the inverse of modulo .
  3. Encrypt a message as . Decrypt as .

Every tool in this chapter appears: primes, the extended Euclidean algorithm for , fast exponentiation, and a generalization of Fermat’s theorem to show decryption undoes encryption. Security rests on an asymmetry: multiplying two primes is easy, while factoring their product is believed to be infeasible at sufficient size.

Common mistakes

  • Dividing in a congruence without checking that the divisor is coprime to the modulus.
  • Forgetting that remainders are non-negative in the mathematical convention.
  • Confusing (a number) with (a relationship).
  • Calling 1 a prime. It is excluded so that factorization is unique.

Learn by doing

  1. Find with the Euclidean algorithm, and integers with .
  2. Compute and .
  3. Find the inverse of 5 modulo 12, and solve .
  4. Does 4 have an inverse modulo 10? Why?
  5. What is the last digit of ?

Where this shows up

Hash tables, checksums, random number generators and all of public-key cryptography. Angles wrap around modulo , which is modular arithmetic on the reals: a joint at and one at are apart, a detail that breaks naive angle losses in robot learning (02 Geometry and Kinematics).