Sub topic 15
Powers of a Prime in n! and Trailing Zeros
This chapter assumes you can factorize a number into prime powers, use floor division (the integer part of a quotient), and read exponent notation. You should also know that the HCF of exponents is replaced by a minimum when you compare prime factorizations. The aim is to find the highest power of a prime, or of a composite number, that divides n!. Trailing zeros are the most common CAT use of this.
Why n! needs its own method
n! means 1 × 2 × 3 × … × n. For n = 100 the product has 158 digits, so you cannot multiply it out or factorize it directly. You can still find how many times a prime p divides it by counting how many factors in the product contribute a p.
Take 10! and ask for the power of 3:
- Multiples of 3 up to 10 are 3, 6, 9. That gives three factors of 3.
- 9 is also a multiple of 9, so it contributes one extra 3.
- The total is 3 + 1 = 4, so 3^4 divides 10! and 3^5 does not.
The extra count for 9 is the key point. Multiples of p give one factor of p each. Multiples of p² give one more each. Multiples of p³ give one more again, and so on.
Legendre's formula
The exponent of a prime p in n! is
E_p(n!) = ⌊n/p⌋ + ⌊n/p²⌋ + ⌊n/p³⌋ + …
Stop when p^k exceeds n, because every later term is zero.
A practical way to compute it is to divide repeatedly:
- Divide n by p and keep the integer quotient.
- Divide that quotient by p again and keep the integer quotient.
- Repeat until the quotient is 0.
- Add all the quotients.
For 100! and p = 5, the quotients are 20, 4, 0, so the exponent is 24.
Trailing zeros
A trailing zero is a zero at the end of a number's decimal form. Each trailing zero comes from one factor of 10, and 10 = 2 × 5. The number of trailing zeros of N is therefore the number of complete (2, 5) pairs in N's prime factorization. That equals the smaller of the exponent of 2 and the exponent of 5.
In n!, factors of 2 are far more plentiful than factors of 5, since every second number is even and only every fifth is a multiple of 5. So the exponent of 5 is the smaller one, and
trailing zeros of n! = E_5(n!)
Here is the full calculation for 100!:
| Prime | Quotients | Exponent in 100! |
|---|---|---|
| 2 | 50, 25, 12, 6, 3, 1 | 97 |
| 5 | 20, 4 | 24 |
The minimum of 97 and 24 is 24, so 100! ends in 24 zeros.
Running the calculation in Python
The program below computes Legendre's formula for any prime. It also handles composite divisors, a case covered in the next section.
def legendre(n, p):
total = 0
while n:
n //= p
total += n
return total
def factorize(m):
f = {}
d = 2
while d * d <= m:
while m % d == 0:
f[d] = f.get(d, 0) + 1
m //= d
d += 1
if m > 1:
f[m] = f.get(m, 0) + 1
return f
def max_power(n, m):
"""Largest k such that m**k divides n!"""
return min(legendre(n, p) // e for p, e in factorize(m).items())
print(legendre(100, 5)) # 24
print(legendre(100, 2)) # 97
print(max_power(50, 12)) # 22
print(max_power(100, 12)) # 48Composite divisors: the highest power of m in n!
To find the highest k such that m^k divides n!, write m as a product of prime powers. Then work through these steps:
- Factorize m = p₁^a₁ × p₂^a₂ × …
- Compute E_p(n!) for each prime p in m.
- Divide each exponent by the matching a, and take the integer part.
- The answer is the smallest of those results.
Example. Find the highest power of 12 that divides 50!.
- 12 = 2² × 3.
- E₂(50!) = 25 + 12 + 6 + 3 + 1 = 47, and ⌊47/2⌋ = 23.
- E₃(50!) = 16 + 5 + 1 = 22, and ⌊22/1⌋ = 22.
- The minimum is 22, so 12^22 divides 50! and 12^23 does not.
Notice that here 3 is the limiting prime, not 2. A prime does not have to be the largest to be the bottleneck. You must check every prime in m.
The same idea gives trailing zeros in other bases. A number ends in k zeros in base b when b^k divides it. So the number of trailing zeros of 100! in base 12 is max_power(100, 12) = 48. Check this: E₂ = 97 gives ⌊97/2⌋ = 48, and E₃ = 33 + 11 + 3 + 1 = 48, so the minimum is 48.
Finding n from a required exponent
CAT also asks the reverse question: what is the smallest n for which n! contains at least a given power of p?
Example. Find the smallest n such that 5^6 divides n!.
- E₅ changes only at multiples of 5, so test multiples of 5.
- For n = 20, E₅ = 4. For n = 25, E₅ = 5 + 1 = 6.
- For n = 24, E₅ = 4, which is too small.
- The smallest n is 25.
The exponent is a step function, and the steps get bigger at powers of p. The table shows this for p = 5.
| n | 4 | 5 | 9 | 10 | 20 | 24 | 25 | 30 |
|---|---|---|---|---|---|---|---|---|
| Trailing zeros of n! | 0 | 1 | 1 | 2 | 4 | 4 | 6 | 7 |
Edge cases and traps
- Skipped values. The count jumps from 4 at n = 24 to 6 at n = 25. No n has n! ending in exactly 5 zeros. The same happens after every power of 5. When a question asks how many n give exactly k zeros, the answer can be 0 or 5. It is 5 when k is reachable, because the values n, n+1, n+2, n+3, n+4 share one count.
- A quick upper bound. E_p(n!) is always less than n/(p − 1). For 100! and p = 5, the bound is 25, and the true value is 24. This is useful for ruling out answer options.
- Do not divide once. The most common error is computing only ⌊100/5⌋ = 20 and forgetting the 25 and 125 terms.
- Floor each term separately. Compute ⌊n/p²⌋ from n, or equivalently floor the running quotient. Never round fractions, and never add fractions before taking the floor.
- Products that are not factorials. Trailing zeros come from any product's 2s and 5s, so you must count both. For example, 10 × 20 × 30 × … × 100 equals 10^10 × 10!. The 10! contributes 2 more zeros, so the product ends in 12 zeros. Here the factors of 2 do not automatically exceed the factors of 5, so check both when the product is not a plain factorial.
- Do not confuse digits and zeros. "Trailing zeros" counts only zeros at the end. Zeros inside the number do not count.
Exercises
Exercise
How many trailing zeros does 250! have?
Show answer
Count the 5s: ⌊250/5⌋ = 50, ⌊250/25⌋ = 10, ⌊250/125⌋ = 2, and ⌊250/625⌋ = 0. The total is 50 + 10 + 2 = 62. Factors of 2 are more plentiful, so 250! ends in 62 zeros.
Exercise
What is the highest power of 18 that divides 40!?
Show answer
18 = 2 × 3². E₂(40!) = 20 + 10 + 5 + 2 + 1 = 38, so the 2-limit is 38. E₃(40!) = 13 + 4 + 1 = 18, and ⌊18/2⌋ = 9, so the 3-limit is 9. The minimum is 9, so 18^9 is the highest power.
Exercise
Is there a positive integer n for which n! ends in exactly 11 zeros? Explain.
Show answer
n = 45 gives ⌊45/5⌋ + ⌊45/25⌋ = 9 + 1 = 10 zeros. n = 50 gives 10 + 2 = 12 zeros. Since 11 lies between these, check n = 46 to 49: each has the same count as 45, which is 10. So no n gives exactly 11 zeros.