factorial
trailing zeros
Ruby programming
mathematical algorithms
number theory

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.

Practice algorithms

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

ruby
1def trailing_zeros_in_factorial(n)
2  count = 0
3  divisor = 5
4
5  while divisor <= n
6    count += n / divisor
7    divisor *= 5
8  end
9
10  count
11end
12
13puts trailing_zeros_in_factorial(10)   # 2
14puts trailing_zeros_in_factorial(25)   # 6
15puts trailing_zeros_in_factorial(100)  # 24

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 5 contribute 50 / 5 = 10
  • multiples of 25 contribute an extra 50 / 25 = 2
  • multiples of 125 contribute 0

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:

ruby
1def slow_trailing_zeros(n)
2  fact = (1..n).reduce(1, :*)
3  fact.to_s[/0*\z/].to_s.length
4end

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 of 5.
  • The correct formula is n / 5 + n / 25 + n / 125 + ... using integer division.
  • A simple Ruby loop over powers of 5 solves 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 of n!.

Course
Intermediate
27 lessons
15 hours
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 course
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.

Practice algorithms