Skip to content
Handbook · Chapter 2

Number Theory

Primes and divisors, GCD and LCM, modular arithmetic with Euler, Fermat and the Chinese Remainder Theorem, Diophantine equations, bases and p-adic valuation.

Book pages 33–45 · 44 entries

2.1 Primes

open on YouTube
open on YouTube

Remark 2.1.2

To check whether a number nn is prime, you only need to test the primes that are less than or equal to n\sqrt{n}.

Learn it:Remark 21.0.2

Book p. 33Checked numerically

Concept 2.1.3 · Prime Factorization

Prime factorization writes a number as a product of primes.

Examples: the prime factorization of 2121 is 3×73 \times 7, and the prime factorization of 6060 is 22×3×52^2 \times 3 \times 5.

Learn it:Concept 21.0.3 · Prime Factorization

Book p. 33Checked numerically

Theorem 2.1.4 · Number of Factors of a Number

If the prime factorization of a number is

p1e1×p2e2×⋯×pkek,p_1^{e_1} \times p_2^{e_2} \times \cdots \times p_k^{e_k},

then the number of factors of this number is

(e1+1)(e2+1)⋯(ek+1).(e_1 + 1)(e_2 + 1)\cdots(e_k + 1).

Learn it:Theorem 21.0.4 · Number of Factors of a Number

Book p. 34Checked numerically

Remark 2.1.5

To find the number of factors of a number:

  1. Find the prime factorization of the number.
  2. Add 11 to each of the exponents.
  3. Multiply them together.

Learn it:Remark 21.0.5

Book p. 34

Concept 2.1.6 · Divisibility Rules

Divisor Rule
22 Last digit is even
33 Sum of digits is divisible by 33
44 Last 22 digits divisible by 44
55 Last digit is 00 or 55
66 Divisible by 22 and 33
77 Take out multiples of 77 until you reach a small number that is either divisible or not divisible by 77
88 Last 33 digits are divisible by 88
99 Sum of digits is divisible by 99
1010 Last digit is 00
1111 Add the digits in the odd positions (OO) and the digits in the even positions (EE). If ∣O−E∣\lvert O - E \rvert is divisible by 1111, then the number is also divisible by 1111
1212 Divisible by 33 and 44
1515 Divisible by 33 and 55

Learn it:Concept 22.0.1 · Divisibility RulesSection 22.0a Divisibility Rules

Book p. 34Checked numerically

2.2 Integer Sequences

open on YouTube

2.3 Palindromes

open on YouTube

2.4 GCD/LCM

open on YouTube
open on YouTube
open on YouTube
open on YouTube

Definition 2.4.2 · Least Common Multiple

The Least Common Multiple (LCM) of two or more nonzero integers is the smallest positive integer that is divisible by each of them.

Correction

The book says "two or more integers (which are not all zero)". If any one of them is 00, no positive integer is divisible by it, so the LCM needs every integer nonzero.

Learn it:Definition 23.0.2 · Least Common MultipleSection 23.0a GCD and LCM from Prime Factors

Book p. 35

Concept 2.4.3

gcd⁡(m,n)\gcd(m, n) can be found by taking the lowest prime exponents from the prime factorizations of mm and nn.

lcm⁡(m,n)\operatorname{lcm}(m, n) can be found by taking the highest prime exponents from the prime factorizations of mm and nn.

Learn it:Concept 23.0.3 · GCD and LCM from prime factorizations

Book p. 36Checked numerically

Theorem 2.4.4

The product of the GCD and LCM of two numbers is equal to the product of the two numbers:

gcd⁡(m,n)⋅lcm⁡(m,n)=m⋅n.\gcd(m, n) \cdot \operatorname{lcm}(m, n) = m \cdot n.

Learn it:Theorem 23.0.4 · GCD × LCM

Book p. 36Checked numerically

Theorem 2.4.5

If two numbers have a common factor cc, then

gcd⁡(ac,bc)=c⋅gcd⁡(a,b).\gcd(ac, bc) = c \cdot \gcd(a, b).

Learn it:Theorem 23.0.5 · Pulling out a common factor

