The Collatz Conjecture states that for any natural number n, if n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. If you repeat the process continuously for n, n will eventually reach 1.

For example, if n = 20, the resulting sequence will be:

[20, 10, 5, 16, 8, 4, 2, 1] 

Write a program that will output the length of the Collatz Conjecture for any given n. In the example above, the output would be 8.

For more reading see: http://en.wikipedia.org/wiki/Collatz_conjecture

Source: codewars | Difficulty: EASY
Tags: Mathematics

Starter code: def collatz(n):
	

Solutions:
def collatz(n):
	step = 1
	while n != 1:
		n = [n // 2, n * 3 + 1][n % 2]
		step += 1
	return step


from itertools import count

def collatz(n):
	for iteration in count(1):
		if n == 1:
			return iteration
		elif n % 2 == 0:
			n //= 2
		else:
			n = 3 * n + 1


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