harmonicf.py


Below is the syntax highlighted version of harmonicf.py from §2.1 Using and Defining Functions.


#-----------------------------------------------------------------------
# harmonicf.py
#-----------------------------------------------------------------------

import stdio
import sys

# Return the nth harmonic number.
def harmonic(n):
    total = 0.0
    for i in range(1, n+1):
        total += 1.0 / float(i)
    return total

# Write to standard output the harmonic numbers specified as
# command-line arguments.

for j in range(1, len(sys.argv)):
    arg = int(sys.argv[j])
    value = harmonic(arg)
    stdio.writeln(value)

#-----------------------------------------------------------------------

# python harmonicf.py 1 2 4
# 1.0
# 1.5
# 2.083333333333333

# python harmonicf.py 10 100 1000 10000
# 2.9289682539682538
# 5.187377517639621
# 7.485470860550343
# 9.787606036044348


Copyright © 2000–2015, Robert Sedgewick, Kevin Wayne, and Robert Dondero.
Last updated: Fri Oct 20 20:45:16 EDT 2017.