Skip to content

Instantly share code, notes, and snippets.

View rdrewd's full-sized avatar

R. Drew Davis rdrewd

View GitHub Profile
@rdrewd
rdrewd / problem14_results.txt
Created November 26, 2013 07:55
Timing results for the various implementations of Project Euler problem 14. Nico's is fastest.
$ time python -t -O problem14_nico.py
('The starting position generating the longest sequence is', 837799)
real 0m6.914s
user 0m6.645s
sys 0m0.202s
$ time python -t -O main_timer.py problem14
args= ['main_timer.py', 'problem14']
from problem14 import main
@rdrewd
rdrewd / problem14_nico.py
Created November 26, 2013 07:51
Nico's implementation of Project Euler problem 14.
"""
Longest Collatz sequence
------------------------
The following iterative sequence is defined for the set of positive integers:
n n/2 (n is even)
n 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 40 20 10 5 16 8 4 2 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
@rdrewd
rdrewd / problem14.py
Created November 26, 2013 07:47
Cached implementation of Project Euler problem 14. Unlike the uncached version, this one meets the performance requirement.
#! `env python` -t
def seq(n):
"""
Generate the sequence for Project Euler problem 14.
===================================================
n is a positive integer that is the first number in the sequence.
I'm told that although it hasn't been proven, that it is believed that
regardless of the starting number, the sequence eventually gets to 1,
@rdrewd
rdrewd / problem14_nocache.py
Created November 26, 2013 07:40
Un-cached implementation of Project Euler problem 14. Fails to meet the "under a minute" time requirement.
#! `env python` -t
def seq(n):
"""
Generate the sequence for Project Euler problem 14.
===================================================
n is a positive integer that is the first number in the sequence.
I'm told that although it hasn't been proven, that it is believed that
regardless of the starting number, the sequence eventually gets to 1,
@rdrewd
rdrewd / maintimer.py
Created November 26, 2013 07:26
My "main" routine for Project Euler. Runs that program specified by arg1, times the execution and prints the answer and timing The timer is rather too coarse and the details (real time vs. CPU time) differ from Linux to WIndows.
from time import clock
if __name__ == "__main__":
import sys
print "args=", sys.argv
x="from " + sys.argv[1] + " import main"
print x
exec x
tstart=clock()*1000
answer=main()
tend=clock()*1000