def mirror_str_test(a, b): ab = a + b if len(ab) > 5: if is_prime(int(ab)) and is_prime(int(b + a)): return True elif ab in primes and b + a in primes: return True return False
def prime_summations(n): ''' problem 77 ''' prime_sums, primes = {0:1,1:0}, [] for i in range(2,n): if is_prime(i): primes.append(i) return prime_sums
def pandigital_prime(num_digits = 4): ''' find largest number that uses all digits 1 through num_digits and is prime problem 41 ''' for num_d in range(num_digits,0,-1): digits = [str(d) for d in range(1,num_d+1)] perms = itertools.permutations(''.join(reversed(digits))) for i in perms: num = int(''.join(i)) if is_prime(num): return num
def truncatable_primes(maximum = 1000000): ''' find all eleven 'truncatable' primes problem 37 ''' poss_digits = set('1379') all_primes = get_all_primes_under(maximum) poss_primes = [p for p in [str(p) for p in all_primes] if set(p[1:]) <= poss_digits] #primes can only be composed of the digits 1,3,7, and 9 #after the first digit final_primes = [] for p in poss_primes: found = True length = len(p) for i in range(0,length): if not is_prime(int(p[i:])) or not is_prime(int(p[:i+1])): found = False break if found: final_primes.append(int(p)) return final_primes
def is_circular_prime(n): ''' return whether circle prime or not problem 35 ''' if type(n) != int: raise TypeError if not is_prime(n): return False s = str(n) length = len(s) i = 0 while i < length: i += 1 if not is_prime(int(rotate_string(s,i))): return False return True
def alternate_prime_sum(): ps=get_all_primes_under(3946) mt,ms,mcps=len(ps),sum(ps),(0,0) while mt>mcps[0]: ls=ms if ms%2 else ms-1 while not is_prime(ls): ls-=2 k,ts=0,ms while ts-ls>0: ts-=ps[k]; k+=1 if ts==ls and mt-k>mcps[0]: mcps=(mt-k,ts) mt-=1 ms-=ps[mt] print(mcps)
def quadratic_prime_with_a_and_b_under(maximum): ''' returns a list of tuple (a,b) from 'n^2 + an + b' and an integer representing how effective that quad is at predicting primes for consecutive n values. b must be a prime (0 + 0a + b) if at all effective, and a+b+1 must be a prime to get 2 problem 27 ''' primes_under_max = get_all_primes_under(maximum) products = itertools.product(range(-maximum,maximum+1),primes_under_max) terms = {(a,b):2 for (a,b) in products if a+b+1 in primes_under_max or is_prime(a+b+1)} for (a,b),num in terms.items(): n = 2 q = get_quadratic(n,a,b) while q in primes_under_max if q < maximum else is_prime(q): n += 1 q = get_quadratic(n,a,b) terms[(a,b)] = n-1 return terms
def main(): start = time.time() primes = list() p = 2 limit = 2*10**6 while p <=limit: if problem_7.is_prime(p): primes.append(p) p +=1 print sum(primes) end = time.time() print "time: %s" % (end-start)
def spiral_prime_ratio(i = 10): ''' find the first ring of spiral prime at which the ratio of primes is less than given % problem 58 ''' percent = 100/i primes = 0 corners = 1 for i in range(2,10**6,2): side = i+1 square = side**2 primes += len([c for c in range(1,4) if is_prime(square-(c*i))]) corners += 4 if percent*primes < corners: return (side,primes,corners)
def prime_summations(limit): primes = [] sums = defaultdict(int) n = 0 while True: if is_prime(n): sums[n] += 1 primes.append(n) sums[n] += sum(sums[n-k] for k in primes if k <= n / 2) print sums if n == 7: print [(n-k, sums[n-k]) for k in primes if k <= n / 2] if sums[n] >= limit: return n n += 1
def get_circular_primes_under(maximum): ''' find all circular primes under a given maximum problem 35 ''' if type(maximum) != int: raise TypeError sieve = {d:0 for d in range(1,maximum+1)} for (i,v) in sieve.items(): if v != 0: continue strung = str(i) length = len(strung) if '0' in strung: possibilities = [i] + [int(rotate_string(strung,j)) for j in range(1,length) if rotate_string(strung,j)[0] != '0'] else: possibilities = [i]+[int(rotate_string(strung,j)) for j in range(1,length)] found = 1 if len([p for p in possibilities if is_prime(p)]) == length else -1 sieve.update({p:found for p in possibilities}) return [k for (k,v) in sieve.items() if v == 1]