from bisect import bisect_left def genDoubleSquares(bound): for i in range(1, bound): yield i ** 2 * 2 def oddCompositeSieve(bound): sieve = [True] * bound sieve[0] = sieve[1] = False for (i, prime) in enumerate(sieve): if prime: for n in range(i * i, bound, i): sieve[n] = False elif i % 2 == 1 and i > 1: yield i bound = 10000 primes = list(primeSieve(bound)) doubleSquares = list(genDoubleSquares(bound)) for i in oddCompositeSieve(bound): summable = False for j in primes[:bisect_left(primes, i)]: for k in doubleSquares[:bisect_left(doubleSquares, i)]: if j + k == i: summable = True break if summable: break if not summable: print(i) break
#=============================================================================== # Find the sum of all the primes below two million. #=============================================================================== from Common import primeSieve print(sum(primeSieve(2000000)))
# # d2d3d4=406 is divisible by 2 # d3d4d5=063 is divisible by 3 # d4d5d6=635 is divisible by 5 # d5d6d7=357 is divisible by 7 # d6d7d8=572 is divisible by 11 # d7d8d9=728 is divisible by 13 # d8d9d10=289 is divisible by 17 # # Find the sum of all 0 to 9 pandigital numbers with this property. #=============================================================================== from Common import primeSieve from itertools import permutations primes = [0] + list(primeSieve(18)) pandigitals = list(permutations(range(10))) def isSpecial(n): s = str(n) for i in range(1, 8): sub = s[i] + s[i + 1] + s[i + 2] if not int(sub) % primes[i] == 0: return False return True total = 0 for t in pandigitals: t = str(t).replace(', ', '').replace('(', '').replace(')', '') if isSpecial(t): total += int(t)
#=============================================================================== # 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 Common import primeSieve cache = {} for i in primeSieve(10000): s = str(i) if len(s) == 4: ordered = ''.join(sorted(s)) if ordered in cache: cache[ordered].append(i) else: cache[ordered] = [i] for k in cache: perms = cache[k] if len(perms) >= 3: diffs = {} for i in range(len(perms)): for j in range(i + 1, len(perms)): diff = perms[j] - perms[i] if diff in diffs: if not str(perms[i]) in diffs[diff]:
#=============================================================================== # Find the sum of the only eleven primes that are both truncatable from left to right and right to left. #=============================================================================== from Common import isPrime, primeSieve def isTruncatablePrime(n): if n <= 7: return False s = str(n) length = len(s) for i in range(length): if not isPrime(int(s[i:])): return False for i in range(length): if not isPrime(int(s[:length - i])): return False return True print(sum([i for i in primeSieve(1000000) if isTruncatablePrime(i)]))
#=============================================================================== # How many circular primes are there below one million? #=============================================================================== from Common import isPrime, primeSieve def isCircularPrime(n): s = str(n) for i in range(len(s)): if not isPrime(int(s[i:] + s[:i])): return False return True print(len([i for i in primeSieve(1000000) if isCircularPrime(i)]))
# =============================================================================== # What is the largest n-digit pandigital prime that exists? # =============================================================================== from Common import primeSieve, isPandigital print(max([i for i in primeSieve(10000000) if isPandigital(i)]))
# # d2d3d4=406 is divisible by 2 # d3d4d5=063 is divisible by 3 # d4d5d6=635 is divisible by 5 # d5d6d7=357 is divisible by 7 # d6d7d8=572 is divisible by 11 # d7d8d9=728 is divisible by 13 # d8d9d10=289 is divisible by 17 # # Find the sum of all 0 to 9 pandigital numbers with this property. #=============================================================================== from Common import primeSieve from itertools import permutations primes = [0] + list(primeSieve(18)) pandigitals = list(permutations(range(10))) def isSpecial(n): s = str(n) for i in range(1, 8): sub = s[i] + s[i + 1] + s[i + 2] if not int(sub) % primes[i] == 0: return False return True total = 0 for t in pandigitals: t = str(t).replace(', ', '').replace('(', '').replace(')', '')