LeetCode 172 Factorial Trailing Zeroes (Medium)| Count Trailing Zeroes in Factorials Solution In Java

Ever wondered why some numbers have so many trailing zeroes? When working with large factorials like 100! or even 25!, trailing zeroes seem to appear out of nowhere! But what if I told you there’s a simple, efficient way to count those zeroes without ever calculating the full factorial? Yes, there is! And we can do it with just a few lines of Java code. Read on to find out how.

The Secret Behind Trailing Zeroes in Factorials

Trailing zeroes are produced by factors of 10 in a number, and factors of 10 come from multiplying 2 and 5. In factorials, you’ll have plenty of 2s, but 5s are rarer. So, the number of trailing zeroes in a factorial is determined by how many multiples of 5 are present in the numbers from 1 to n.

Sounds easy, right? Let's break it down with an example.

Example:

For n = 25, we want to know how many trailing zeroes are in 25!.

  • Multiples of 5 in this range are: 5, 10, 15, 20, 25. Each of these contributes a factor of 5.
  • But 25 is special. It's a multiple of 
    525^2

So, how do we count all these 5s efficiently? The answer lies in this simple Java solution.

A Quick and Slick Java Solution

Here’s the magic code that does the job in just a few lines:

private static int getTrailingZeros(int n) {

    int count = 0;

    while(n>0) {

n /=5;

count += n;

    }

    return count;

}

How It Works:

  • Divide and conquer: Every time you divide n by 5, you count how many multiples of 5 exist. This is where the 5s hide!
  • Stack 'em up: Add up those counts. Some numbers (like 25, 125, etc.) contain more than one factor of 5, so we keep dividing n by 5 until there are no more 5s to count.
  • Fast as lightning: This method works in O(log n) time, which means it’s super fast, even for large numbers like 100!.

Example Walkthrough for n = 100:

Let’s see the magic happen for n = 100:

  1. First loop: 100 / 5 = 20 → We found 20 multiples of 5.
  2. Second loop: 20 / 5 = 4 → We found 4 more multiples of 25 (which give extra factors of 5).
  3. Now n = 0, so the loop stops. The total count is 24 trailing zeroes.

Without breaking a sweat, we know that 100! has 24 trailing zeroes—no need to calculate the full factorial!

Why This Solution Rocks:

  • Efficient: Traditional approaches might involve calculating huge numbers like 100!, but this method skips all that heavy lifting and zooms right in on the key factor—multiples of 5.
  • Simple: With just a few lines of Java code, you can solve this problem for any number n.
  • Scalable: Whether n is 25, 100, or 10,000, this approach handles it with ease.

Conclusion: Count Trailing Zeroes Like a Pro

So the next time you’re faced with the daunting task of calculating how many zeroes are trailing behind a giant factorial, don’t panic! With this clever Java trick, you can unlock the answer in no time.

Trailing zeroes don’t have to be a mystery—now you have the power to count them like a pro!

Post a Comment

Previous Post Next Post