Primes and Factorization

Study Sheet

Primes and Factorization

The atoms of arithmetic and the unique way numbers split into them

Primes

Concept
Definition and infinitude

A prime is an integer p>1p > 1 whose only positive divisors are 11 and pp; an integer n>1n > 1 that is not prime is composite. There are infinitely many primes: given any finite list p1,…,pkp_1, \ldots, p_k, the number N=p1p2⋯pk+1N = p_1p_2\cdots p_k + 1 has a prime factor, and it is none of the pip_i (each leaves remainder 11).

Euclid's argument does not say NN is prime; it says NN has a NEW prime factor, which is all that is needed — no finite list can be complete. To test whether nn is prime, trial-divide by the primes up to n\sqrt n and no further: if n=abn = ab with 1<a≤b1 < a \le b, then a⋅a≤ab=na\cdot a \le ab = n, so the smaller factor is at most n\sqrt n. A composite number cannot hide both of its factors above its square root.

Example
Sieve to 6060
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596017 primes survive; multiples of 2, 3, 5, 7 are struck
Striking multiples of the primes up to √60 leaves exactly the primes.

To list the primes up to 6060, write out 2,…,602, \ldots, 60 and strike the proper multiples of 22, then of 33, 55 and 77. Then stop: 60<8\sqrt{60} < 8, so every composite number up to 6060 has a prime factor of at most 77 and is already struck. What survives are the 1717 primes 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,592, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59.

Example
Is 221221 prime?

If 221=ab221 = ab with 1<a≤b1 < a \le b, then a≤221<15a \le \sqrt{221} < 15 (as 152=22515^2 = 225). So only the primes 2,3,5,7,11,132, 3, 5, 7, 11, 13 need testing. 221221 is odd, its digit sum 55 is not a multiple of 33, and it does not end in 00 or 55; 221=7⋅31+4221 = 7\cdot31 + 4 and 221=11⋅20+1221 = 11\cdot20 + 1; but 221=13⋅17221 = 13\cdot17. Composite.

Example
Why the search must reach n\sqrt n

Is 391391 prime? Here 192=361<391<400=20219^2 = 361 < 391 < 400 = 20^2, so test the primes up to 1919. The small ones fail at a glance; 391=7⋅55+6391 = 7\cdot55 + 6, 391=11⋅35+6391 = 11\cdot35 + 6, 391=13⋅30+1391 = 13\cdot30 + 1; and then 391=17⋅23391 = 17\cdot23. The factor appears only at 1717 — stopping the search early at 1313 would have wrongly declared 391391 prime.

Side note
Where it goes wrong

Believing Euclid's number p1⋯pk+1p_1\cdots p_k + 1 is itself prime. 2⋅3⋅5⋅7+1=2112\cdot3\cdot5\cdot7 + 1 = 211 is prime, but 2⋅3⋅5⋅7⋅11⋅13+1=30031=59⋅5092\cdot3\cdot5\cdot7\cdot11\cdot13 + 1 = 30031 = 59\cdot509 is not. The proof only needs its prime factors to be new, and 5959 and 509509 are. (And 11 is not prime: allowing it would wreck the uniqueness of factorizations, since 6=2⋅3=1⋅2⋅36 = 2\cdot3 = 1\cdot2\cdot3.)

Try it
Try it: primality

Is 197197 prime?

Show the answer

Since 142=196<197<225=15214^2 = 196 < 197 < 225 = 15^2, test the primes up to 1313: 197197 is odd, has digit sum 1717, does not end in 00 or 55; 197=7⋅28+1197 = 7\cdot28 + 1, 197=11⋅17+10197 = 11\cdot17 + 10, 197=13⋅15+2197 = 13\cdot15 + 2. No prime up to 197\sqrt{197} divides it, so it is prime.

The Fundamental Theorem of Arithmetic

Concept
Unique factorization

Every integer n>1n > 1 is a product of primes, and the factorization is unique up to order: n=p1e1p2e2⋯pkekn = p_1^{e_1}p_2^{e_2}\cdots p_k^{e_k}. The exponents answer most questions about nn: the number of divisors is (e1+1)(e2+1)⋯(ek+1)(e_1 + 1)(e_2 + 1)\cdots(e_k + 1); nn is a perfect square exactly when every eie_i is even.

