# There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. # How many circular primes are there below one million? from time import time start = time() from module import sieve, is_prime def rotate(n: int) -> list: r = set() for i in range(len(str(n))): r.add(int(f'{str(n)[i:]}{str(n)[:i]}')) return r circular_primes = [] for n in sieve(2, 1000000): counter = 0 if '0' not in str(n): rotations = rotate(n) for r in rotations: if is_prime(r): counter += 1 if counter == len(rotations): circular_primes.append(n) print(len(circular_primes)) print(time() - start) # Answer: 55
# 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 time import time start = time() from module import sieve, is_prime interesting_primes = [] for n in sieve(20, 750000): n1 = n n2 = n # left counter = 1 if '0' not in str(n): for i in range(len(str(n1)) - 1): if is_prime(int(str(n1)[1:])): counter += 1 n1 = int(str(n1)[1:]) if counter == len(str(n)): # right counter = 1 for i in range(len(str(n2)) - 1): if is_prime(int(str(n2)[:-1])): counter += 1 n2 = int(str(n2)[:-1]) if counter == len(str(n)): interesting_primes.append(n)
# Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). # If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a # and b are called amicable numbers. # For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; # therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. # Evaluate the sum of all the amicable numbers under 10000. from module import divisors_n, sieve prime_nums = sieve(2, 10000) amicables = [] checked_nums = [] for i in range(2, 10000): if (i not in prime_nums and i not in checked_nums): da = sum(divisors_n(i)) db = sum(divisors_n(da)) checked_nums.extend([da, db]) if (i == db and da != db): amicables.extend([i, da]) print(int(sum(amicables))) # Answer: 31626
# 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 time import time start = time() from module import sieve, is_prime primes = sieve(2, 10000) fin_seq = [] l = len(primes) j = l while j != 0: i = 0 while i + j < l + 1: seq = primes[i:i + j] if sum(seq) <= 1_000_000: if is_prime(sum(seq)): if len(seq) > len(fin_seq): fin_seq = seq i += 1 j -= 1 print(sum(fin_seq)) print(time() - start) # Answer: 997651
# (n^2 + n + 41) # It turns out that the formula will produce 40 primes for the consecutive integer values 0 <= n <= 39. # However, when n = 40, 40^2 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41 # The incredible formula (n^2 -79n + 1601) was discovered, which produces 80 primes for the consecutive values 0 <= n <= 79 # The product of the coefficients, −79 and 1601, is −126479. # 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 time import time from module import sieve, is_prime start = time() # find primes up to 1000 - these are possible values for b, since when n is 0, b must be prime primes_up_to_one_thousand = sieve(2, 1000) primes = primes_up_to_one_thousand.copy() xy = 0 largest = 0 for x in primes_up_to_one_thousand: for y in primes_up_to_one_thousand: n = 0 # positive 'a' and positive 'b' while True: quadratic = (n * (n + x)) + y if is_prime(quadratic) is False: if n - 1 > largest: largest = (n - 1)