Geoframer wrote:
> Hi everyone,
> 
> 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.

Try putting the actual computation into a function. Make any variable 
that is used more than once a local variable of the function (ans, b,i). 
Accessing local variables is faster than accessing global variables. 
This gives about 40% speedup in my tests.

Use psyco if it is available in your contest, for me that shaved off 
another 30-50%; you will get more benefit from psyco the more iterations 
you have, because psyco introduces some compilation overhead that has to 
be made up by the faster code.
http://psyco.sourceforge.net/

You really aren't doing much so there's not a lot to optimize that I can 
see. You could try asking on comp.lang.python, lots of people there are 
good at optimization.

Kent



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to