SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
5.recursion
Exercise:
Programming Excercises
Question:6 | ISBN:978013274719 | Edition: 6

Question

(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).

 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.