Example #1
0
def solve(n=10**6):
    # get a list of the primes below n
    start = time.time()
    primes = eratosthenes(n)
    primest = time.time()
    # every prime contianing an even digit with the exception of 2 is not
    # circular
    mark = lambda x : x == 2 or all(c not in str(x) for c in map(str, [0,2,4,6,8]))
    # mark all potentially safe primes
    sprimes = filter(mark, primes)
    markt = time.time()
    circs = {}
    count = 0
    for p in sprimes:
        if p not in circs:
            val = all(x in sprimes for x in rots(p))
            for x in rots(p):
                circs[x] = val
        if circs[p]:
            count += 1
    end = time.time()
    print ("Solution to PE35: {0}.\nSieve populated in {1}.\nUnsafe primes" +
            " filtered in {2}.\nCircular primes found in {3}.\nTot" +
            " time: {4}.").format(count, primest - start, markt - primest, end - markt,
                                 end - start)
Example #2
0
def solve():
    encountered = set()
    primes      = eratosthenes(10**4)
    relevant    = set(filter(lambda x : x > 10**3, primes))
    ctr = 0
    for p in relevant:
        # skip primes that you already have dealt with
        if p in encountered:
            continue
        seq = set()
        for q in perms(p):
            if q in relevant:
                encountered.add(q)
                seq.add(q)
        # don't bother if less then 3 perms of p are primes
        if len(seq) >= 3:
            seq = sorted(seq)
            for sols in indices(len(seq), 3):
                i,j,k = map(int, sols)
                if seq[k] - seq[j] == seq[j] - seq[i]:
                    print (seq[i], seq[j], seq[k])
                    ctr += 1
                    if ctr == 2:
                        return
Example #3
0
from utils.sieve import eratosthenes
from itertools   import combinations
# generates an index list from [0...x-1] of the size y
indices = lambda x, y: combinations(map(str, range(x)), y)

# generate the family from a template
def get_fam(temp):
    rs  = set()
    for i in range(0 if temp[0] != '*' else 1, 10):
        rs.add(int(temp.replace('*', str(i))))
    return rs

primes   = set(eratosthenes(10**6))

def solve():
    # generate templates (the last digit should always be odd)
    for t in range(101,1000, 2):
        for c in indices(5,2):
            a = ['*']*6
            i, j = map(int,c)
            # last digit cannot be a `*`, or at least 4 number wd be composite
            a[i], a[j], a[5] = list(str(t))
            # assemble string
            temp = "".join(a)
            fam = get_fam(temp)
            if len(fam.intersection(primes)) >= 8:
                print fam.intersection(primes)
                print "Answer to PE51 is {0}".format(min(fam.intersection(primes)))
                return min(fam.intersection(primes))
solve()