Question

I need help with recursion !!!!! Problem: create a program that takes in an Integer and...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
I need help with recursion !!!!! Problem: create a program that takes in an Integer and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT