(Summing series) Write a recursive function to compute the following series:
m(i) = 1/2 + 2/3 + ........ + i/i+1
Write a test program that prompts the user to enter an integer for i and displays m(i).
Summing Series Program code:
# Recursive function to compute the series
# m(i) = 1/2 + 2/3 + ... + i / (i + 1).
def compute_series(i):
# Base case: If i is 1, return
#the base value of the series.
if i == 1:
return 1 / 2.0
# Recursive case: Compute the sum of
#the series up to i-1 and add i / (i + 1).
return compute_series(i - 1) + i / (i + 1)
# Prompt the user for input
i = int(input("Enter an integer value for i: "))
# Compute and display m(i)
result = compute_series(i)
print(f"m({i}) = {result:.4f}")
Executed Output:
Enter an integer value for i: 22
m(22) = 19.2657