Book p. 36Checked numerically

Remark 2.4.7

We can apply the Euclidean algorithm again and again to find the GCD of large numbers: each step leaves two smaller numbers, until they are small enough to read off the GCD. For example,

gcd⁡(186,92)=gcd⁡(186−2⋅92, 92)=gcd⁡(2,92)=gcd⁡(2, 92−2⋅46)=gcd⁡(2,0)=2.\begin{aligned}&\gcd(186, 92) \\ &= \gcd(186 - 2 \cdot 92,\ 92) \\ &= \gcd(2, 92) \\ &= \gcd(2,\ 92 - 2 \cdot 46) \\ &= \gcd(2, 0) = 2.\end{aligned}

Learn it:Remark 23.0.7

Book p. 36Checked numerically

2.5 Modular Arithmetic

open on YouTube
open on YouTube
open on YouTube
open on YouTube
open on YouTube

Theorem 2.5.2

If a≡x(modn)a \equiv x \pmod{n} and b≡y(modn)b \equiv y \pmod{n}, then

ab≡xy(modn).ab \equiv xy \pmod{n}.

Learn it:Theorem 24.0.2 · Multiplying Congruences

Book p. 38Checked numerically

Theorem 2.5.3

If a≡x(modn)a \equiv x \pmod{n}, then

am≡xm(modn)a^m \equiv x^m \pmod{n}

for every positive integer mm.

Learn it:Theorem 24.0.3 · Powers of Congruences

Book p. 38Checked numerically

Theorem 2.5.4 · Euler’s Totient Function

If nn has the prime factorization

p1e1⋅p2e2⋅p3e3⋯pkek,p_1^{e_1} \cdot p_2^{e_2} \cdot p_3^{e_3} \cdots p_k^{e_k},

then

φ(n)=n(1−1p1)(1−1p2)⋯(1−1pk),\begin{aligned}\varphi(n) = n &\left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right) \\ &\cdots\left(1 - \frac{1}{p_k}\right),\end{aligned}

where φ(n)\varphi(n) denotes the number of positive integers less than or equal to nn that are relatively prime to nn.

Steps to find the totient of a number:

  1. Find the prime factorization.
  2. For each prime, calculate 1−1pi1 - \frac{1}{p_i} and multiply these together.
  3. Multiply this product by the number nn to get the totient.

Learn it:Theorem 24.0.5 · Euler's Totient FunctionSection 24.0b Euler, Fermat, Wilson and the Binomial Theorem

Book p. 38Checked numerically

Theorem 2.5.5 · Euler’s Totient Theorem

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

if and only if

gcd⁡(a,n)=1.\gcd(a, n) = 1.

Learn it:Theorem 24.0.6 · Euler's Totient Theorem

Book p. 38Checked numerically

Corollary 2.5.6 · Fermat’s Little Theorem

If pp is a prime and gcd⁡(a,p)=1\gcd(a, p) = 1 (that is, p∤ap \nmid a), then

ap−1≡1(modp).a^{p-1} \equiv 1 \pmod{p}.

For a prime pp the converse holds too: ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p only when p∤ap \nmid a. Note that this follows directly from Euler’s Totient Theorem, since φ(p)=p−1\varphi(p) = p - 1.

Correction

The book says ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p "if and only if pp is a prime and gcd⁡(a,n)=1\gcd(a, n) = 1". The nn should be pp, and pp being prime is not forced: 2340≡1(mod341)2^{340} \equiv 1 \pmod{341} although 341=11⋅31341 = 11 \cdot 31. The theorem is the one-way statement for a prime pp.

Learn it:Corollary 24.0.7 · Fermat's Little Theorem

Book p. 39Checked numerically

Fact 2.5.7

For any integer xx,

x2≡0,1(mod3)x^2 \equiv 0, 1 \pmod{3}
x2≡0,1(mod4)x^2 \equiv 0, 1 \pmod{4}

Learn it:Section 26.0 Strategies for Integer Equations

Book p. 39Checked numerically

Concept 2.5.8

If aa is the modular inverse of b(modn)b \pmod{n}, then

