def substring_divisible(x): """ Determine if number is substring divisible check that each 3 digit part is divisible by all the primes up to 18 :param x: :return: """ # iterate through successive three digits and check if they # can be divided by successively larger primes for i, j in zip(sieve_of_eratosthenes(18), range(1, 8)): if int(x[j:j + 3]) % i == 0: continue else: return False return True
from problem07 import sieve_of_eratosthenes if __name__ == "__main__": """ 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? https://radiusofcircle.blogspot.com/2016/06/problem-49-project-euler-solution-with-python.html """ # 4 digit primes primes = [ prime for prime in sieve_of_eratosthenes(10000) if len(list(str(prime))) == 4 and prime > 1487 ] for key, prime in enumerate(primes): # get list of permutations, and convert to ints (permutations() returns list of strings) permutation_numbers = [ int(''.join(x)) for x in itertools.permutations(str(prime)) if int(''.join(x)) in primes ] # get rid of non prime permutations permutation_numbers = list( set([number for number in permutation_numbers if number in primes])) # check that they all of the same difference? for i in range(len(permutation_numbers)):
def primes_below(n): return sieve_of_eratosthenes(n)
import time from problem07 import sieve_of_eratosthenes from problem32 import is_pandigital if __name__ == "__main__": start_time = time.time() primes = sieve_of_eratosthenes(7654321) pandigital_primes = [] for prime in primes: if is_pandigital(prime, len(str(prime))): pandigital_primes.append(prime) print(max(pandigital_primes), time.time() - start_time)
import time from problem07 import sieve_of_eratosthenes if __name__ == "__main__": """ The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. How many circular primes are there below one million? https://radiusofcircle.blogspot.com/2016/05/problem-35-project-euler-solution-with-python.html """ start_time = time.time() primes = sieve_of_eratosthenes(1000000) count = 0 for prime in primes: # assuming that there are no non-prime digits (2, 4, 6, 8, 5, 0) all_prime_digits = True # start with tens digit number = prime // 10 # looping through digits while number: if (number % 10) % 2 == 0 or (number % 10) % 5 == 0: all_prime_digits = False break number //= 10 if not all_prime_digits: continue length = len(str(prime)) number = prime