# Exercise 3.7 # a) def sum_1k(M): s = 0 for k in range(1, M+1): s += 1.0/k return s # b) # For M = 3 the correct answer is s = 1 + 1/2 + 1/3 = 1.8333.... def test_sum_1k(): M = 3 expected = 1 + 1.0/2 + 1.0/3 computed = sum_1k(M) success = abs(expected-computed) < 1E-14 message = 'Error detected' assert success, message