Uniqueness is the deep half. Existence is easy (keep splitting); uniqueness needs Euclid's lemma — if a prime divides a product, it divides a factor — which in turn rests on Bézout. In number systems where uniqueness fails, like Z[−5]\mathbb Z[\sqrt{-5}] where 6=2⋅3=(1+−5)(1−−5)6 = 2\cdot3 = (1 + \sqrt{-5})(1 - \sqrt{-5}), almost every familiar argument breaks. Prime factorizations are the coordinates of the integers: the divisor count formula holds because a divisor of nn is exactly a choice of exponent 0,1,…,ei0, 1, \ldots, e_i for each prime, with no other primes allowed.

Example
Factoring by repeated division

Factor 50405040. Divide by the smallest prime as long as it goes in: 5040→2520→1260→630→3155040 \to 2520 \to 1260 \to 630 \to 315 uses four 22s; 315→105→35315 \to 105 \to 35 uses two 33s; and 35=5⋅735 = 5\cdot7. So 5040=24⋅32⋅5⋅75040 = 2^4\cdot3^2\cdot5\cdot7. Working upward from the smallest prime guarantees nothing is missed: once the 22s are gone, no later quotient is even.

Example
The divisors of 200200
124851020402550100200× 2 across · × 5 up
Each divisor of 200 = 2³·5² is one choice of a column (power of 2) and a row (power of 5).

200=23⋅52200 = 2^3\cdot5^2. By uniqueness, a divisor of 200200 can contain no prime but 22 and 55, and no more of each than 200200 has, so it is 2i5j2^i5^j with 0≤i≤30 \le i \le 3 and 0≤j≤20 \le j \le 2. That is 44 choices times 33 choices: τ(200)=12\tau(200) = 12 divisors, laid out as a grid in which moving right multiplies by 22 and moving up multiplies by 55.

Example
Making a perfect square

Find the smallest positive kk with 540k540k a perfect square. Factor: 540=22⋅33⋅5540 = 2^2\cdot3^3\cdot5. A square needs every exponent even; 222^2 already is, 333^3 needs one more 33, and 515^1 needs one more 55. So k=3⋅5=15k = 3\cdot5 = 15, and indeed 540⋅15=8100=902540\cdot15 = 8100 = 90^2. Any smaller kk leaves an odd exponent somewhere.

Side note
Where it goes wrong

Multiplying the exponents instead of the exponents plus one. For 200=23⋅52200 = 2^3\cdot5^2 that gives 3⋅2=63\cdot2 = 6, but the grid above has 1212 entries: the exponent 00 is a legitimate choice for each prime (it is how 11, 22, 44, 88, 55 and 2525 arise), so each prime offers e+1e + 1 options.

Try it
Try it: square divisors

How many positive divisors of 36003600 are perfect squares?

Show the answer

3600=24⋅32⋅523600 = 2^4\cdot3^2\cdot5^2. A square divisor 2a3b5c2^a3^b5^c needs a,b,ca, b, c even: a∈{0,2,4}a \in \{0, 2, 4\}, b∈{0,2}b \in \{0, 2\}, c∈{0,2}c \in \{0, 2\}. That is 3⋅2⋅2=123\cdot2\cdot2 = \mathbf{12} square divisors (from 11 up to 3600=6023600 = 60^2 itself).

Factorials and Legendre's Formula

Concept
Legendre's formula

The exponent of a prime pp in n!n! is ⌊np⌋+⌊np2⌋+⌊np3⌋+⋯\left\lfloor\dfrac np\right\rfloor + \left\lfloor\dfrac n{p^2}\right\rfloor + \left\lfloor\dfrac n{p^3}\right\rfloor + \cdots, a sum that stops once pk>np^k > n. Each term counts the multiples of a power of pp up to nn.

The factors of n!n! are 1,2,…,n1, 2, \ldots, n, and pp appears in n!n! once for every factor pp in every one of them. Multiples of pp contribute at least one; multiples of p2p^2 contribute a second, counted in the second term; multiples of p3p^3 a third, and so on. So the sum counts each k≤nk \le n exactly as many times as pp divides it. The picture below counts the same dots two ways, by columns and by rows.

