Python program to calculate harmonic series

后端 未结 11 1914
终归单人心
终归单人心 2020-12-06 19:15

Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...

11条回答
  •  盖世英雄少女心
    2020-12-06 19:24

    How about this:

    partialsum = 0
    for i in xrange(1,1000000):
        partialsum += 1.0 / i
    print partialsum
    

    where 1000000 is the upper bound.

提交回复
热议问题