Skip to content

Instantly share code, notes, and snippets.

@anirudhjayaraman
Created September 1, 2015 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anirudhjayaraman/3674adfdd3dffbfd8cfd to your computer and use it in GitHub Desktop.
Save anirudhjayaraman/3674adfdd3dffbfd8cfd to your computer and use it in GitHub Desktop.
Project Euler Problem 14 - naive method
# Longest Collatz Sequence under a million
# Function listing collatz sequence for a number
def collatz(n):
"function listing collatz sequence for a positive integer"
coll = []
coll.append(n)
while n != 1:
if n % 2 == 0:
n = n/2
coll.append(n)
else:
n = 3*n + 1
coll.append(n)
return coll
longest = 0
j = 0
for i in xrange(1, 1000000):
lencoll = len(collatz(i))
if lencoll > longest:
longest = lencoll
j = i
print j
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment