Skip to content

Instantly share code, notes, and snippets.

@Spriithy
Last active August 10, 2016 09:13
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 Spriithy/e226fffba599ef57f250d761dbc027d6 to your computer and use it in GitHub Desktop.
Save Spriithy/e226fffba599ef57f250d761dbc027d6 to your computer and use it in GitHub Desktop.
Simple calculation program for the 3x+1 problem
import time
it = 0
L = []
i = 1
mi = 0
mv = 1
dt = time.time()
while i <= 1000000:
d = time.time()
j = i
t = 0
print('----= Checking ' + str(int(i)) + ' =----')
while not j in L:
if j == 1:
break
else:
print('[iteration:' + str(it) + ']\tInserted:\t' + str(int(j)))
L.append(int(j))
if j % 2 == 0:
j /= 2
else:
j = 3 * j + 1
t += 1
it += 1
print('[iteration:' + str(it) + ']\tValidated ' + str(int(i)) + ' with ' + str(t) + ' iterations in ' + str(d - dt) + 's.')
dt = d
if t > mi:
mi = t
mv = i
i += 1
it += 1
print('-------------------------------------------------')
print('Max iterations for: ' + str(int(mv)) + ' in ' + str(mi) + ' iterations.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment