def main(): size = 1000000 sieve = get_sieve(size) seen = set([]) required_family_size = 8 smallest = size for num in xrange(size): if sieve[num] == 0 or num in seen: continue str_num = str(num) for i in xrange(10): char = str(i) if sieve[num] == 1: family = [num] seen.add(num) else: family = [] if char in str_num: for j in xrange(10): if j != i: new_num = int(str_num.replace(char, str(j))) if len(str(new_num)) != len(str_num): continue if sieve[new_num] == 1: family.append(new_num) seen.add(new_num) if len(family) >= required_family_size: family.sort() if family[0] < smallest: smallest = family[0] print smallest
25 = 7 + 2 * 3^2 27 = 19 + 2 * 2^2 33 = 31 + 2 * 1^2 It turns out that the conjecture was false. What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? """ import math from problem47 import get_sieve max_num = 100000 sieve = get_sieve(max_num) def get_smaller_prime(num): for i in xrange(num - 1, 0, -1): if sieve[i] == 1: return i # throw an error return 0 def is_goldbach(num): prime = get_smaller_prime(num) while prime > 1: new_num = num - prime if new_num % 2 == 0: sqrt = math.sqrt(new_num / 2) if sqrt == int(sqrt):
42 21 22 23 24 25 26 43 44 45 46 47 48 49 It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 62%. If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%? """ from math import sqrt from problem47 import get_sieve sieve = get_sieve(10000000) def is_prime(num): if num < len(sieve): return sieve[num] == 1 limit = int(sqrt(num)) + 1 if limit > len(sieve): print "Error: sieve not large enough" return False for i in xrange(2, limit): if sieve[i] == 1 and num % i == 0: return False return True
25 = 7 + 2 * 3^2 27 = 19 + 2 * 2^2 33 = 31 + 2 * 1^2 It turns out that the conjecture was false. What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? """ import math from problem47 import get_sieve max_num = 100000 sieve = get_sieve(max_num) def get_smaller_prime(num): for i in xrange(num - 1, 0, -1): if sieve[i] == 1: return i # throw an error return 0 def is_goldbach(num): prime = get_smaller_prime(num) while prime > 1: new_num = num - prime if new_num % 2 == 0:
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? for each i in the set new_set = permuations(rest of set - i) return_set = set() for item in set: return_set.add(i + item) return set """ from problem47 import get_sieve sieve = get_sieve(10000) def get_permutations(num): def _get_permutations_helper(str_num): if not str_num: return set([""]) permutations = set() for i in xrange(len(str_num)): new_num = list(str_num) head = new_num.pop(i) temp_permutations = _get_permutations_helper(new_num) for permutation in temp_permutations: permutations.add(head + permutation) return permutations