Created
June 30, 2014 15:08
-
-
Save danvctr/c5187c5495d991bf11e3 to your computer and use it in GitHub Desktop.
Python solution for https://projecteuler.net/problem=14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def collatz(n, seq): | |
seq.append(n) | |
if n == 1: | |
return n | |
else: | |
if n % 2 == 0: | |
return collatz(int(n/2), seq) | |
else: | |
return collatz(((3*n)+1), seq) | |
def main(): | |
longestLen = 0 | |
longestSeq = 0 | |
for n in range(1, 1000000): | |
seq = list() | |
collatz(n, seq) | |
if len(seq) > longestLen: | |
longestLen = len(seq) | |
longestSeq = seq | |
print "The longest seq is %s with a length of %s." % (longestSeq, longestLen) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment