Python 3 test to prove efficiency of this method
-
(Slowest) Bruteforce method:
def trailing_zeroes_bf(n):
import math
count=0
for i in str(math.factorial(n))[::-1]:
if i=='0':
count+=1
else:
break
return count(Second fastest) Count 5s:
def trailing_zeroes_5(n):
count=0
power=[i for i in range(100) if 5i<=n][::-1]
for i in range(1,n+1):
for pwr in power:
if i%(5pwr)==0:
count+=pwr
break
return count(Fastest) Divide rounded quotient by 5:
def trailing_zeroes_improv5(n):
import math
count=0
start=math.floor(n/5)
while True:
count+=start
start=math.floor(start/5)
if start==0:
return countThe method taught really is more efficient