Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python


Suppose we have a number k, now consider the smallest positive integer value x where all values from 1 to k divide evenly. In other words, consider the smallest value x where x is divisible by all numbers from 1 through k. We have to find the number of trailing zeroes in x.

So, if the input is like k = 6, then the output will be 0, as the smallest x here is 60, 60 can be divided using 1, 2, 3, 4, 5 and 6. There is only one trailing zeroes in 60.

To solve this, we will follow these steps −

  • res := 0

  • x := 1

  • while x * 5 <= k, do

    • res := res + 1

    • x := x * 5

  • return res

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, k):
      res = 0
      x = 1
      while x * 5 <= k:
         res += 1
         x *= 5
      return res
ob = Solution()
k = 6
print(ob.solve(k))

Input

6

Output

1

Updated on: 25-Dec-2020

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements