Search code examples
pythonpython-3.xcollatz

Collatz Loop Structure


Please help me understand what i did wrong. The problem is

The Collatz Sequence Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1.

Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.”)

Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value.

Hint: An integer number is even if number % 2 == 0, and it’s odd if number % 2 == 1.

def collatz(number):
    if number%2==0:
        print(number//2)
    else:
        print(3*number+1)

x = int(input('print num'))
while TRUE:
    if collatz(x)!=1:
        break

Solution

  • You must print and return in the function collatz(num):

    def collatz(number):
        """prints and returns the next number in the Collatz sequence
        """
        if number % 2 == 0:
            next_collatz_number = number // 2
        else:
            next_collatz_number = 3 * number + 1
        print(next_collatz_number)
        return next_collatz_number
    
    x = int(input('print num'))
    
    while True:
        x = collatz(x)
        if x == 1:
            break