def solve(num,remaining): #base case. #Recheck that the number is valid if len(num) == 10 and len(remaining) == 0: valid = True primes = cf.generate_primes_less_than(20) #this loop makes sure that each candidate substring is divisible by #the prime as defined by the problem (d2d3d4 % 2 == 0 and d3d4d5 %3 == 0, etc...) for j in range(1,7): substr = num[j:j+3] prime = primes[j-1] if int(substr) % prime != 0: valid = False break #if indeed we pass the check, return the number if valid: return int(num) else: return 0 #as we flesh out the number, make sure it's valid before we move onto #further ones. sum = 0 for i in range(len(remaining)): temp = num + remaining[i] length = len(temp) if temp[0] != '0' and (length < 4 or length == 4 and int(temp[3]) % 2 == 0 or length == 5 and int(temp[2:5]) % 3 == 0 or length == 6 and int(temp[3:6]) % 5 == 0 or length == 7 and int(temp[4:7]) % 7 == 0 or length == 8 and int(temp[5:8]) % 11 == 0 or length == 9 and int(temp[6:9]) % 13 == 0 or length == 10 and int(temp[7:10]) % 17 == 0): sum += solve(temp, remaining[0:i] + remaining[i+1:len(remaining)]) return sum
""" Created on 2013-01-23 @author: paymahn """ import CommonFunctions as cf import itertools as it import time t = time.time() primes = cf.generate_primes_less_than(10000) # generate primes less than 10000 primes = set(x for x in primes if x > 1000) # remove primes less than 1000 (we only have 4 digit primes now) for i in primes: permutations = [ cf.tuple_to_num(x) for x in it.permutations(str(i)) ] # get all the permutations of the digits of the current prime permutations = set( x for x in permutations if x in primes ) # remove duplicate permutations and non prime permutations triplets = [x for x in it.permutations(permutations, 3)] # it's hard to explain this one succintly, use a debugger for x in triplets: # test each triplet and print the triplet if it's valid sorted_triplet = sorted(x) if sorted_triplet[2] - sorted_triplet[1] == sorted_triplet[1] - sorted_triplet[0]: print sorted_triplet print time.time() - t
def replace(num, digit, locations): result = num for i in locations: result -= num % 10**(i+1) result += digit*10**i result += num % 10**i return result t = time() LIMIT = 1000000 primes = cf.generate_primes_less_than(LIMIT) prime_set = set(primes) valid_primes = [x for x in primes if x > 100109] for p in primes: length = int(math.log10(p)) + 1 for x in it.combinations(range(length),3): #not sure why the 3 works, but it does primes_created = [] for r in range(10): new_num = replace(p,r,x) #replaces digits indicated by x with the digit r in the number p if new_num > 0 and int(math.log10(new_num)) + 1 == length and new_num in prime_set: primes_created.append(new_num) if len(primes_created) == 8 and p in primes_created:
@author: paymahn ''' ''' f**k this, it's not working. I'm gonna try something different ''' import CommonFunctions as cf import itertools as it import time t = time.time() pandigitals = [cf.tuple_to_num(x) for x in it.permutations(range(10))] primes= cf.generate_primes_less_than(20) tally = 0 for i in pandigitals: i = str(i) if len(i) == 10: valid = True for j in range(1,7): substr = i[j:j+3] prime = primes[j-1] if int(substr) % prime != 0: valid = False break if valid: tally += int(i)
@author: paymahn ''' ''' Can't find the 11th prime ''' import CommonFunctions as cf import time import math t = time.time() primes = cf.generate_primes_less_than(1000000) #remove primes that have even numbers #turn into set for quick lookups usable_primes = set([x for x in primes if not cf.contains_even_digit(x)]) tally = 0 count = 0 for x in usable_primes: if x > 7 and count < 11: #test left trucation left = True temp = x
''' Created on Jan 18, 2013 @author: PaymahnMoghadasian ''' # this was made better by implement the sieve of Erastothene in CommonFunctions.py import CommonFunctions as cf print sum(cf.generate_primes_less_than(2000000))
''' Created on 2013-01-22 @author: paymahn ''' import itertools as it import CommonFunctions as cf pan9 = [int(''.join(map(str,x))) for x in it.permutations(range(1,8),7)] pan9.sort(reverse=True) primes = cf.generate_primes_less_than(int(7654321**0.5)) for i in pan9: if cf.is_prime(i, primes): print i break print "finished"
''' Note that b must be prime and positive (I'm not sure if there's such a thing as a negative prime) |a| must be bigger than b because primes can't be negative. If |a| < b and b is prime then f(1) is negative, which is not prime EX: let f(n) = n*n + a*n + b f(1) = 1 + a + b If |a| < b then f(1) < 2 ''' import CommonFunctions as cf import time t = time.time() values_of_b = cf.generate_primes_less_than(1000) primes = set(cf.generate_n_primes(300)) best_a, best_b = None, None best_n = 0 print time.time() - t for b in values_of_b: for a in range(-b + 1, 10000): n = 0 while n * n + a * n + b in primes: n += 1 if n > best_n: best_n = n best_a = a
''' Created on 2013-01-21 @author: paymahn ''' import CommonFunctions as cf import math primes = set(cf.generate_primes_less_than(1000000)) count = 0 for i in primes: #print "Testing prime", i len = int(math.log10(i) + 1) temp = i valid = True for j in range(len): temp = (temp % 10**(len-1)) * 10 + temp/10**(len-1) if temp not in primes: valid = False break if valid: print i count += 1 print count
''' Created on 2013-01-23 @author: paymahn ''' import CommonFunctions as cf MAX = 10000 primes = set(cf.generate_primes_less_than(MAX)) odd_composite = [x for x in range(1,MAX) if x%2==1 and x not in primes and x > 1] squares = [x*x for x in range(1,MAX) if x*x <= MAX/2] for i in odd_composite: goldbach = False for j in squares: if i - 2*j in primes and not goldbach: goldbach = True break if not goldbach: print i break