how many trailing zeroes are there in 100! (100 factorial)?
Solution
One per factor of 10, and one per factor of 5 (there are more than enough 2’s to pair with the 5’s), plus one per factor of ten squared (one occurrence) and one per factor of 5 squared (three occurrences).
So if I’m counting correctly, that’d be 10 + 10 + 1 + 3== 24 zeroes.
Assuming the question meant *trailing* zeroes. It’d be much harder to also count the intermingled zero digits in the entire expansion.
2026 Update: Trailing Zeros in n! — And Why It Matters
How many trailing zeros does 100! have? The naive approach — compute 100! then count zeros — overflows. The clever approach: count factors of 10 = min(factors of 2, factors of 5) in 100!. Since factors of 2 always exceed factors of 5, just count factors of 5.
def trailing_zeros_factorial(n: int) -> int:
"""
Count trailing zeros in n!
Each trailing zero requires one factor of 10 = 2 × 5.
Factors of 2 always outnumber factors of 5, so count 5s.
"""
count = 0
power = 5
while power int:
"""Count trailing zeros in n! when written in base b."""
from sympy import factorint
prime_factors = factorint(b)
result = float('inf')
for p, exp in prime_factors.items():
# Count factors of prime p in n!
p_count = 0
power = p
while power <= n:
p_count += n // power
power *= p
result = min(result, p_count // exp)
return result
# Base 12: 12 = 2^2 * 3, so need pairs of (2^2, 3)
print(trailing_zeros_base_b(100, 12)) # limited by 3s: 100//3+100//9+...=48, //2=24
Why interviewers ask this: Tests mathematical thinking over brute-force. The key insight (count prime factors of the smaller prime) generalizes to: number of times k divides n!, Legendre’s formula, and combinatorics problems about divisibility. Appears at Jane Street, Citadel, Google, and quantitative trading firms.
💡Strategies for Solving This Problem
Recursion and Large Numbers
This showed up at Amazon in 2024. Seems simple but tests recursion, overflow handling, and optimization thinking.
The Problem
Calculate 100! (100 factorial). That's 100 × 99 × 98 × ... × 2 × 1.
Challenge: Result has 158 digits. Standard integers overflow. Need BigInt or custom solution.
Approach 1: Simple Recursion
Classic factorial:
factorial(n) = n × factorial(n-1) factorial(0) = 1
O(n) time, O(n) space for call stack. Works but not optimized.
Approach 2: Iterative
Loop from 1 to n, multiply. Same O(n) time but O(1) space. Better.
Approach 3: Tail Recursion
Convert to tail-recursive form so compiler can optimize to iteration. Some languages (not JavaScript) optimize this automatically.
The Real Challenge: BigInt
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
That's 158 digits. Need arbitrary precision arithmetic.
JavaScript has BigInt built-in (ES2020+). Other languages need libraries or manual implementation.
Optimization: Memoization
If calculating multiple factorials, cache results. factorial(100) uses factorial(99), etc.
At Amazon
Started with simple recursion. Interviewer asked "What about overflow?" That's when I switched to BigInt. Then asked "How to optimize for many queries?" Showed memoization.
Also discussed: How many trailing zeros? (24 - count factors of 5)