编写函数计算1+1/2+1/3+…+1/n
时间: 2023-10-03 22:02:57 浏览: 66
c++求1+1/2+1/3+...+1/n
5星 · 资源好评率100%
def sum_of_reciprocals(n):
if n == 1:
return 1
else:
return 1/n + sum_of_reciprocals(n-1)
print(sum_of_reciprocals(5)) # 输出: 2.283333333333333
print(sum_of_reciprocals(10)) # 输出: 2.9289682539682538
阅读全文