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 modules.matlib import isPrime, nextPrime mxl = 0 # maximum length of consecutive primes list mxsm = 0 # sum of longest consecutive primes list s = 2 # start while s < 1000000: # while start is below 1 milion cp = [] # consecutive primes t = s # start of second loop while (t + sum(cp) < 1000000) and isPrime( t + sum(cp) ): # while sum of consecutive primes plus the last one is below 100 cp.append(t) # append prime to consecutive primes list if len(cp) > mxl: # if list of consecutive primes is longest and sum of those consecutive primes is prime mxl = len(cp) # lenght of current list mxsm = sum(cp) # sum of consecutive primes t = nextPrime(t) # continue with next prime s = nextPrime(s) # continue with next prime print(mxsm) # output sum of longest consecutive primes list
#!/usr/bin/python3 """ Problem 10 - Summation of primes The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ from modules.matlib import isPrime sm = 2 # sum, starting with first prime for x in range(3, 2000000, 2): # for all odd numbers below two million if isPrime(x): # if is prime sm += x # add it to sum print(sm) # return result
def isTruncatablePrime(n): n = str(n) if len(n) < 2: return False for i in range(1, len(n), 1): if not isPrime(int(n[i:])) or not isPrime(int(n[:-i])): return False return True