Must satisfy the following:
Define a function named series. This function will accept two arguments, both integers, named base and span. The function will compute the sum of the integers, starting at the value of base and down to 0 (zero) that are span apart. In other words, if base is 19 and span is 3, series will compute the sum of 19 + 16 + 13 + 10 + 7 + 4 + 1.
The series function does not need to handle negative base and span values, and these should not be tested for.
The implementation of series must be recursive, and thus must satisfy the three requirements for a recursive function. 1. It must call itself. 2. It must have a stopping condition. 3. It must reduce the problem with each recursive call.
The file must successfully load into a running Python program as a module via the use of the import command.
#content of sumseries.py
def series(m,n):
s=0
#base case, recursion will terminate when the valueof m will be
lessthan or 0
if m <= 0:
return s
# Recursive case:
else: #compute the sum
s= s + m + series(m-n,n)
return s #return s
#content of main.py
import sumseries #import the sumseries module
#input base and span values
base = (int)(input("Enter the value of base"))
span = (int)(input("Enter the value of span"))
if(base<=0 or span<=0): #validate the inputs
print("Invalid Input")
else:
#call series() for sum of sumseries module
print("Sum of series is : ",sumseries.series(base,span))
output
Get Answers For Free
Most questions answered within 1 hours.