Problem 14

completed December 1, 2011

Comments

These are getting a lot easier now that I’m understanding Python better. For this script, I used a def(). This allowed me to split up the script into easier and more manageable chunks. It’s also just a good tool to have to make the script look cleaner. I’m glad it’s now in my repertoire.

I also learned to use the while statement. Good one to use here. The != means not equal to.

Results

Took just 51 seconds!

Code

from time import time
start_time= time()
print 'Process started'

#n  n/2 (n is even)
#n  3n + 1 (n is odd)


def seq(n):
	iter = 1
	while n != 1:
		if n%2 == 1:
			n = (3*n) + 1
		elif n%2 == 0:
			n = n/2
		iter = iter + 1
	return iter
			
seq_list = []
for i in range(1000000, 0, -1):
	seq_list.append([seq(i), i])

print max(seq_list)


end_time= time()
elapse_time= end_time - start_time
print elapse_time, 'seconds have passed'