diff --git a/problem1.py b/problem1.py index 1d0a2b3..b850184 100644 --- a/problem1.py +++ b/problem1.py @@ -6,13 +6,12 @@ 233168 """ - import itertools -from utilities import lower_than +from utilities import lower_than -LIMIT=1000 -ANSWER=233168 +LIMIT = 1000 +ANSWER = 233168 def multiples(n): @@ -20,7 +19,8 @@ def multiples(n): Yields all the multiples of n (to infinity) """ for i in itertools.count(start=1): - yield i*n + yield i * n + threes = itertools.takewhile(lower_than(LIMIT), multiples(3)) fives = itertools.takewhile(lower_than(LIMIT), multiples(5)) @@ -31,4 +31,3 @@ def multiples(n): assert answer == ANSWER print answer - diff --git a/problem14.py b/problem14.py index 494dd97..4085aa5 100644 --- a/problem14.py +++ b/problem14.py @@ -1,22 +1,26 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from utilities import lower_than -from itertools import count, imap, takewhile +from itertools import count, takewhile -from functools import lru_cache +from utilities import lower_than limit = 1e6 +cache = {1: 1} + -@lru_cache def collatz_len(n): + try: + return cache[n] + except KeyError: + retval = 1 + collatz_len((3 * n + 1) if (n % 2) else (n / 2)) + cache[n] = retval + return retval - if n == 1: - return 1 - else: - return 1 + collatz_len((3*n + 1) if (n % 2) else (n / 2)) +gen = ((n, collatz_len(n)) for n in takewhile(lower_than(limit), count(1))) +answer = max(gen, key=lambda t: t[1])[0] +assert answer == 837799 -answer = max(imap(collatz_len, takewhile(lower_than(limit), count(start=1))), key=lambda t: t[1]) -print(answer) +print answer diff --git a/problem2.py b/problem2.py index 5caf5d0..0c42f98 100644 --- a/problem2.py +++ b/problem2.py @@ -1,9 +1,11 @@ """ -Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: +Each new term in the Fibonacci sequence is generated by adding the previous two terms. +By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... -By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. +By considering the terms in the Fibonacci sequence whose values do not exceed four million, +find the sum of the even-valued terms. """ import itertools @@ -15,5 +17,5 @@ seq = itertools.takewhile(lower_than(LIMIT), fibonacci()) answer = sum(itertools.ifilter(lambda n: (n % 2) == 0, seq)) +assert answer == 4613732 print answer - diff --git a/problem23.py b/problem23.py index 87aab88..81b1af9 100644 --- a/problem23.py +++ b/problem23.py @@ -48,4 +48,7 @@ def is_sum_of_abundants(n): if __name__ == '__main__': numbers = xrange(upper_limit + 1) - print sum(n for n in numbers if not is_sum_of_abundants(n)) + answer = sum(n for n in numbers if not is_sum_of_abundants(n)) + + assert answer == 4179871 + print answer diff --git a/problem25.py b/problem25.py index 0d10aab..25aa81c 100644 --- a/problem25.py +++ b/problem25.py @@ -8,7 +8,8 @@ for i, f in enumerate(fibonacci()): if f >= M: + # The +1 is necessary because we index from 0, math starts from 1 + answer = i + 1 + assert answer == 4782 + print answer break - -# The +1 is necessary because we index from 0, math starts from 1 -print i + 1 diff --git a/problem3.py b/problem3.py index c3a4f93..cd530dd 100644 --- a/problem3.py +++ b/problem3.py @@ -20,4 +20,5 @@ if not number % p: answer = p +assert answer == 6857 print answer diff --git a/problem30.py b/problem30.py index 77206cb..1fe646e 100644 --- a/problem30.py +++ b/problem30.py @@ -37,4 +37,7 @@ def sum_fifth_powers(number): solutions.add(n) print solutions - print sum(solutions) + answer = sum(solutions) + + assert answer == 443839 + print answer diff --git a/problem35.py b/problem35.py index cc6586d..15c3766 100644 --- a/problem35.py +++ b/problem35.py @@ -35,8 +35,11 @@ def is_circular_prime(n): if __name__ == '__main__': + count = 0 for n in xrange(2, int(1e6)): if is_circular_prime(n): count += 1 + + assert count == 55 print count diff --git a/problem41.py b/problem41.py index 9304039..c4106b8 100644 --- a/problem41.py +++ b/problem41.py @@ -41,5 +41,6 @@ def largest_pandigital_prime(n): for n in candidate_n: p = largest_pandigital_prime(n) if p: + assert p == 7652413 print p break diff --git a/problem7.py b/problem7.py index 2db56d4..7f3991d 100644 --- a/problem7.py +++ b/problem7.py @@ -2,11 +2,11 @@ # -*- coding: utf-8 -*- import itertools -from utilities import primes +from utilities import primes N = int(1e4) answer = next(itertools.islice(primes(), N, N + 1)) +assert answer == 104743 print answer - diff --git a/problem79.py b/problem79.py index 438cf6d..4314914 100644 --- a/problem79.py +++ b/problem79.py @@ -47,4 +47,5 @@ def remove_value(dictionary, value): remove_value(digits_preceding, first) solution += first + assert solution == '73162890' print solution diff --git a/problem9.py b/problem9.py index ab32c6d..6ab0316 100644 --- a/problem9.py +++ b/problem9.py @@ -1,10 +1,10 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from utilities import lower_than import itertools import math +from utilities import lower_than N = 1000 @@ -15,10 +15,9 @@ def is_perfect_square(n): def find_tuple(): - - for a in itertools.takewhile(lower_than(N-1), itertools.count(1)): - for b in itertools.takewhile(lower_than(N-a), itertools.count(a+1)): - c = math.sqrt(a*a + b*b) + for a in itertools.takewhile(lower_than(N - 1), itertools.count(1)): + for b in itertools.takewhile(lower_than(N - a), itertools.count(a + 1)): + c = math.sqrt(a * a + b * b) if a + b + c == N: return a, b, int(c) else: @@ -27,9 +26,10 @@ def find_tuple(): a, b, c = find_tuple() -assert a*a + b*b == c*c +assert a * a + b * b == c * c assert a + b + c == N print a, b, c -print a * b * c +assert a * b * c == 31875000 +print a * b * c