I need help with recursion !!!!!
Problem: create a program that takes in an Integer and returns a list of factors. Return empty list if input is less than or equal to 0. Return List(1) if input is 1.
The solution cannot use mutability, meaning no variables and you can only assign values. It cannot use break
Solution can be in pseudo code, java, scala, or python. Only thing is it has to be a recursive solution
I'm having trouble appending to the list using recursion for instance when I debug my solution for factor(1050), I get
List(2, 3, 5, 5, 7)
List(2, 3, 5, 5)
List(2, 3, 5, 5)
List(2, 3)
List(2, 3)
List(2) <---- this is what is returned
This means my newest stack has all the factors but it is not being returned. I don't know if that makes sense. If anything seeing a working solution will help. Help is much appreciated.
I don't completely understand what you want to say but here I done something what I understant from your problem.
def function_list (first, current_no, n,
single_result_list):
if (first> n or current_no > n):
return
if (current_no == n) :
print(*single_result_list)
return
for i in range(first, n):
if (i * current_no > n):
break
if (n % i == 0):
single_result_list.append(i)
function_list(i,
i * current_no, n,
single_result_list)
single_result_list.remove(single_result_list[-1])
def fact(n):
single_result_list = []
function_list(2, 1, n,single_result_list)
n = 1050
fact(n)
--> here the lists are created from different factors of this number.
I need help with recursion !!!!! Problem: create a program that takes in an Integer and...