def memo_is_prime(n): if n in primes: return True if is_prime(n): primes.add(n) return True return False
# -*- coding: utf-8 -*- """ Created on Mon Oct 30 15:36:05 2017 @author: Edward We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? """ import itertools from myutils import is_prime largest = 0 for i in range(2, 11): vals = [i for i in range(1, i)] for permute in itertools.permutations(vals): val = sum([a * 10**b for b, a in enumerate(permute[::-1])]) if is_prime(val): if val > largest: largest = val print(largest)
# -*- coding: utf-8 -*- """ Created on Mon Oct 30 21:05:29 2017 @author: Edward The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence? """ from myutils import is_prime primes = set() for i in range(1001, 10000, 2): if is_prime(i): primes.add(i) for prime in primes: for i in range(1, 10000 - prime): if prime + i in primes and prime + 2 * i in primes and sorted( str(prime + i)) == sorted(str(prime)) and sorted( str(prime + 2 * i)) == sorted(str(prime)): print(prime, prime + i, prime + 2 * i, i)
ind = 1 next_size = 3 while True: num_vals = (next_size - 2) * 4 + 4 values = list( range(ind + next_size - 1, num_vals + ind + 1, next_size - 1)) ind = values[-1] next_size += 2 yield values num_primes = 0 total = 1 for size, vals in zip(range(1, 100001, 2), triangle_generator()): if vals == [1]: continue total += len(vals) for v in vals: if is_prime(v): num_primes += 1 if num_primes / total < 0.1: print(size) break