ab≡1(modn).ab \equiv 1 \pmod{n}.

We also write

a−1≡b(modn)a^{-1} \equiv b \pmod{n}

since aa and bb are inverses (modn)\pmod{n}.

Learn it:Concept 24.0.8 · Modular Inverses

Book p. 39Checked numerically

Theorem 2.5.9 · Wilson’s Theorem

For a prime pp,

(p−1)!≡p−1≡−1(modp).(p-1)! \equiv p - 1 \equiv -1 \pmod{p}.
Correction

The book prints the congruence without saying that pp must be prime. It fails for composite pp: (4−1)!=6≡2(mod4)(4-1)! = 6 \equiv 2 \pmod 4.

Learn it:Theorem 24.0.9 · Wilson's Theorem

Book p. 39Checked numerically

Theorem 2.5.10 · Chinese Remainder Theorem

If a positive number xx satisfies

x≡a1(modn1)x \equiv a_1 \pmod{n_1}
x≡a2(modn2)x \equiv a_2 \pmod{n_2}
⋮\vdots
x≡ak(modnk)x \equiv a_k \pmod{n_k}

where the nin_i are pairwise relatively prime, then xx has a unique solution (modn1⋅n2⋅n3⋯nk)\pmod{n_1 \cdot n_2 \cdot n_3 \cdots n_k}.

Learn it:Theorem 24.0.11 · Chinese Remainder TheoremSection 24.0c CRT and Linear Congruences

Book p. 40Checked numerically

Remark 2.5.11

Be careful! This may not be true if some of the nin_i share common factors, as then the congruences might contradict each other.

Learn it:Remark 24.0.12

Book p. 40

Concept 2.5.12 · Solving Linear Congruences

To solve a system of 2 linear congruences you can either

  • Guess and check until you reach a value that satisfies both congruences, or
  • use the algebraic method:
    1. Start with the 2 congruences n≡r1(modm1)n \equiv r_1 \pmod{m_1} and n≡r2(modm2)n \equiv r_2 \pmod{m_2}, where m1m_1 and m2m_2 are relatively prime.
    2. Rewrite them algebraically: n=km1+r1n = k m_1 + r_1 and n=jm2+r2n = j m_2 + r_2.
    3. Set them equal mod m2m_2: km1+r1≡r2(modm2)k m_1 + r_1 \equiv r_2 \pmod{m_2}, so k≡(r2−r1)⋅m1−1(modm2)k \equiv (r_2 - r_1) \cdot m_1^{-1} \pmod{m_2}.
    4. Guess and check to find the value of m1−1(modm2)m_1^{-1} \pmod{m_2}.
    5. Using the value k≡k0(modm2)k \equiv k_0 \pmod{m_2}, rewrite it algebraically: k=m2t+k0k = m_2 t + k_0.
    6. Substitute it back into the expression n=km1+r1n = k m_1 + r_1.
    7. Convert back to mods to get the final congruence n≡k0m1+r1(modm1m2)n \equiv k_0 m_1 + r_1 \pmod{m_1 m_2}.
Correction

The book's step 3 reads m1≡(r2−r1)⋅k−1(modm2)m_1 \equiv (r_2 - r_1) \cdot k^{-1} \pmod{m_2} and step 4 asks for k−1k^{-1}; solving km1+r1≡r2k m_1 + r_1 \equiv r_2 for kk gives k≡(r2−r1)⋅m1−1k \equiv (r_2 - r_1) \cdot m_1^{-1}. Steps 1 and 5 also use undefined letters bb, dd (meaning m1m_1, m2m_2 and kk mod m2m_2).

Learn it:Concept 24.0.13 · Solving Linear Congruences

Book p. 40Checked numerically

Concept 2.5.13

The solution to

n≡r1(modm1)n \equiv r_1 \pmod{m_1}
n≡r2(modm2)n \equiv r_2 \pmod{m_2}

with m1,m2m_1, m_2 relatively prime is

n≡r1+m1(r2−r1)⋅i(modm1m2)\begin{aligned}n \equiv{} &r_1 + m_1 (r_2 - r_1) \cdot i \\ &\pmod{m_1 m_2}\end{aligned}

