Sub topic 14
Factorials, Prime Powers, and Trailing Zeros
Prerequisites and the factorial structure
Assume you can factor integers, use exponent notation, and work with divisibility. This chapter uses those skills to analyze factorials.
The factorial of a nonnegative integer \(n\), written \(n!\), is the product of every positive integer from 1 through \(n\):
\[ n! = n(n-1)(n-2)\cdots 2\cdot 1 \]
By definition, \(0! = 1\). A factorial contains many prime factors because each number in the product contributes its own prime factorization.
For example:
\[ 10! = 1\cdot2\cdot3\cdot4\cdot5\cdot6\cdot7\cdot8\cdot9\cdot10 \]
The factors of 2 and 5 are especially important when counting trailing zeros.
Prime-power content of a factorial
The exponent of a prime \(p\) in \(n!\) is found by counting multiples of \(p\), then multiples of \(p^2\), then multiples of \(p^3\), and so on:
\[ 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 \]
Here, \(v_p(n!)\) means the number of times \(p\) appears as a factor in \(n!\). Stop when the power of \(p\) exceeds \(n\).
For \(50!\), the exponent of 5 is:
\[ v_5(50!)=\left\lfloor\frac{50}{5}\right\rfloor+ \left\lfloor\frac{50}{25}\right\rfloor =10+2=12 \]
The first term counts ten multiples of 5. The second term adds one extra factor of 5 from each multiple of 25.
A number such as 25 contributes two factors of 5, while 5 contributes one. This is why counting only multiples of 5 is insufficient.
def prime_exponent_in_factorial(n, p):
count = 0
power = p
while power <= n:
count += n // power
power *= p
return count
print(prime_exponent_in_factorial(50, 5)) # 12Counting trailing zeros
A trailing zero is created by a factor of 10:
\[ 10=2\cdot5 \]
Therefore, the number of trailing zeros in \(n!\) is:
\[ \min(v2(n!),v5(n!)) \]
There are usually more factors of 2 than factors of 5, so the answer is generally \(v_5(n!)\).
| Quantity | Calculation for \(50!\) | Result |
|---|---|---|
| Factors of 2 | \(25+12+6+3+1\) | 47 |
| Factors of 5 | \(10+2\) | 12 |
| Trailing zeros | \(\min(47,12)\) | 12 |
GRE strategy and common errors
For a factorial trailing-zero question:
- Count the powers of 5 using repeated division by 5.
- Count powers of 2 only if the problem asks you to verify the limiting factor.
- Add the quotients, not the remainders.
For example, the number of trailing zeros in \(125!\) is:
\[ \left\lfloor\frac{125}{5}\right\rfloor+ \left\lfloor\frac{125}{25}\right\rfloor+ \left\lfloor\frac{125}{125}\right\rfloor =25+5+1=31 \]
Do not confuse the number of multiples of 5 with the total number of factors of 5. The multiples of 25 and 125 contribute extra factors.
Exercise
What is the exponent of 3 in 20!, and how many trailing zeros does 20! have?
Show answer
The exponent of 3 is floor(20/3) + floor(20/9) = 6 + 2 = 8. The trailing-zero count is floor(20/5) = 4.