the number of trailing zeros in a factorial of a given number - Ruby
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
The number of trailing zeros in n! is not found by computing the factorial and counting zeros at the end. The efficient solution is to count how many factors of 5 appear in n!, because factors of 2 are already abundant.
Why Factors of 5 Matter
A trailing zero in base 10 comes from a factor of 10, and:
10 = 2 * 5
In a factorial, there are always more factors of 2 than factors of 5, so the number of factors of 5 determines the answer.
That means the count of trailing zeros is:
floor(n / 5) + floor(n / 25) + floor(n / 125) + ...
The extra terms matter because numbers like 25 contribute two factors of 5, numbers like 125 contribute three, and so on.
Ruby Implementation
This runs in O(log_5 n) time, which is very efficient even for large inputs.
Step-by-Step Example
Take n = 50.
- multiples of
5contribute50 / 5 = 10 - multiples of
25contribute an extra50 / 25 = 2 - multiples of
125contribute0
So the answer is:
10 + 2 = 12
That means 50! ends with 12 zeros.
Why You Should Not Build the Factorial
A brute-force approach might look like this:
This works only for small values and does far more work than necessary. Factorials grow extremely fast, so building the whole value is wasteful when the factor-of-five formula gives the answer directly.
This is a classic example where mathematical reasoning beats brute force.
Edge Cases
For 0! and 1!, the answer is 0, because:
- '
0! = 1' - '
1! = 1'
The same function already handles those cases naturally because the loop never runs when n < 5.
You should also assume the input is a nonnegative integer. Negative factorials are not defined in the usual integer factorial sense, so reject or guard such inputs if your code may receive them.
Why the Formula Scales Well
Each loop multiplies the divisor by 5, so the number of iterations is small even for large n.
For example, if n = 1_000_000, the divisors are:
- '
5' - '
25' - '
125' - '
625' - and so on
That is far cheaper than multiplying one million integers together.
Common Pitfalls
Computing the full factorial and then counting zeros is the main inefficiency.
Forgetting that 25, 125, and larger powers of 5 contribute multiple factors causes undercounting.
Spending time counting factors of 2 is unnecessary because factorials always contain more than enough of them.
Confusing decimal trailing zeros with binary trailing zeros leads to the wrong problem entirely.
Summary
- Trailing zeros in
n!are determined by the number of factors of5. - The correct formula is
n / 5 + n / 25 + n / 125 + ...using integer division. - A simple Ruby loop over powers of
5solves the problem efficiently. - Do not compute the whole factorial just to count zeros.
- The algorithm is fast because it grows with powers of
5, not with the size ofn!.
Related reading
- The relationship between Paxos family and data consistency
- The Sieve of Atkin
- The sieve of Eratosthenes in F
- The simplest algorithm for poker hand evaluation
- This version of TensorFlow Probability requires TensorFlow version 2.3
- Three questions about doing lots of calculations
- The time complexity of counting sort
- Three Way Merge Algorithms for Text

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.