Skip to content

Instantly share code, notes, and snippets.

@msalahi
msalahi / collatz.py
Created September 9, 2011 14:53
oheyandy
import time
bound,mem = 1000000,{}
def collatz(n):
if n==1: return [1]
else: return [n] + collatz(n/2 if (n%2==0) else 3*n+1) if (n>1) else [1]
def collatzCount(n):
if n not in mem: mem[n]= 1 + collatzCount(n/2 if (n%2==0) else 3*n+1) if (n>1) else 1
return mem[n]