Skip to content

Commit

Permalink
Refactors files, adds asserts with the solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
mippolito committed Mar 7, 2017
1 parent 5c6d0d3 commit b20cfb9
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 33 deletions.
11 changes: 5 additions & 6 deletions problem1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
233168
"""


import itertools
from utilities import lower_than

from utilities import lower_than

LIMIT=1000
ANSWER=233168
LIMIT = 1000
ANSWER = 233168


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))
Expand All @@ -31,4 +31,3 @@ def multiples(n):
assert answer == ANSWER

print answer

24 changes: 14 additions & 10 deletions problem14.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

5 changes: 4 additions & 1 deletion problem23.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions problem25.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions problem3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
if not number % p:
answer = p

assert answer == 6857
print answer
5 changes: 4 additions & 1 deletion problem30.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions problem35.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions problem41.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions problem7.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 change: 1 addition & 0 deletions problem79.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ def remove_value(dictionary, value):
remove_value(digits_preceding, first)
solution += first

assert solution == '73162890'
print solution
14 changes: 7 additions & 7 deletions problem9.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand All @@ -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

0 comments on commit b20cfb9

Please sign in to comment.