Example #1
0
def solve():
    sieve = Sieve()
    sieve.compute_upto(100000) # prime the sieve

    # we're starting with 23 to make life easy
    found_soln = False
    primes = (i for i in count(23) if sieve.is_prime(i))
    digits = ''.join(str(d) for d in range(0,10)) # 0-9
    for p in primes:
        p_str = str(p)
        for i in xrange(1,len(p_str)):
            # combinations of indices into p_str of length i where every value index in
            # p_str has the same character
            idx_combs = (j for j in combinations(xrange(0,len(p_str)), i)
                         if all(imap(lambda x: p_str[x] == p_str[j[0]], j)) and len(j) > 0)
            for comb in idx_combs:
                group_members = set([])
                # we've got all the possible index combinations, so let's try swapping them
                for new_char in digits:
                    test_str = set_val_at_idxs(p_str, comb, new_char)
                    val = int(test_str)
                    if not test_str.startswith('0') and sieve.is_prime(val):
                        group_members.add(val)
                if len(group_members) >= 7:
                    print sorted(group_members)
                if len(group_members) == 8:
                    found_soln = True
                    break;
            if found_soln:
                break
        if found_soln:
            break
Example #2
0
def problem46():
    s = Sieve()
    odd_composites = (i for i in counter(2) if not s.is_prime(i) and i % 2 == 1)
    for c in odd_composites:
        print "c = %d" % c
        two_times_squares = (2 * j**2 for j in counter(1))
        for tts in two_times_squares:
            if s.is_prime(c - tts):
                break
            elif tts > c:
                # this should mean that we couldn't find a sum of a prime and twice a square
                print "{c = %d, tts = %d, c-tts = %d}" % (c, tts, c-tts)
                return c
Example #3
0
def problem49():
    s = Sieve()
    four_digit_primes = (i for i in xrange(1000, 9999+1) if s.is_prime(i))
    grouped_primes = {}
    for i in four_digit_primes:
        # the key into the map will be a sorted representation of
        # the digits in 'i'
        key = ''.join(sorted(str(i)))
        if not key in grouped_primes:
            # create a list at that key
            grouped_primes[key] = []
        grouped_primes[key].append(i)
    prime_groups_bigger_than_three = (grouped_primes[k] for k in grouped_primes if len(grouped_primes[k]) >= 3)
    for grp in prime_groups_bigger_than_three:
        diff_counts = {}
        pairwise_diffs = [(grp[i],grp[j],grp[j]-grp[i]) for i in xrange(0,len(grp)) for j in xrange(i+1,len(grp))]
        for p1,p2,diff in pairwise_diffs:
            if diff not in diff_counts:
                diff_counts[diff] = 0
            diff_counts[diff]+=1
        solution_diffs = (k for k in diff_counts if diff_counts[k] == 2)
        for s in solution_diffs:
            # there should only be two of these! (one should be the example)
            pairs = filter(lambda x: x[2] == s, pairwise_diffs)
            if pairs[0][1] == pairs[1][0] and pairs[0][0] != 1487:
                return ''.join(map(str, [pairs[0][0], pairs[0][1], pairs[1][1]]))
    return None
Example #4
0
#!/usr/bin/env python

from Sieve import Sieve

upto = 2000000

s = Sieve(upto=2000000)

primes_to_2_mil = (i for i in xrange(2,upto) if s.is_prime(i))

print reduce(lambda x,y: x + y, primes_to_2_mil)