Example
The power of 22 in 10!10!
1234567891010/2 → 510/4 → 210/8 → 18 dots in all: the exponent of 2 in 10!
Columns: the factors of 2 in each k. Rows: ⌊10/2⌋, ⌊10/4⌋, ⌊10/8⌋. Same dots, same total.

Stack one dot above each k=1,…,10k = 1, \ldots, 10 for each factor 22 in kk. Counting by rows: the bottom row has a dot at every even kk, ⌊10/2⌋=5\lfloor 10/2 \rfloor = 5 of them; the second row at the multiples of 44, ⌊10/4⌋=2\lfloor 10/4 \rfloor = 2; the third at the multiples of 88, ⌊10/8⌋=1\lfloor 10/8 \rfloor = 1. Total 5+2+1=85 + 2 + 1 = 8, so 282^8 exactly divides 10!=362880010! = 3628800.

Example
Trailing zeros of 100!100!

Each trailing zero is a factor 10=2⋅510 = 2\cdot5, and n!n! has far more 22s than 55s, so the 55s decide. Exponent of 55 in 100!100!: ⌊100/5⌋+⌊100/25⌋=20+4=24\lfloor100/5\rfloor + \lfloor100/25\rfloor = 20 + 4 = 24 (and 125>100125 > 100 stops the sum). So 100!100! ends in 2424 zeros.

Example
The largest power of 1212

Find the largest kk with 12k∣30!12^k \mid 30!. Since 12k=22k3k12^k = 2^{2k}3^k, both primes limit kk. Exponent of 22 in 30!30!: 15+7+3+1=2615 + 7 + 3 + 1 = 26, so 2k≤262k \le 26 and k≤13k \le 13. Exponent of 33: 10+3+1=1410 + 3 + 1 = 14, so k≤14k \le 14. The tighter bound wins: k=13k = 13. Note that the scarcer prime here is 22, because 1212 needs two of them at a time.

Side note
Where it goes wrong

Stopping after the first term. ⌊100/5⌋=20\lfloor 100/5 \rfloor = 20 counts 25,50,75,10025, 50, 75, 100 only once each, but each of them contains 525^2; the second term adds the missing 44. Counting the 22s instead of the 55s is the other slip — the answer is the smaller count, never the larger.

Try it
Try it: trailing zeros

How many zeros end 250!250!?

Show the answer

Count the factors of 55: ⌊250/5⌋+⌊250/25⌋+⌊250/125⌋=50+10+2=62\lfloor250/5\rfloor + \lfloor250/25\rfloor + \lfloor250/125\rfloor = 50 + 10 + 2 = \mathbf{62} (and 625>250625 > 250). The 22s are more plentiful, so there are 6262 zeros.

Why It Works

Lemma: if pp is prime and p∣abp \mid ab, then p∣ap \mid a or p∣bp \mid b. Proof: if p∤ap \nmid a then gcd⁡(p,a)=1\gcd(p, a) = 1, so by Bézout 1=px+ay1 = px + ay for some integers x,yx, y. Multiply by bb: b=pbx+abyb = pbx + aby, and pp divides both terms, so p∣bp \mid b. Uniqueness of factorization follows: if p1⋯pr=q1⋯qsp_1\cdots p_r = q_1\cdots q_s, then p1p_1 divides the right side, hence (by the lemma, repeatedly) divides some qjq_j, hence equals it; cancel and repeat. The lemma is exactly what fails in Z[−5]\mathbb Z[\sqrt{-5}], where 22 divides (1+−5)(1−−5)(1 + \sqrt{-5})(1 - \sqrt{-5}) but neither factor.

Write vp(k)v_p(k) for the number of times pp divides kk; the exponent of pp in n!n! is ∑k≤nvp(k)\sum_{k \le n} v_p(k), the column count in the figure. Now vp(k)v_p(k) equals the number of j≥1j \ge 1 with pj∣kp^j \mid k, so the sum counts pairs (k,j)(k, j) with k≤nk \le n and pj∣kp^j \mid k. Count the same pairs row by row: for fixed jj there are ⌊n/pj⌋\lfloor n/p^j \rfloor multiples of pjp^j up to nn. Switching the order of a double count is one of the most useful moves in the subject.