Last active
June 20, 2020 15:17
-
-
Save prkagrawal/736033bbce27ff94b6d8d4fe0a9a047b to your computer and use it in GitHub Desktop.
Calculating no of trailing zeroes in factorial of given number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def trailing_zeroes(n): | |
# 0s are produced when 2 and 5 are multiplied | |
# so you'll need to count how many 2s and 5s are there | |
# 2s are always more than 5s so count just 5s | |
res = 0 | |
k = 5 | |
# find all powers of 5 | |
# 25 has 2 5s, 125 has 3 5s, etc | |
while k <= n: | |
res += n // k | |
k *= 5 | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment