[Tutor] Can my code be optimized any further (speed-wise)?

Geoframer geoframer at gmail.com
Sat Oct 7 23:35:05 CEST 2006


Hi everyone,

The last few days i've been learning python and have been doing this by
trying to solve problems for a programming competition.
One particular problem is bugging me. I have already solved it but my
solution does not compute in the set time-condition. I know
for a fact that this particular problem is solvable in the time-limit using
Python, so i was wondering if my solution is maybe inefficitient code-wise.
If my code can't be optimized for speed any more i must be missing something
and should labor hard to find a new algorithm ;-).

The problem is to compute the number of trailing zero's in factorials (n! =
1*2*3*4*.......*n). with n <= 1000000000

My solution is as follows (a = times to read a number (b) to process) :

---------------------------------------------------------------------------

a = input()
for i in range(a):
    lst = (5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
48828125, 244140625)
    ans = 0
    b = input()
    for i in lst:
        if i <= b:
            ans += b//i
    print ans

----------------------------------------------------------------------------

Please, if you have suggestions to improve the speed of this algorithm give
an argumentation as to why a certain something is faster.
That would allow me to learn to program faster algorithms in Python al
together instead of just in this problem.

Kind regards - Geofram

P.s. I know python is probably not the best language for these kinds of
problems (I can code it in C or C++), it just bugs me that it's possible
and my solution is failing...  ;-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061007/3ecd30c3/attachment.html 


More information about the Tutor mailing list