Skip to content

Instantly share code, notes, and snippets.

@edalorzo
Created November 25, 2012 03:14
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 edalorzo/4142264 to your computer and use it in GitHub Desktop.
Save edalorzo/4142264 to your computer and use it in GitHub Desktop.
Project Euler-Problem #14
def collatz(start=1):
n = start
while n!= 1:
n = n / 2 if not n % 2 else 3 * n + 1
yield n
def count(limit=13):
n = 2
while n < limit:
length = sum([1 for x in collatz(n)])
yield (n, length)
n += 1
if __name__ == '__main__':
#this algoritm needs improvement because it took 26 seconds.
result = reduce(lambda (k1,v1), (k2,v2): (k1,v1) if v1 > v2 else (k2,v2) ,count(1000000))
print "The result is: %s,%s" % result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment