def primes_table_solution(): """Compute a primes table up to the LIMIT and use that to quickly check rotations.""" primes = utils.sieve_of_eratosthenes(LIMIT) circular_primes = [ i for i in range(2, LIMIT) if all(rotation in primes for rotation in rotations(i)) ] return len(circular_primes)
def sieve_solution(): """ Use Sieve of Eratosthenes to compute primes up to 7 digits and then test for pandigital. The 3 divisibility rule states that an number whose digits sum to a number that is divisible by 3 then the whole number is divisible by 3. These reduces the search space to a max of 7 digit numbers: 1+2+3+4+5+6+7+8+9=45 -> All 9 digit pandigital numbers are divisible by 3 1+2+3+4+5+6+7+8=36 -> All 8 digit pandigital numberes are divisible by 3 """ primes = utils.sieve_of_eratosthenes(10000000) return next( prime for prime in sorted(utils.sieve_of_eratosthenes(10000000) , reverse=True) if utils.is_pandigital(prime) )
def tuned_sieve_solution(): """Using a priori knowledge of the solution to set sieve limit for highest performance.""" return sorted(list(utils.sieve_of_eratosthenes(104744)))[NTH_PRIME_NUMBER - 1]
def untuned_sieve_solution(): """Guessing large sieve limit in hopes that it contains the NTH_PRIME_NUMBER.""" return sorted(list( utils.sieve_of_eratosthenes(1000000)))[NTH_PRIME_NUMBER - 1]
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? """ import itertools from project_euler import utils FOUR_DIGIT_PRIMES = [ prime for prime in utils.sieve_of_eratosthenes(10000) if prime > 999 ] def prime_permutations(value): """Return all permutations of digits in value which are prime.""" return list( set([ int(''.join(perm)) for perm in itertools.permutations(str(value)) if int(''.join(perm)) in FOUR_DIGIT_PRIMES ])) def linearly_increasing(values): """Return True if values are linearly increasing.""" return all(values[i] - values[i - 1] == values[1] - values[0]
def sieve_solution(): """Use sieve of eratosthenes to get sum of all primes below LIMIT.""" return sum(utils.sieve_of_eratosthenes(LIMIT))
The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? """ from project_euler import utils LIMIT = 1000000 PRIME_SET = utils.sieve_of_eratosthenes(LIMIT) # fast membership tests PRIME_LIST = list(PRIME_SET) def max_prime_sequence(start): """Return consecutive prime sequence which sums to a prime number.""" sequence = [] seq_len, seq_sum = 1, PRIME_LIST[start] for i in range(start + 1, len(PRIME_LIST)): seq_sum += PRIME_LIST[i] seq_len += 1 if seq_sum > LIMIT: break if seq_sum in PRIME_SET: