Sub topic 14
Factorials, prime powers, and trailing zeros
This chapter assumes you can write a prime factorization, use exponent notation, and count divisors from exponents. You also need floor division: ⌊17/5⌋ = 3 means "how many whole 5s fit in 17". Everything here applies those skills to one object, n!.
What a factorial contains
For a positive integer n, n! = 1 × 2 × 3 × ... × n, and 0! = 1 by definition. Two consequences drive almost every GRE factorial question:
- n! is a multiple of every integer from 1 to n, because each one appears as a factor.
- n! has a large, predictable prime factorization. You can read off how many times each prime appears without multiplying anything out.
For example, 10! = 3,628,800 = 2⁸ × 3⁴ × 5² × 7. The sections below show how to get those exponents directly.
Counting a prime in n!
To find the exponent of a prime p in n!, count how many factors of p each number from 1 to n supplies:
- Every multiple of p supplies at least one p. There are ⌊n/p⌋ of them.
- Every multiple of p² supplies a second p that the first count missed. There are ⌊n/p²⌋ of them.
- Every multiple of p³ supplies a third, and so on.
Add the counts until the power of p exceeds n:
exponent of p in n! = ⌊n/p⌋ + ⌊n/p²⌋ + ⌊n/p³⌋ + ...
For 10!:
- Prime 2: ⌊10/2⌋ + ⌊10/4⌋ + ⌊10/8⌋ = 5 + 2 + 1 = 8
- Prime 3: ⌊10/3⌋ + ⌊10/9⌋ = 3 + 1 = 4
- Prime 5: ⌊10/5⌋ = 2
- Prime 7: ⌊10/7⌋ = 1
This matches 2⁸ × 3⁴ × 5² × 7. The code below implements the formula and checks it against a direct count on the full product.
from math import factorial
def exponent_in_factorial(n, p):
count = 0
power = p
while power <= n:
count += n // power
power *= p
return count
def exponent_in_integer(m, p):
count = 0
while m % p == 0:
m //= p
count += 1
return count
for n, p in [(10, 2), (10, 3), (10, 5), (50, 5), (100, 7)]:
formula = exponent_in_factorial(n, p)
direct = exponent_in_integer(factorial(n), p)
assert formula == direct
print(n, p, formula)The output is 10 2 8, 10 3 4, 10 5 2, 50 5 12, 100 7 16. The assertions pass, so the formula and the brute-force count agree.
Trailing zeros
A trailing zero is a factor of 10 in the number. Since 10 = 2 × 5, each zero needs one 2 and one 5. The number of trailing zeros is therefore the number of complete pairs (2, 5) in the prime factorization, which is min(exponent of 2, exponent of 5).
In n!, the exponent of 2 is never smaller than the exponent of 5, because multiples of 2 are far more common than multiples of 5. The 5s are the bottleneck, so:
trailing zeros of n! = ⌊n/5⌋ + ⌊n/25⌋ + ⌊n/125⌋ + ...
def trailing_zeros_of_factorial(n):
count = 0
power = 5
while power <= n:
count += n // power
power *= 5
return count
print(trailing_zeros_of_factorial(50)) # 12
print(trailing_zeros_of_factorial(100)) # 24The table shows how the terms build up, including the jumps at powers of 5.
| n | ⌊n/5⌋ | ⌊n/25⌋ | ⌊n/125⌋ | Trailing zeros |
|---|---|---|---|---|
| 24 | 4 | 0 | 0 | 4 |
| 25 | 5 | 1 | 0 | 6 |
| 50 | 10 | 2 | 0 | 12 |
| 100 | 20 | 4 | 0 | 24 |
| 125 | 25 | 5 | 1 | 31 |
Some counts are impossible
Going from 24! to 25! adds two zeros at once, so n! never has exactly 5 trailing zeros. The values of n from 20 to 24 give 4, and n = 25 gives 6. The number of trailing zeros skips values at every power of 5. Watch for this in questions of the form "for how many n does n! end in exactly k zeros?". The answer can be 0 or 5, and it is never anything in between.
Trailing zeros of other numbers
The same pairing logic works for any product, not only factorials. Write the number in prime form and take the smaller of the exponents of 2 and 5. Here the 2s are not guaranteed to be more plentiful, so you must check both.
For 12⁵ × 25³, expand each factor:
- 12⁵ = (2² × 3)⁵ = 2¹⁰ × 3⁵
- 25³ = (5²)³ = 5⁶
The product is 2¹⁰ × 3⁵ × 5⁶. It has min(10, 6) = 6 trailing zeros. The 3⁵ plays no part.
The largest power that divides n!
A question may ask for the largest integer k such that m^k divides n!. Follow these steps:
- Factor m into primes, for example 12 = 2² × 3.
- For each prime p with exponent e in m, compute the exponent of p in n!.
- Divide that exponent by e and round down.
- Take the minimum over all primes of m.
Take m = 12 and n = 20. The exponent of 2 in 20! is 10 + 5 + 2 + 1 = 18, and 18 ÷ 2 gives 9. The exponent of 3 is 6 + 2 = 8, and 8 ÷ 1 gives 8. The minimum is 8, so 12⁸ divides 20! but 12⁹ does not.
from math import factorial
def exponent_in_factorial(n, p):
count = 0
power = p
while power <= n:
count += n // power
power *= p
return count
def largest_power_dividing_factorial(m, n):
best = None
p = 2
while m > 1:
if m % p == 0:
e = 0
while m % p == 0:
m //= p
e += 1
k = exponent_in_factorial(n, p) // e
best = k if best is None else min(best, k)
p += 1
return best
k = largest_power_dividing_factorial(12, 20)
print(k) # 8
print(factorial(20) % 12**k == 0) # True
print(factorial(20) % 12**(k + 1) == 0) # FalseFactorial algebra and divisibility shortcuts
Many GRE factorial questions need no prime counting. They need factoring.
- Cancel the common part. n!/(n−2)! = n(n−1), because (n−2)! cancels. So 12!/10! = 12 × 11 = 132.
- Factor out the smaller factorial in a sum. 10! + 11! = 10!(1 + 11) = 10! × 12. This shows that the sum is divisible by 12 and by every divisor of 10!.
- Use "n! + k" for 2 ≤ k ≤ n. Both terms are divisible by k, so the sum is divisible by k. For example, 20! + 7 is a multiple of 7, so it is not prime. This means n! + 2, n! + 3, ..., n! + n are all composite. It also shows that arbitrarily long runs of consecutive composite integers exist.
- n! + 1 is not divisible by any integer from 2 to n. Dividing it by any such k leaves remainder 1. So any prime factor of n! + 1 must be larger than n.
Edge cases to check
- Small n. For n < p, the exponent of p in n! is 0. For example, 7 does not divide 6!.
- 0! and 1!. Both equal 1. They have no prime factors and no trailing zeros.
- Factorials of non-integers or negatives. The GRE defines n! only for non-negative integers. If a question includes such a value, it is usually a trap.
- Number of divisors. To count the divisors of n!, first find every prime exponent. For 10! = 2⁸ × 3⁴ × 5² × 7, the count is (8+1)(4+1)(2+1)(1+1) = 270. Include every prime up to n, not only 2 and 5.
- Perfect-square questions. n! is a perfect square only for n = 0 or 1. For n ≥ 2, the largest prime p ≤ n appears exactly once in n! (by Bertrand's postulate, there is a prime between n/2 and n, and it has no second multiple at or below n). One odd exponent rules out a square.
Exercises
Exercise
How many trailing zeros does 80! have?
Show answer
Add ⌊80/5⌋ + ⌊80/25⌋ = 16 + 3 = 19. The next term, ⌊80/125⌋, is 0. So 80! has 19 trailing zeros.
Exercise
What is the largest integer k such that 6^k divides 20!?
Show answer
Write 6 = 2 × 3. The exponent of 2 in 20! is 10 + 5 + 2 + 1 = 18, and the exponent of 3 is 6 + 2 = 8. The limiting prime is 3, so k = min(18, 8) = 8.
Exercise
Is there a positive integer n such that n! has exactly 5 trailing zeros? Explain.
Show answer
No. 24! has ⌊24/5⌋ = 4 trailing zeros. 25! has 5 + 1 = 6, because 25 contributes two 5s. The count jumps from 4 to 6 and skips 5.
Exercise
Is 30! + 17 prime, composite, or impossible to tell?
Show answer
Composite. 17 is at most 30, so 17 divides 30!, and it also divides 17. It therefore divides the sum 30! + 17. The sum is larger than 17, so it has a divisor other than 1 and itself.