-
Notifications
You must be signed in to change notification settings - Fork 0
/
PE050.py
59 lines (54 loc) · 1.41 KB
/
PE050.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/user/bin/env python3
maxnum = 0
maxsum = 0
maxstart = 0
start = 2
def nextPrime(current):
if current < 2:
return 2
elif current == 2:
return 3
else:
current += current%2 + 1
isPrime = False
while not isPrime:
isPrime = True
for i in range(3, int(current**0.5)+2, 2):
if i < current and not current % i:
isPrime = False
if isPrime: return current
current += 2
def isPrime(inp):
if inp < 2:
return False
if inp == 2:
return True
if not inp % 2:
return False
for i in range(3, int(inp**0.5)+2, 2):
if i < inp and not inp % i:
return False
return True
while start < 1000000:
num = 1
sum = start
next = start
maxNumFromHere = 0
maxSumFromHere = 0
while sum < 1000000:
next = nextPrime(next)
num += 1
sum += next
if isPrime(sum) and sum < 1000000:
maxSumFromHere = sum
maxNumFromHere = num
if maxNumFromHere > maxnum:
maxnum = maxNumFromHere
maxsum = maxSumFromHere
maxstart = start
# print("starting from", start, ",", maxNumFromHere, "primes add up to", maxSumFromHere)
# if maxNumFromHere < 300:
# break
start = nextPrime(start)
outp = 'Starting from ' + str(maxstart) + ', ' + str(maxnum) + ' primes add up to ' + str(maxsum);
print(outp)