algorithm


Finding natural numbers having n Trailing Zeroes in Factorial


I need help with the following problem.
Gives an integer m and i need to find the number of positive integers n, such that the factorial of n ends with exactly m zeroes.
I wrote this code it works fine and i get the right output, but it take way too much time as the numbers increase.
a=input()
while a:
x=[]
m,n,fact,c,j=input(),0,1,0,0
z=10*m
t=10**m
while z-1:
fact=1
n=n+1
for i in range(1,n+1):
fact=fact*i
if fact%t==0 and ((fact/t)%10)!=0:
x.append(int( n))
c=c+1
z=z-1
for p in range(c):
print x[p],
a-=1
print c
Could someone suggest me a more efficient way to do this. Presently, it take 30 sec for a test case asking for numbers with 250 trailing zeros in its factorial.
Thanks
To get number of trailing zeroes of n! efficiently you can put
def zeroes(value):
result = 0;
d = 5;
while (d <= value):
result += value // d; # integer division
d *= 5;
return result;
...
# 305: 1234! has exactly 305 trailing zeroes
print zeroes(1234)
In order to solve the problem (what numbers have n trailing zeroes in n!) you can use these facts:
number of zeroes is a monotonous function: f(x + a) >= f(x) if a >= 0.
if f(x) = y then x <= y * 5 (we count only 5 factors).
if f(x) = y then x >= y * 4 (let me leave this for you to prove)
Then implement binary search (on monotonous function).
E.g. in case of 250 zeroes we have the initial range to test [4*250..5*250] == [1000..1250]. Binary search narrows the range down into [1005..1009].
1005, 1006, 1007, 1008, 1009 are all numbers such that they have exactly 250 trainling zeroes in factorial
Focus on the number of 2s and 5s that makes up a number. e.g. 150 is made up of 2*3*5*5, there 1 pair of 2&5 so there's one trailing zero. Each time you increase the tested number, try figuring out how much 2 and 5s are in the number. From that, adding up previous results you can easily know how much zeros its factorial contains.
For example, 15!=15*...*5*4*3*2*1, starting from 2:
Number 2s 5s trailing zeros of factorial
2 1 0 0
3 1 0 0
4 2 0 0
5 2 1 1
6 3 1 1
...
10 5 2 2
...
15 7 3 3
..
24 12 6 6
25 12 8 8 <- 25 counts for two 5-s: 25 == 5 * 5 == 5**2
26 13 8 8
..
Refer to Peter de Rivaz's and Dmitry Bychenko's comments, they have got some good advices.

Related Links

Multiliteration implementation with inaccurate distance data
Weighted Quick-Union with Path Compression. How can depth of 4 can be achieved?
How to check if number is power of 5 [closed]
N th term of series:0,1,3,6,10,15,21,
Runtime of the algorithm?
Quick-Find Union on N Elements
PMR QuadTree data structure and algorithm [closed]
Board-traversing function in Haskell returning duplicates
Intersection points between 2 discs in 3d
Optimized search - can anyone help to calculate the complexity of this algorithm
Stem comparsion algorithm
Evenly duplicate games to reach a maximum amount per participant
Finding all the nodes between two certain vertices in a directed graph
Find points given distances between them
X-Y heuristic function for solving N-puzzle
How to create a simple circular array?

Categories

HOME
windows
sonarqube
pagination
interface
obfuscation
alarm
yandex
soa
big-o
zip
vsm
openpyxl
mxgraph
datastage
jsonpath
enterprise-architect
symbol
parameter-passing
capistrano3
esoteric-languages
powershell-v5.0
google-data-studio
jersey-1.0
squashfs
highstock
jbutton
github3.py
login-script
vue-chartjs
dynamic-jasper
least-squares
searchkit
openalpr
cd
filesystemwatcher
cin
movilizer
monkey-testing
procfile
plan-9
pom.xml
manova
corruption
semantic-logging
modelsim
css-transforms
libjpeg
monkey
choice
materialized-path-pattern
angular2-testing
stax
x-tag
i386
avspeechsynthesizer
appscale
yajsw
libvlc
maybe
dynamics-nav-2016
custom-errors
iokit
forwarding
mod-proxy
airbrake
avahi
phpldapadmin
bcache
infosphere-spl
dbaccess
execve
android-cookiemanager
encryption-asymmetric
jjaql
mutual-authentication
workitem
scmmanager
http-status-code-410
pdfviewer
panda3d
jquery-blockui
uimanageddocument
objectquery
appjs
usertype
filemerge
update-statement
exponentiation
umra
web-garden
burndowncharts
cfwindow
spread
dojo-dnd
entitykey
flvplayback
product-management
idatareader

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile