# The number 3797 has an interesting property. Being prime itself, it is # possible to continuously remove digits from left to right, and remain prime # at each stage: 3797, 797, 97, and 7. Similarly we can work from right to # left: 3797, 379, 37, and 3. # # Find the sum of the only eleven primes that are both truncatable from left # to right and right to left. # # NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. from p010 import prime_list3 from itertools import ifilter primes = prime_list3(1000000) prime_set = set(primes) def truncations(number): digits = str(number) for i in xrange(1,len(digits)): yield int(digits[:-i]) yield int(digits[i:]) def is_truncatable(number): if number not in prime_set: return False if any( trunc not in prime_set for trunc in truncations(number) ): return False return True
# product of the coefficients, -79 and 1601, is -126479. # Considering quadratics of the form: # n^(2) + an + b, where |a| < 1000 and |b| < 1000 # where |n| is the modulus/absolute value of n # e.g. |11| = 11 and |-4| = 4 # Find the product of the coefficients, a and b, for the quadratic expression # that produces the maximum number of primes for consecutive values of n, # starting with n = 0. from p010 import prime_list3 primes = set(prime_list3(79 ** 2 + 999 * 79 + 1000)) def quadratic_fn(a, b): def fn(n): return n ** 2 + (a * n) + b return fn class Quadratic(object): def __init__(self, a, b): self.a = a self.b = b self.fn = quadratic_fn(a, b)
def pandigital_primes(): biggest_pandigital = 7654321 primes = prime_list3(biggest_pandigital+1) for prime in ifilter(is_pandigital, primes): yield prime