def solve(cur_list): print cur_list if len(cur_list) == LIST_LENGTH: print cur_list, sum(cur_list) exit() start = 1 if cur_list == [] else inb(cur_list[-1], p) + 1 for i in p[start:MAX]: recurse = True for j in cur_list: pairing = int(str(j) + str(i)) if pair_dict.get(pairing) != None: recurse = False break if recurse: solve(cur_list + [i])
It can be seen that there are 21 elements in this set. How many elements would be contained in the set of reduced proper fractions for d <= 1,000,000? """ # return int(n * reduce(lambda x,y: x*y, [1-(1./i) for i in pfact])) from util import inb, pf, primes, totient from math import sqrt end = 10 ** 6 ps = primes(end) ps_sq = primes(int(sqrt(end))) pfacts = {} # print ps, inb(2, ps) for i in xrange(2, end + 1): if inb(i, ps) != -1: pfacts[i] = (set([i]), i - 1) else: for prime in ps_sq: if i % prime == 0: pf, totient = pfacts[i / prime] if prime in pf: pfacts[i] = (pf, totient * prime) else: pfacts[i] = (pf.union(set([prime])), totient * (prime - 1)) break print(sum([pfacts[i][1] for i in xrange(2, end + 1)]))
#! /usr/bin/python from util import primes from util import inb p = primes(1000000) s = [] res = 21 for i in range(len(p)): s.append(sum(p[i:i+res])) if s[i] > 1000000: break while s != []: res += 2 for i in range(len(s)): s[i] += p[i+res-1] + p[i+res-2] if s[i] > 1000000: s = s[:i] break if inb(s[i],p): print s[i],res
#! /usr/bin/python from util import primes from util import inb p = primes(1000000) s = [] res = 21 for i in range(len(p)): s.append(sum(p[i:i + res])) if s[i] > 1000000: break while s != []: res += 2 for i in range(len(s)): s[i] += p[i + res - 1] + p[i + res - 2] if s[i] > 1000000: s = s[:i] break if inb(s[i], p): print s[i], res
print "Done\n" pair_dict = {} # Use a simple recursive backtracking algorithm def solve(cur_list): print cur_list if len(cur_list) == LIST_LENGTH: print cur_list, sum(cur_list) exit() start = 1 if cur_list == [] else inb(cur_list[-1], p) + 1 for i in p[start:MAX]: recurse = True for j in cur_list: pairing = int(str(j) + str(i)) if pair_dict.get(pairing) != None: recurse = False break if recurse: solve(cur_list + [i]) # Precompute pairs of primes that when concatenated together produce other primes for i in range(1,MAX): print "%d/%d" % (i,MAX) for j in range(i+1,MAX): concat = (int(str(p[i]) + str(p[j])), int(str(p[j]) + str(p[i]))) if ((concat[0] < PRIMES_TO) and (len([k for k in concat if inb(k,p) != False]) != 2)) or (len([k for k in concat if is_prime(k,p) != False]) != 2): pair_dict[concat[0]] = True solve([])