Write a Python Program to test and print Collatz Conjecture for a Given Number


Topic: Write a Python Program to test and print Collatz Conjecture for a Given Number

Solution

def collatz(n):
    while n > 1:
        print(n, end=' ')
        if (n % 2):
            # n is odd
            n = 3*n + 1
        else:
            # n is even
            n = n//2
    print(1, end='')



List all Python Programs