Пример #1
0
import sys
from itertools import *
import c_prime

primes = c_prime.get_primes(10000)
#primes = c_prime.get_primes(10000000)

primes_s = set(primes)

cache = {}
def check(a, b):
#    print a, b
    if (a,b) in cache:
        return cache[(a,b)]
        
    else:
        p1 = int(str(a)+str(b))
        p2 = int(str(b)+str(a))
    
        retval =(p1 in primes_s and p2 in primes_s) or (c_prime.is_prime(p1) and c_prime.is_prime(p2))
        cache[(a,b)] = retval
        return retval
        
        
    

range_max = len(primes)

sol_sum_min = None
sol_min = None
Пример #2
0
from itertools import islice
import c_prime

PRIME_MAX = 1000000
primes = c_prime.get_primes(PRIME_MAX, set)

def quad(a, b):
    n = 0
    while True:
        yield n**2+a*n+b
        n=n+1
        
#print primes
sol_max = 0

for a in xrange(-1000, 1001):
    for b in xrange(-1000, 1001):
        sol = 0
        for idx, n in enumerate(quad(a, b)):
#            print idx, n
            if n > PRIME_MAX:
                raise ValueError(str(n))

            if n<1:
                break
                            
            if n not in primes:
                sol = idx
                break
Пример #3
0

import c_prime
from itertools import permutations

primes = [x for x in c_prime.get_primes(10000) if x>1000]
primes_set = set(primes)

solved = False
for prime in primes:
#    print prime
    for digits in permutations(map(int, str(prime)), 4):
        if digits[0] == 0 or digits[-1]%2 == 0:
            continue
        num = digits[0]*1000 + digits[1]*100 + digits[2]*10 + digits[3]
        if num == prime:
            continue
        if num<prime:
            continue
             
        if num in primes_set and prime+(num-prime)*2 in primes_set and set(str(prime)) == set(str(prime+(num-prime)*2)):
            print "found", prime, num, prime+(num-prime)*2, num-prime, str(prime)+str(num)+str(prime+(num-prime)*2)
            break
            



    
Пример #4
0
import c_prime
from itertools import product

primes = [x for x in c_prime.get_primes(1000000)]
primes_set = set(map(str, primes))

digits_s = map(str, range(10))

solved = False

# very brute force
# plenty of spaces for improvements

for prime in primes:
    prime_s = str(prime)
    for src in sorted(set(prime_s)):
        if prime_s[0] == src:
            dsts = digits_s[1:]
        else:
            dsts = digits_s[:]
        
        dsts.remove(src)
        cnt = 1
        for dst in dsts:
            prime_tmp = prime_s.replace(src, dst)
            if prime_tmp in primes_set:
                cnt += 1
        if cnt>=8:
            print prime, src, cnt
            print "--"