Math Core

Module 3.6 · Number Theory

Factorials and Legendre's formula

You already know how to count trailing zeros of n!n! by counting factors of 55. Legendre's formula is the general version: it gives the exact power of any prime in n!n!. With it you can find trailing zeros in any base, the largest power of 1212 dividing 50!50!, and the power of a prime dividing a binomial coefficient. That last one leads to a beautiful fact about which entries of Pascal's triangle are odd.

Legendre's formula

Among 1,2,…,n1, 2, \dots, n, exactly ⌊np⌋\left\lfloor \dfrac{n}{p} \right\rfloor are multiples of pp, and each contributes at least one factor of pp to n!n!. Exactly ⌊np2⌋\left\lfloor \dfrac{n}{p^2} \right\rfloor are multiples of p2p^2, and each contributes a second factor. Continue with p3,p4,…p^3, p^4, \dots Each number is counted once for every factor of pp it has.

Legendre's formula

The exponent of the prime pp in n!n! is

vp(n!)=⌊np⌋+⌊np2⌋+⌊np3⌋+⋯=n−sp(n)p−1,v_p(n!) = \left\lfloor \frac{n}{p} \right\rfloor + \left\lfloor \frac{n}{p^2} \right\rfloor + \left\lfloor \frac{n}{p^3} \right\rfloor + \cdots = \frac{n - s_p(n)}{p - 1},

where sp(n)s_p(n) is the sum of the digits of nn in base pp.

The digit-sum form is a great shortcut for p=2p = 2: v2(n!)=n−(number of 1s in the binary form of n)v_2(n!) = n - (\text{number of 1s in the binary form of } n). For instance, 2026=1111110101022026 = 11111101010_2 has eight 11s, so v2(2026!)=2026−8=2018v_2(2026!) = 2026 - 8 = 2018.

Trailing zeros in any base

n!n! ends in kk zeros in base bb when bkb^k divides n!n! but bk+1b^{k+1} does not. Factor bb into primes; for each prime power pep^e in bb, the number of copies available is ⌊vp(n!)e⌋\left\lfloor \dfrac{v_p(n!)}{e} \right\rfloor. The answer is the smallest of these. In base 1010 the bottleneck is always 55, but in other bases you must check each prime.

Binomial coefficients

Since (nk)=n!k! (n−k)!\dbinom{n}{k} = \dfrac{n!}{k!\,(n-k)!}, you get vp(nk)=vp(n!)−vp(k!)−vp((n−k)!)v_p\dbinom{n}{k} = v_p(n!) - v_p(k!) - v_p((n-k)!). Using the digit-sum form, this simplifies to

vp(nk)=sp(k)+sp(n−k)−sp(n)p−1,v_p\binom{n}{k} = \frac{s_p(k) + s_p(n - k) - s_p(n)}{p - 1},

which equals the number of carries when you add kk and n−kn - k in base pp (Kummer's theorem). In particular, (nk)\dbinom{n}{k} is odd exactly when adding kk and n−kn - k in binary has no carries, meaning every 11 in the binary form of kk sits where nn also has a 11. So if nn has mm ones in binary, exactly 2m2^m entries in row nn of Pascal's triangle are odd.

Worked example: A prime in a factorial

What is the largest power of 33 that divides 100!100!?

⌊1003⌋+⌊1009⌋+⌊10027⌋+⌊10081⌋=33+11+3+1=48.\left\lfloor \frac{100}{3} \right\rfloor + \left\lfloor \frac{100}{9} \right\rfloor + \left\lfloor \frac{100}{27} \right\rfloor + \left\lfloor \frac{100}{81} \right\rfloor = 33 + 11 + 3 + 1 = 48.

So 3483^{48} divides 100!100! but 3493^{49} does not.

Worked example: Trailing zeros in base 12

How many zeros does 30!30! end in when written in base 1212?

12=22⋅312 = 2^2 \cdot 3. Factors of 22: 15+7+3+1=2615 + 7 + 3 + 1 = 26, enough for ⌊262⌋=13\left\lfloor \dfrac{26}{2} \right\rfloor = 13 copies of 222^2. Factors of 33: 10+3+1=1410 + 3 + 1 = 14. The bottleneck is 222^2, so 30!30! ends in 1313 zeros in base 1212.

Worked example: A power of 2 in a binomial coefficient

What is the largest power of 22 dividing (10050)\dbinom{100}{50}?

By Legendre, v2(100!)=50+25+12+6+3+1=97v_2(100!) = 50 + 25 + 12 + 6 + 3 + 1 = 97 and v2(50!)=25+12+6+3+1=47v_2(50!) = 25 + 12 + 6 + 3 + 1 = 47. So v2(10050)=97−2⋅47=3v_2\dbinom{100}{50} = 97 - 2 \cdot 47 = 3, and the answer is 232^3.

With carries: 50=110010250 = 110010_2 has three 11s, and adding 50+5050 + 50 in binary carries exactly once for each of them. Three carries again gives 232^3.

Worked example: Odd entries in a row of Pascal's triangle

For how many kk with 0≤k≤1000 \le k \le 100 is (100k)\dbinom{100}{k} odd?

100=11001002100 = 1100100_2 has three 11s. (100k)\dbinom{100}{k} is odd exactly when the 11s of kk are among those three positions, so there are 23=82^3 = 8 such kk.

Common mistake

For a base like 12=22⋅312 = 2^2 \cdot 3, don't just compare v2v_2 and v3v_3. You need two 22s for every 1212, so divide v2v_2 by 22 (rounding down) before comparing. Also, don't stop Legendre's sum early: ⌊npk⌋\left\lfloor \dfrac{n}{p^k} \right\rfloor terms keep contributing until pk>np^k \gt n.

Practice

Practice 1

How many zeros are at the end of 2026!2026!?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

What is the largest integer kk such that 7k7^k divides 500!500!?

Practice 3

How many zeros does 20!20! end in when written in base 66?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

What is the largest integer kk such that 12k12^k divides 50!50!?

Practice 5

What is the smallest positive integer kk such that no factorial n!n! ends in exactly kk zeros?

Practice 6

What is the exponent of 33 in the prime factorization of (200100)\dbinom{200}{100}?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

What is the smallest positive integer nn such that 21002^{100} divides n!n!?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 8

For how many integers kk with 0≤k≤2550 \le k \le 255 is (255k)\dbinom{255}{k} odd, and for how many kk with 0≤k≤2560 \le k \le 256 is (256k)\dbinom{256}{k} odd? Give the sum of the two counts.

Real contest practice