def pe41(): """ >>> pe41() 7652413 """ primes = Primes(1000000) for perm in permutations(range(7, 0, -1)): n = list_num(perm) if primes.is_prime(n): return n return -1
def pe49(limit=10000): """ >>> pe49() (2969, 6299, 9629) """ pr = Primes() for num in range(1111, limit): sn = str(num) if sn.find('0') >= 0: continue if pr.is_prime(num): pp = {num : 1} for p in permutations(list(sn)): n = int(''.join(p)) if pr.is_prime(n): pp[n] = 1 primes = sorted(pp.keys()) pl = len(primes) for a in range(pl): if primes[a] == 1487: continue for b in range(a+1, pl): c = (primes[a] + primes[b]) >> 1 if c in pp: return (primes[a], c, primes[b]) return None
def pe27(): """ >>> pe27() (-59231, (-61, 971, 71)) """ m = (0, 0, 0) primes = Primes(15000) for a in range(-999, 1000): for b in range(max(2, 1 - a), 1000): n, cnt = 0, 0 while 1: p = n * (n + a) + b if primes.is_prime(p): cnt += 1 else: break n += 1 if cnt > m[2]: m = (a, b, cnt) return (m[0] * m[1], m)
def pe27(): """ >>> pe27() (-59231, (-61, 971, 71)) """ m = (0, 0, 0) primes = Primes(15000) for a in range(-999, 1000): for b in range(max(2, 1 - a), 1000): n, cnt = 0, 0 while 1: p = n * (n + a) + b if primes.is_prime(p): cnt += 1 else: break n += 1 if cnt > m[2]: m = (a, b, cnt) return (m[0] * m[1], m)
from primes import Primes n = i = 1 p = Primes() percent = 1 diagonals = 1 # this takes into account the center (1) prime_diagonals = 0 while percent > 0.1: for x in range(4): n+=2*i prime_diagonals += p.is_prime(n) diagonals += 4 i+=1 percent = float(prime_diagonals) / diagonals print prime_diagonals, '/', diagonals, ',', percent print 'side =', int(n**(0.5)), 'primes =', prime_diagonals, '/', diagonals, percent
""" project euler #77 matt donahoe i enjoyed thinking of this one. did some back of the envelope (literally) discovered a dynamic programming solution """ from primes import Primes p=Primes(5000) ways = {} #ways.get((n,p),0) = number of ways to sum to n using only primes >= p primes = [] n=1 while s<=50000000: n+=1 s = 0 if p.is_prime(n): s = 1 primes.append(n) for x in primes[::-1]: s+=ways.get((n-x,x),0) ways[(n,x)] = s print n,s
# For the moment the maximum sequence has 0 length # Each time we find a longer sequence we will record it here # together with its parameters (point in the square, ...) maxi = {'n': 0} for a, b in get_coeffs(N): i = a + N - 1 # indices in the matrix j = b + N - 1 f = make_fun(a, b) # how many primes does f produces? n = 0 while True: p = f(n) if not primes.is_prime(p): break else: pass n += 1 # records number of primes in the matrix matrix[i][j] = n # if we have found a better sequence, we save it in 'maxi' if n > maxi['n']: maxi = {'n': n, 'f': f} print(f.format(), " ------> ", maxi['n']) # print ("===============================================================", a, b, n)
# For the moment the maximum sequence has 0 length # Each time we find a longer sequence we will record it here # together with its parameters (point in the square, ...) maxi = {'n': 0} for a, b in get_coeffs(N): i = a + N - 1 # indices in the matrix j = b + N - 1 f = make_fun(a, b) # how many primes does f produces? n = 0 while True: p = f(n) if not primes.is_prime(p): break else: pass n += 1 # records number of primes in the matrix matrix[i][j] = n # if we have found a better sequence, we save it in 'maxi' if n > maxi['n']: maxi = {'n': n, 'f': f} print (f.format(), " ------> ", maxi['n']) # print ("===============================================================", a, b, n)