Skip to content

Instantly share code, notes, and snippets.

@prkagrawal
Last active June 20, 2020 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prkagrawal/736033bbce27ff94b6d8d4fe0a9a047b to your computer and use it in GitHub Desktop.
Save prkagrawal/736033bbce27ff94b6d8d4fe0a9a047b to your computer and use it in GitHub Desktop.
Calculating no of trailing zeroes in factorial of given number
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