0

I am trying to count the iterations in a function, but I can't get it right. What is wrong with the code?

Every time I call the function, the counter give me zero as a result. I want to add a pause between iteration an print the result one by one, but I couldn't fix it.

My code is below:

n = 41

def collatz(n):
   
    count = 0

    if n != 1 and n % 2 == 1 :
        
        n = n * 3 + 1
        print(n) 
        count += 1
        collatz(n)
        
    
    elif n != 1 and n % 2 == 0:
        
        n = n / 2 
        print(n)
        count += 1 
        collatz(n)
        
    
    else:
        print('done')
        print(count)
        return
        
    
collatz(n)

4
  • your problem is that you are restarting your counter every time Commented Sep 19, 2022 at 20:05
  • 1
    count has to be a global variable or something that collatz returns so that the caller can add it to a running total. Commented Sep 19, 2022 at 20:06
  • Just initiate your counter variable outside of function so every time it is called it is not zeroed. Also print(count) should be outside too. Commented Sep 19, 2022 at 20:06
  • I'm sure you didn't mean to write a recursive function. You could use a loop by adding while True: just after initialising count and adjusting the indentation. Commented Sep 19, 2022 at 20:24

2 Answers 2

3

Its because in each recursion/function call, its again set to 0. You can pass it as an argument to the function to get past it.

Something like this should help solve it.

def collatz(n, count=0):
   
    if n != 1 and n % 2 == 1 :
        
        n = n * 3 + 1
        print(n) 
        count += 1
        collatz(n, count)
        
    
    elif n != 1 and n % 2 == 0:
        
        n = n / 2 
        print(n)
        count += 1 
        collatz(n, count)
        
    
    else:
        print('done')
        print(count)
        return
        
    
collatz(n)
Sign up to request clarification or add additional context in comments.

Comments

-1

The other answer is correct, in that the issue is that the counter is reset at the beginning of each function call. The simplest alternative, which I do not recommend, is to use a global counter variable. The other answer (there's only one at the moment) passes the iteration along in the recursive call, which also works.

Since it looks like you're exploring the Collatz Conjecture, I expect that you'd like to do some more experiments with the sequence of numbers. To that end, it is better to make a generator for the sequence, then do whatever processing (counting and printing) you'd like on that sequence. This makes it much more versatile.

The following generator function can be used in the way you requested, but is more versatile.

def collatz(n):
    while n>1:
        if n%2 == 1:
            n = 3*n + 1
        else:
            n = n//2
        yield n

for n, count in enumerate(collatz(5)):
    print(f'n={n+1},\t\tcount={count}')

#how many steps from 41
print(sum(1 for n in collatz(41)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.