[Shootout-list] harmonic.python

Tim Rowe digitig at gmail.com
Sun Apr 23 22:57:15 UTC 2006


The program for the harmonic sum benchmark looks to have some addition
operations that can be taken out, and the explicit casts can be taken
out (though I don't know if that's a time saving, as an implicit cast
will still be done).  Here's an amended version that /might/ be
faster.

# http://shootout.alioth.debian.org/
#
# Contributed by Greg Buchholz
# Amended by Tim Rowe to remove O(N) redundant additions,
# O(N) explicit casts and one function call. Might be faster.

import sys

s = 0.0
for i in range(1, int(sys.argv[1]) + 1):
    s = s + 1.0/i
print "%.9f" % s

-- 
Tim Rowe



More information about the Shootout-list mailing list