Example #1
0
def solve_p060(size):
    for n in Primes():
        sn = str(n)
        pp = []
        for p in pmap:
            if is_prime(int(sn + p)) and is_prime(int(p + sn)):
                pp.append(p)
        pmap[sn] = set(pp)
        
        for sol in iter_sol(sn, size):
            print ', '.join(sol)
            return sum(map(int, sol))
Example #2
0
def max_pan_prime():
    '''Returns the largest pandigital prime, or 0 if not found (it should exist
    according to the example given in the problem).'''
    for digits in [7, 4]:
        for x in itertools.permutations(range(digits, 0, -1)):
            y = int(''.join(map(str, x)))
            if is_prime(y):
                return y
    return 0
Example #3
0
def is_concat_set(S):
    for a, b in it.combinations(S, 2):
        # print '\t', concat(a, b), concat(b, a), is_prime(concat(a, b)), is_prime(concat(b, a))
        if not is_prime(concat(a, b)) or not is_prime(concat(b, a)):
            return False
    return True
Example #4
0
'''Concatenate the numbers a,b.'''
concat = lambda a, b: int(str(a) + str(b))

def is_concat_set(S):
    for a, b in it.combinations(S, 2):
        # print '\t', concat(a, b), concat(b, a), is_prime(concat(a, b)), is_prime(concat(b, a))
        if not is_prime(concat(a, b)) or not is_prime(concat(b, a)):
            return False
    return True

#--------------------------------------------
# Stack solution
#--------------------------------------------
'''x,y = prime strings. Is the pair x,y valid?'''
valid_pair = lambda x, y: is_prime(int(x + y)) and is_prime(int(y + x))

def valid_seq(a):
    '''Assuming a[:-1] is a valid seq, is a valid?'''
    x = str(a[-1])
    return all(valid_pair(x, str(y)) for y in a[:-1])

def _min_sum(k, num_primes):
    '''Return the min k-seq sum (k>=2). Search among the first num_primes (>=2) primes.'''
    p = primes('first', num_primes)[1:]  # Exclude 2 since we know it cannot be in a valid sequence
    a, d, smin, N, min_d = [0], 1, 0, len(p), 0  # a contains indices into p
    min_pa = []
    print 'N', N
    while True:
        pa = [p[x] for x in a]
        valid = valid_seq(pa)