In [3]:
import itertools
import math
from collections import Counter

In [63]:
def rotations(n): 
    nstr = str(n)
    return set([ int(nstr[-i:] + nstr[:-i]) for i in range(len(nstr))])

In [64]:
from math import sqrt, ceil
import numpy as np

def rwh_primes(n):
    # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
    """ Returns  a list of primes < n """
    sieve = [True] * n
    for i in xrange(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
    return [2] + [i for i in xrange(3,n,2) if sieve[i]]

In [65]:
rwh_primes(10)


Out[65]:
[2, 3, 5, 7]

In [66]:
def count_circular_primes(n): 
    primes = set(rwh_primes(n))
    return len(filter(lambda x: rotations(x).issubset(primes), primes))

In [67]:
answer_31 = count_circular_primes(1000000)
print answer_31


55

In [68]:
squares = set([x**2 for x in range(1, 501)])

In [69]:


In [70]:
pairs = filter(lambda x: abs(x[0] - x[1]) in squares, itertools.combinations(squares, 2))

In [71]:
pairs = [(max(x[0], x[1]), min(x[0], x[1])) for x in pairs]

In [72]:
triplets = [(x[0], x[1], x[0] - x[1]) for x in pairs]

In [74]:
eligible = filter(lambda x: x <= 1000, [sum([int(math.sqrt(x)) for x in y]) for y in triplets])

In [1]:
data = Counter(eligible)
answer_39 = data.most_common(1)[0][0]
print answer_39


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-a23cfcbac3cf> in <module>()
----> 1 data = Counter(eligible)
      2 answer_39 = data.most_common(1)[0][0]
      3 print answer_39

NameError: name 'Counter' is not defined

In [5]:
# Problem 43
def works(x):
    return int(x[3]) % 2 == 0 and int(''.join(x[2:5])) % 3 == 0 and int(x[5]) % 5 == 0 and int(''.join(x[4:7])) % 7 == 0 and int(''.join(x[5:8])) % 11 == 0 and int(''.join(x[6:9])) % 13 == 0 and int(''.join(x[7:10])) % 17 == 0

sum([int(''.join(x)) for x in itertools.permutations("0123456789", 10) if x[0] != '0' and works(x)])


Out[5]:
16695334890

In [ ]: