Python program to calculate harmonic series

后端 未结 11 1920
终归单人心
终归单人心 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:38

    Using the simple for loop

    def harmonicNumber(n):
    x=0
    for i in range (0,n):
        x=x+ 1/(i+1)
    return x
    

提交回复
热议问题