[Tutor] Need help speeding up algorithm.

Ian Witham witham.ian at gmail.com
Tue Sep 25 23:30:22 CEST 2007


>
>
> from itertools import count, takewhile
>
> def numfaczeroes2(n):
>      def while_(e):
>          return n//(5**e) > 0
>      return sum(n//(5**exponent) for exponent in takewhile(while_,
> count(1)))
>
>
> It is quite a bit slower, though; probably because of the extra function
> call introduced for while_():
>

I sped this up slightly by using a lambda function instead of while_(), but
not by much.

My solution still took over 5 seconds on the Sphere Judge machine. The top
Python solution takes around 0.6 seconds!

While I haven't submitted Terry's solution, my Timer comparisons suggest
that it would take around 1.5 seconds to process the data. This doesn't take
into account any time used inputting and outputting the test data.

If anyone knows the 0.6 second solution feel free to share!

Ian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070926/f40b963c/attachment-0001.htm 


More information about the Tutor mailing list