project-euler/050_consecutive_prime_sum.py

37 lines
840 B
Python

#!/usr/bin/env python3
#longest consecutive sum of primes that is a prime itself <x
def primes(x):
#pra stevila do xa
nums=[True]*x
primes=[2]
i=3
while i<x:
if nums[i-1]==True:
primes.append(i)
j=i*2
while j<x:
nums[j-1]=False
j+=i
i+=2
sums=[]
prime=0
m=1
for i in primes:
sums=list(map(lambda x: x+i,sums))
sums.append(i)
a=-2
while abs(a)<=len(sums):
try:
if nums[sums[a]-1]==True and abs(a)>m:
prime=sums[a]
m=abs(a)
except:
sums=sums[a+1:]
break
a-=1
return prime
if __name__ == '__main__':
print(primes(10**6))