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
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
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
def main(): # q = Decimal(0.065536) # r = Numeric.sqrt(q) # s = r * r # t = None if q == 0 else 100 * (q - s) / q # print('q = {}, r = {}, s = {}, t = {}'.format(q, r, s, t)) # return # 411.5 sec using integers n = 999983 n = 113 # results = Poly.pascalTest(n) # print(results) # return # isPrime = Poly.isPrimeTest(n) # print('isPrime({}) = {}'.format(n, isPrime)) # return if len(sys.argv) > 1: limit = int(sys.argv[1]) if limit <= 0: raise ValueError('limit must be positive') # sieve = Sieve() if len(sys.argv) > 2: last = int(sys.argv[2]) if last < 0: raise ValueError('limit must not be negative') sieve = Sieve() primes10 = sieve.testLast(limit, last) return # sieve.ntest(9) # return # 1 prime summing to 2 # sieve.test(3) # 4 primes summing to 17 sieve.test(11) # 9 primes summing to 100 sieve.test(25) # 25 primes summing to 1,060 # sieve.test(100) # 303 primes summing to 277,050 sieve.test(2000) # 78,498 primes summing to 37,550,402,023 sieve.test(1000000) # 148,933 primes summing to 142,913,828,922 # sieve.test(2000000) # sieve.test(10000000) # 664,579 summing to 3,203,324,994,356 # 5,761,455 summing to 279,209,790,387,276 # sieve.test(100000000) # rate can be 2 MHz sieve.test(limit)
#!/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)
def main(): s = Sieve(200) print s.findPrimes()