#exercise 3.7 from "A primer on..."
"""
compute and return: sum_(k=1)^M (1/M)
"""
def sum_1k(M):
s = 0
for k in range(1,M+1):
s += 1.0/k
return s
def test_sum_1k():
expected = 1.0+1.0/2+1.0/3
computed = sum_1k(3)
tol = 1e-10
success = abs(expected-computed) < tol
msg = f'Expected {expected}, got {computed}'
assert success,msg
test_sum_1k()
print(sum_1k(2))
print(sum_1k(10))
"""
Terminal> python sum_func.py
1.5
2.9289682539682538
Note that there is no output from the call to the test
function, since the test passes.
"""