where i≡m1−1(modm2)i \equiv m_1^{-1} \pmod{m_2}.

Correction

The book prints n≡r1+m1(r2−r1)⋅in \equiv r_1 + m_1 (r_2 - r_1) \cdot i without a modulus; the solution is unique mod m1m2m_1 m_2 (and needs m1m_1, m2m_2 relatively prime so that m1−1m_1^{-1} exists).

Learn it:Concept 24.0.14 · The Two-Congruence Formula

Book p. 41Checked numerically

Remark 2.5.14

To solve a system of more than 2 congruences, just solve them 2 at a time until you are left with just 1 congruence.

Learn it:Remark 24.0.15

Book p. 41

2.6 Diophantine Equations

open on YouTube

Concept 2.6.2 · Strategies to Solve Diophantine Equations

Here are some ideas on how to solve Diophantine equations.

  • Take mods of different numbers. This is generally useful when you want to
    1. show there are no solutions to a Diophantine equation, or
    2. show that there is only a specific type of solution.
  • Bound the possible values of different terms. This is generally useful when there are finitely many solutions.
  • Factor, using the various factorizations from the algebra chapter, to find all the solutions.
  • Substitute to simplify your Diophantine equation.
  • Look for conditions on what must be multiples or divisors of your variables, and rewrite your Diophantine equation in terms of that.

Learn it:Concept 26.0.2 · Strategies to Solve Diophantine Equations

Book p. 42

2.7 Bases

open on YouTube
open on YouTube

Definition 2.7.1 · Bases

A number expressed in base-nn is similar to base 1010, except that instead of regrouping to a new place value every 1010, we regroup every nn.

A number in base nn with digits am,am−1,…,a2,a1,a0a_m, a_{m-1}, \ldots, a_2, a_1, a_0 can be expressed in the form

a0⋅n0+a1⋅n1+a2⋅n2+⋯+am−1⋅nm−1+am⋅nm\begin{aligned}&a_0 \cdot n^0 + a_1 \cdot n^1 + a_2 \cdot n^2 + \cdots \\ &+ a_{m-1} \cdot n^{m-1} + a_m \cdot n^m\end{aligned}

where all aia_i are digits of the number.

Learn it:Definition 27.0.1 · BasesConcept 27.0.2 · Place Value in Base $n$Section 27.0 Place Value in Any Base

Book p. 42Checked numerically

Theorem 2.7.2 · Chicken McNugget Theorem

The maximum value that cannot be expressed as a sum of non-negative multiples of aa and bb is

ab−a−bab - a - b

if aa and bb are relatively prime.

For relatively prime positive integers a,ba, b, there are exactly

(a−1)(b−1)2\frac{(a-1)(b-1)}{2}

positive integers which cannot be expressed in the form ma+nbma + nb, where mm and nn are non-negative integers.

Correction

The book's second sentence says "where mm and nn are positive integers"; the count (a−1)(b−1)2\frac{(a-1)(b-1)}{2} is for non-negative m,nm, n. (With a=3a = 3, b=5b = 5: 1,2,4,71, 2, 4, 7 are the 44 unreachable numbers, but with positive m,nm, n the numbers 3,5,6,9,103, 5, 6, 9, 10 would be unreachable too.)

Learn it:Theorem 28.2.1 · Chicken McNugget TheoremSection 28.2 Chicken McNugget Theorem

Book p. 43Checked numerically

Remark 2.7.3

This theorem is useful in finding solutions to problems like "the maximum amount of money that can't be created with 33-cent and 55-cent coins".

Learn it:Remark 28.2.2

Book p. 43

2.8 P-adic Evaluation

Definition 2.8.1

vpv_p notation. vp(n)v_p(n) is defined as the exponent of the prime pp in the prime factorization of nn.

For example, v5(75)=2v_5(75) = 2 since 75=3⋅5275 = 3 \cdot 5^2 has 22 factors of 55, and v2(27)=0v_2(27) = 0 since 2727 is odd and has no factors of 22.

Learn it:Section 22.0b Legendre's Formula

