-1

I am trying to solve a problem where recursion is a must. The tasks is: Write a function that takes in an integer n and returns the highest integer in the corresponding Collatz sequence.

My solution is this:

collatz = []

def max_collatz(num):
    collatz.append(num)

    if num == 1:
        return max(collatz)
    else:
        return max_collatz(num / 2) if num%2 == 0 else max_collatz((3 * num) + 1)

However, I need to find a way to solve it without using a list outside of the function. I really couldn't find a solution, is there any?

  • 1
    Have `max_collatz` take a list as a second argument? It doesn't even look like you ever modify it other than to append to it. – wkl Aug 14 '22 at 15:13
  • The function should take only one argument to my understanding. If there isn't another way, then my solution should be good anyways – mustafa bodur Aug 14 '22 at 15:14
  • Why do you think it should only take one argument? If you don't want to have a global list, the solution would be to pass it in as an argument to your method. Additionally, you don't 'need' to solve this via recursion specifically (iterative and recursive methods can be converted between each other). – wkl Aug 14 '22 at 15:15
  • 3
    When you say `"recursion is a must"` why is this so? The `collatz` conjecture seems to be iterative to me. – quamrana Aug 14 '22 at 15:17
  • The reason is that this is an assignment specifically for a recursion. However, if there isn't a way I can store the item in a list inside the function, then my solution should be good like this! Thanks. – mustafa bodur Aug 14 '22 at 15:18
  • Why use a `list` when recursion provides its own collection of return values? – quamrana Aug 14 '22 at 15:21
  • The way your code runs would indicate that you don't even need to store a list, since you only ever care about retrieving `max` of it, which makes me think that you could always return the `max` value of what you're getting from the recursive calls. – wkl Aug 14 '22 at 15:26
  • Your `collatz` function probably should actually be executing the sequence, rather than returning the max. Inside of it, I would update some kind of global that has the current maximum value. – Eric Jin Aug 14 '22 at 15:28
  • 2
    As a programmer you should try very, *very*, **very** hard not to use globals. – quamrana Aug 14 '22 at 15:29
  • @quamrana it's an assignment. And it will probably stack overflow (literally) on any sizable input. Yes, there's a good-practices way to do it iteratively, but the assignment won't let us do that. – Eric Jin Aug 14 '22 at 15:30
  • @EricJin: Yes, that question has been asked and answered. I suspected as much, but we always need clarification. – quamrana Aug 14 '22 at 15:32

1 Answers1

2

It's either the current number or the largest in the rest of the sequence.

def max_collatz(n):
    if n == 1:
        return 1
    elif n % 2:
        return max_collatz(3 * n + 1)
    else:
        return max(n, max_collatz(n // 2))
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65