superstewie
superstewie

Reputation: 63

Collatz function not exiting correctly

Here's a program that is intended to count the length of a Collatz sequence recursively:

def odd_collatz ( n ):
    return (3 * n) + 1

def even_collatz ( n ):
    return int(n / 2)

def collatz_counter ( initialNumber, initialLength ):
    length = initialLength

    while True:
        if initialNumber == 1:
            return length

        elif initialNumber != 1:
            length += 1

            if initialNumber % 2 == 0:
                 collatz_counter(even_collatz(initialNumber), length)

            else:
                collatz_counter(odd_collatz(initialNumber), length)

print(collatz_counter(13, 1)

The expected answer should be 10. However, the program gets stuck in an infinite loop. At the second to last step of the sequence initalNumber equals 2. The program functions as expected: collatz_counter is called using even_collatz and the number 10.

The expected action of the next step would be to run collatz_counter with an initialNumber of 1 and an initialLength of 10. What I would expect would happen is that the first if statement would evaluate to true, collatz_counter should return length and then exit. This is not, however, what happens:

What actually happens is that the function evaluates the first if statement, runs the return length line, and then jumps to the line of code under if initialNumber % 2... and the whole process repeats itself over and over and over in an infinite loop.

Any ideas as to why this might be happening?

Upvotes: 2

Views: 99

Answers (3)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19174

The main error is the while True: loop, coupled with the missing returns.

def odd_collatz ( n ):
    return (3 * n) + 1

def even_collatz ( n ):
    return int(n / 2)

def collatz_counter(initialNumber, length):
    if initialNumber == 1:
        return length
    elif initialNumber != 1:
        length += 1
        if initialNumber % 2 == 0:
             return collatz_counter(even_collatz(initialNumber), length)
        else:
            return collatz_counter(odd_collatz(initialNumber), length)

print(collatz_counter(13, 1))

prints 10.

Upvotes: 0

linusg
linusg

Reputation: 6439

Looks like a typo to me. You define a function collatz_counter expecting two numbers.

But you call it like this:

...
print(collatz_counter(13), 1)

Just try to change the last line to:

print(collatz_counter(13, 1))

And it should be fine.

Hope this helps!

Upvotes: 2

qfwfq
qfwfq

Reputation: 2516

You are mixing recursion and looping in a bit of an odd way. The problem is while True:. Because you never return anything from within the loop there is nothing stopping it from going on forever. Your code reaches 1 then just keeps adding to the length. Here is a fixed version.

def odd_collatz ( n ):
    return (3 * n) + 1

def even_collatz ( n ):
    return int(n / 2)

def collatz_counter ( initialNumber, initialLength ):
    length = initialLength


    if initialNumber == 1:
        return length

    elif initialNumber != 1:
        length += 1

        if initialNumber % 2 == 0:
            return collatz_counter(even_collatz(initialNumber), length)

        else:
            return collatz_counter(odd_collatz(initialNumber), length)

print(collatz_counter(13, 1))

Upvotes: 3

Related Questions