Book p. 43Checked numerically

Theorem 2.8.2 · Vp Exponentiation Formula

vp(nk)=k⋅vp(n)v_p(n^k) = k \cdot v_p(n)

This basically means the power of pp in a number nkn^k is kk times the power of pp in nn.

Learn it:Section 22.0b Legendre's Formula

Book p. 43Checked numerically

Theorem 2.8.3 · Vp Product Formula

vp(ab)=vp(a)+vp(b)v_p(ab) = v_p(a) + v_p(b)

This basically means: exponent of pp in abab = exponent of pp in aa + exponent of pp in bb.

Learn it:Section 22.0b Legendre's Formula

Book p. 44Checked numerically

Theorem 2.8.4 · Vp Division Formula

vp(ab)=vp(a)−vp(b)v_p\left(\frac{a}{b}\right) = v_p(a) - v_p(b)

This basically means: exponent of pp in ab\frac{a}{b} = exponent of pp in aa − exponent of pp in bb.

Learn it:Section 22.0b Legendre's Formula

Book p. 44Checked numerically

Theorem 2.8.5 · Vp Sum Formula

If vp(a)≠vp(b)v_p(a) \ne v_p(b), then

vp(a+b)=min⁡(vp(a),vp(b)).v_p(a + b) = \min(v_p(a), v_p(b)).

If vp(a)=vp(b)v_p(a) = v_p(b), then

vp(a+b)≥min⁡(vp(a),vp(b)).v_p(a + b) \ge \min(v_p(a), v_p(b)).

Book p. 44Checked numerically

Theorem 2.8.6 · Lifting the exponent (LTE) for odd primes

For an odd prime pp: if

a≡b≢0(modp),a \equiv b \not\equiv 0 \pmod{p},

then

vp(an−bn)=vp(a−b)+vp(n).v_p(a^n - b^n) = v_p(a - b) + v_p(n).

This basically means: exponent of pp in an−bna^n - b^n = (exponent of pp in a−ba - b) + (exponent of pp in nn), if aa and bb leave the same remainder when divided by pp, and that remainder is not 00.

Book p. 44Checked numerically

Remark 2.8.7

Be very careful of the condition a≡b≢0(modp)a \equiv b \not\equiv 0 \pmod p! Also remember that p=2p = 2 doesn't work in this equation! Sometimes, when we can't directly apply LTE to our exponent, we can modify it by rewriting the exponent terms.

Book p. 45Checked numerically

Theorem 2.8.8 · Lifting the exponent (LTE) for 2

For odd integers a,ba, b and an even positive integer nn,

v2(an−bn)=v2(a2−b2)+v2(n)−1.\begin{aligned}&v_2(a^n - b^n) \\ &= v_2(a^2 - b^2) + v_2(n) - 1.\end{aligned}

This basically means: exponent of 22 in an−bna^n - b^n = (exponent of 22 in a2−b2a^2 - b^2) + (exponent of 22 in nn) − 11.

(For odd nn instead, v2(an−bn)=v2(a−b)v_2(a^n - b^n) = v_2(a - b).)

Correction

The book states the formula with no conditions. It needs aa and bb odd and nn even: for a=3a = 3, b=1b = 1, n=1n = 1 it would give v2(2)=v2(8)+0−1=2v_2(2) = v_2(8) + 0 - 1 = 2, but v2(2)=1v_2(2) = 1. The book's gloss also says "exponent of pp in nn" for "exponent of 22 in nn".

Book p. 45Checked numerically

Theorem 2.8.9 · Legendre’s Theorem

vp(n!)=⌊np⌋+⌊np2⌋+⋯v_p(n!) = \left\lfloor \frac{n}{p} \right\rfloor + \left\lfloor \frac{n}{p^2} \right\rfloor + \cdots

This basically means: the number of factors of pp in n!n! = the number of multiples of pp that are ≤n\le n + the number of multiples of p2p^2 that are ≤n\le n + ⋯\cdots

Learn it:Theorem 22.0.2 · Legendre's TheoremSection 22.0b Legendre's Formula

Book p. 45Checked numerically