The Collatz conjecture is one of the most famous one. Take any positive integer n, if it is even divide it by 2, if it is odd multiply it by 3 and add 1 and continue indefinitely.The conjecture is that whatever is n the sequence will reach 1. There is many ways to approach this problem, each one of them had given beautifull graphs and impressive display of calculation power. The simplest approach can be found in this kata: http://www.codewars.com/kata/5286b2e162056fd0cb000c20
You look at the Collatz sequence of a number and see when it reaches 1.

In this kata we will take a look at the length of collatz sequences. And how they evolve. Write a function that take a positive integer n and return the number between 1 and n that has the maximum Collatz sequence length and the maximum length. The output has to take the form of an array [number, maxLength] For exemple the Collatz sequence of 4 is [4,2,1], 3 is [3,10,5,16,8,4,2,1], 2 is [2,1], 1 is [1], so `MaxCollatzLength(4)` should return `[3,8]`. If n is not a positive integer, the function have to return [].

* As you can see, numbers in Collatz sequences may exceed n.

The last tests use random big numbers so you may consider some optimisation in your code:

* You may get very unlucky and get only hard numbers: try submitting 2-3 times if it times out; if it still does, probably you need to optimize your code more;

* Optimisation 1: when calculating the length of a sequence, if n is odd, what 3n+1 will be ?

* Optimisation 2: when looping through 1 to n, take i such that i<n/2, what will be the lenght of the sequence for 2i ?

Difficulty: EASY
Starter code: def max_collatz_length(n):
	

Solutions:
from functools import lru_cache

@lru_cache(maxsize=None)
def rec(n):
	return 1 + (0 if n == 1 else rec(3 * n + 1) if n & 1 else rec(n // 2))
memo = [[0, None], [1, 1]]

def max_collatz_length(n):
	if not (type(n) == int and n > 0):
		return []
	while n >= len(memo):
		x = rec(len(memo))
		if x > memo[-1][1]:
			memo.append([len(memo), x])
		else:
			memo.append(memo[-1])
	return memo[n]


class Collatz(object):

	def __init__(self):
		self.memory = {1: 1}

	def calculate(self, n):
		if n in self.memory:
			return self.memory[n]
		result = self.calculate(n // 2) + 1 if n % 2 == 0 else self.calculate(3 * n + 1) + 1
		self.memory[n] = result
		return result

def max_collatz_length(n):
	if not isinstance(n, int) or n <= 0 or isinstance(n, bool):
		return []
	m = 0
	z = 0
	collatz = Collatz()
	for i in range(1, n + 1):
		c = collatz.calculate(i)
		if c > m:
			m = c
			z = i
	return [z, m]


c = {1: 1}

def F(n):
	if n in c:
		return c[n]
	c[n] = 1 + F(n * 3 + 1 if n & 1 else n // 2)
	return c[n]
max_collatz_length = lambda n: [] if type(n) != int or n <= 0 else max([[i, F(i)] for i in range(1, n + 1)], key=lambda x: x[1])


def max_collatz_length(limit):
	if type(limit) != int or limit < 1:
		return []
	results = {1: 1}
	for n in range(2, limit + 1):
		count = 0
		x = n
		while x not in results:
			x = x * 3 + 1 if x % 2 else x // 2
			count += 1
		count += results[x]
		results[n] = count
	sol = max(results, key=results.get)
	return [sol, results[sol]]


def max_collatz_length(n):
	if type(n) != int or n <= 0:
		return []
	lengths = {1: 1}
	for i in range(2, n + 1):
		a = [i]
		while a[-1] not in lengths:
			a += [3 * a[-1] + 1] if a[-1] % 2 else [a[-1] // 2]
		for (j, k) in enumerate(a[::-1]):
			lengths[k] = j + lengths[a[-1]]
	return [max(lengths, key=lengths.get), lengths[max(lengths, key=lengths.get)]]


def max_collatz_length(n):
	if type(n) != int:
		return []
	if n == 0:
		return []
	if n == 1:
		return [1, 1]
	try:
		results = [0 for i in range(n + 1)]
		results[0] = 1
		results[1] = 1
		for num in range(2, n + 1):
			col = num
			ctr = 0
			shortCut = 0
			while shortCut == 0:
				if col % 2 == 0:
					ctr += 1
					col = col >> 1
				else:
					ctr += 2
					col = 3 * col + 1 >> 1
				if col < n:
					shortCut = results[col]
			results[num] = results[col] + ctr
		return [results.index(max(results[1:])), max(results[1:])]
	except:
		return []


from operator import itemgetter

def _collatz_len(n, lengths):
	if n in lengths:
		return [n, lengths[n]]
	count = 0
	curr_num = n
	nums = []
	while curr_num != 1:
		nums.append(curr_num)
		if curr_num in lengths:
			for (i, x) in enumerate(nums):
				if x not in lengths:
					lengths[x] = count + lengths[curr_num] - i
			return [n, count + lengths[curr_num]]
		if curr_num % 2:
			nums.append(3 * curr_num + 1)
			curr_num = (3 * curr_num + 1) // 2
			count += 1
		else:
			curr_num //= 2
		count += 1
	count += 1
	for (i, x) in enumerate(nums):
		if x not in lengths:
			lengths[x] = count - i
	return [n, count]

def max_collatz_length(n):
	if not isinstance(n, int) or isinstance(n, bool) or n <= 0:
		return []
	curr_len = max_len = 0
	lengths = {1: 1, 2: 2}
	results = [_collatz_len(x, lengths) for x in range(1, n // 2 + 1)]
	for x in range(n // 2 + 1, n + 1):
		if x % 2:
			results.append(_collatz_len(x, lengths))
		else:
			results.append([x, results[x // 2 - 1][1] + 1])
	return max(results, key=itemgetter(1))


cache = {1: 1}

def max_collatz_length(n):

	def collatz(n):
		if n in cache:
			return cache[n]
		next_n = 3 * n + 1 if n % 2 else n / 2
		ret = cache[n] = 1 + collatz(next_n)
		return ret
	if not isinstance(n, int) or isinstance(n, bool) or n < 1:
		return []
	return max(([i, collatz(i)] for i in range(1, n + 1)), key=lambda p: p[1])


c = [1, 2, 3, 6, 7, 9, 18, 25, 27, 54, 73, 97, 129, 171, 231, 313, 327, 649, 703, 871, 1161, 2223, 2463, 2919, 3711, 6171, 10971, 13255, 17647, 23529, 26623, 34239, 35655, 52527, 77031, 106239, 142587, 156159, 216367, 230631, 410011, 511935, 626331, 837799]

def max_collatz_length(n):
	if type(n) != int or n < 1:
		return []
	ans = max((i for i in c if i <= n))
	steps = 1
	x = ans
	while x > 1:
		steps += 1
		x = 3 * x + 1 if x % 2 else x // 2
	return [ans, steps]