Skip to content

Instantly share code, notes, and snippets.

@rustyconover
Created May 5, 2015 20:51
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 rustyconover/07dd22f4a67c598167bf to your computer and use it in GitHub Desktop.
Save rustyconover/07dd22f4a67c598167bf to your computer and use it in GitHub Desktop.
Find the number that results in the longest set of terms when using the Collatz sequence
def collatz_count_until_1(n):
count = 0
while n != 1:
if n % 2 == 0:
n = n / 2
else:
n = (3 * n) + 1
count += 1
return count
max = None
for x in range(1, 300000):
length = collatz_count_until_1(x)
if max == None or max[0] < length:
max = (length, x)
print("Maximum stopping distance {0}, starting number {1}".format(max[0], max[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment