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? ''' import pprint #pp = pprint.PrettyPrinter(indent=1) pp = pprint.PrettyPrinter() import prime #primes = [n for n in prime.prime_it(1000000)][::-1] primes = [n for n in prime.prime_it(1000000)] nbprimes = len(primes) #primes = [n for n in prime.prime_it(5000)][::-1] #print( primes ) primes = list(prime.prime_it(1000000)) print(primes) def sum_equal(l,n): return sum(l)==n def has_prime_seq(myprime): p = [m for m in primes if m<myprime//2] # primes lower than myprime nbprimes = len(p) print( nbprimes ) subs = (p[i:nbprimes-j] for i in range(0,nbprimes) for j in range(0,nbprimes-i)) #pp.pprint( subs ) res = [(len(zz),zz,sum(zz)) for zz in subs if sum(zz)==myprime] print( res ) return res
""" The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way: 28 = 2**2 + 2**3 + 2**4 33 = 3**2 + 2**3 + 2**4 49 = 5**2 + 2**3 + 2**4 47 = 2**2 + 3**3 + 2**4 How many numbers below fifty million can be expressed as the sum of a prime square, prime cube, and prime fourth power? """ from prime import prime_it from pprint import pprint num = 50 num = 50000000 # 0.687s r1 = int(num ** 0.5) + 1 r2 = int(num ** (1.0 / 3)) + 1 r3 = int(num ** 0.25) + 1 primes1 = list(prime_it(r1)) primes2 = list(prime_it(r2)) primes3 = list(prime_it(r3)) # pprint( primes ) # print( len(primes) ) # n = sum(1 for a in primes1 for b in primes2 for c in primes3 if a**2 + b**3 + c**4 <num) #print( n ) # 1139575 g = (a ** 2 + b ** 3 + c ** 4 for a in primes1 for b in primes2 for c in primes3 if a ** 2 + b ** 3 + c ** 4 < num) print(len(set(g))) # 1,097,343 unique 1.385s
#/bin/python # http://projecteuler.net/problem=10 # The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. # Find the sum of all the primes below two million. import prime l = (n for n in prime.prime_it(2000000)) print( sum(l) ) # 142913828922