(Sum series) Write a recursive function to compute the following series:
m(i) = 1+ 1/2 + 1/3 +.........+1/i
Write a test program that displays m(i) for i 1, 2, ..., 10.
Sum Series Python code:
#'computeSeries' is used to compute the
#series m(i) = 1 + 1/2 + 1/3 + ... + 1/i.
def computeSeries(i):
# Base case: If i is 1, return
# the base value of the series.
if i == 1:
return 1.0
# Recursive case: Compute the sum of the
#series up to i-1 and add 1/i.
return computeSeries(i - 1) + 1.0 / i
# Test program to display m(i) for i = 1 to 10.
for i in range(1, 11):
result = computeSeries(i)
print(f"m({i}) = {result:.4f}")
Executed Output:
m(9) = 2.8290
m(10) = 2.9290