Convert loop to recursive function
Question:
I have written a python for loop iteration as show below. I was wondering if its possible to convert into a recursive function.
a = int(input("Please enter the first number: "))
b = int(input("Please enter the second number: "))
res = 0
for i in range(a,b+1):
temp = 1
for j in range(1,i+1):
temp = temp * j
res = res + temp
print("Sum of products from 1 to each integer in the range ",a," to ",b," is: ",res)
I am expecting something like the below example:
def recursion(a,b):
res = 0
if condition or a while condtion
....
return ....
a = int(input("Please enter the first number: "))
b = int(input("Please enter the second number: "))
print("Sum of products from 1 to each integer in the range ",a," to ",b," is: ",res)
Any idea ?
Answers:
Could be something like this, imagine dividing the problem into smaller sub-problems
def recursive_sum(a, b, res=0):
if a > b:
return res
temp = 1
for j in range(1, a+1):
temp = temp * j
res = res + temp
return recursive_sum(a+1, b, res)
a = int(input("Please enter the first number: "))
b = int(input("Please enter the second number: "))
res = recursive_sum(a, b)
print(f"Sum of products from 1 to each integer in the range {a} to {b} is: {res}")
a = int(input("Please enter the first number: "))
b = int(input("Please enter the second number: "))
res = sum([x for x in range(int(a), int(b)+1)])
print(res)
My question is why do you need it to be recursive?
I ask that because this particular answer doesn’t seem best approached through recursion.
if you need to use recursion
def recursive_sum(m,n):
if n <= m:
return n
else:
return n+recursive_sum(m, n-1)
EDIT:
the question asked included the sum of the factorial of a range, apologies for only including the sum portion.
def factorial(b):
if (b==1 or b==0):
return 1
else:
return (b * factorial(b - 1))
res = sum(map(factorial, range(a,b+1)))
The original code you’re multiplying each value in the range by the product of the previous and summing.
range(5,10) for instance
total for the way the problem is written
res comes to 4500198
where the factorial sum = 4037880
If you print out just for value 5
you get 1+3+9+33+153 where !5=120
Which is the way you’re trying to accomplish it?
I have written a python for loop iteration as show below. I was wondering if its possible to convert into a recursive function.
a = int(input("Please enter the first number: "))
b = int(input("Please enter the second number: "))
res = 0
for i in range(a,b+1):
temp = 1
for j in range(1,i+1):
temp = temp * j
res = res + temp
print("Sum of products from 1 to each integer in the range ",a," to ",b," is: ",res)
I am expecting something like the below example:
def recursion(a,b):
res = 0
if condition or a while condtion
....
return ....
a = int(input("Please enter the first number: "))
b = int(input("Please enter the second number: "))
print("Sum of products from 1 to each integer in the range ",a," to ",b," is: ",res)
Any idea ?
Could be something like this, imagine dividing the problem into smaller sub-problems
def recursive_sum(a, b, res=0):
if a > b:
return res
temp = 1
for j in range(1, a+1):
temp = temp * j
res = res + temp
return recursive_sum(a+1, b, res)
a = int(input("Please enter the first number: "))
b = int(input("Please enter the second number: "))
res = recursive_sum(a, b)
print(f"Sum of products from 1 to each integer in the range {a} to {b} is: {res}")
a = int(input("Please enter the first number: "))
b = int(input("Please enter the second number: "))
res = sum([x for x in range(int(a), int(b)+1)])
print(res)
My question is why do you need it to be recursive?
I ask that because this particular answer doesn’t seem best approached through recursion.
if you need to use recursion
def recursive_sum(m,n):
if n <= m:
return n
else:
return n+recursive_sum(m, n-1)
EDIT:
the question asked included the sum of the factorial of a range, apologies for only including the sum portion.
def factorial(b):
if (b==1 or b==0):
return 1
else:
return (b * factorial(b - 1))
res = sum(map(factorial, range(a,b+1)))
The original code you’re multiplying each value in the range by the product of the previous and summing.
range(5,10) for instance
total for the way the problem is written
res comes to 4500198
where the factorial sum = 4037880
If you print out just for value 5
you get 1+3+9+33+153 where !5=120
Which is the way you’re trying to accomplish it?