Repository navigation
Expand file tree
/
Copy pathproblem_050_02.py
More file actions
232 lines (197 loc) · 6.92 KB
/
Copy pathproblem_050_02.py
File metadata and controls
232 lines (197 loc) · 6.92 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
##The prime 41, can be written as the sum of six consecutive primes:
##
##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?
import numpy as np
import itertools
import math
import time
import eulertools as et
# 1. Find primes under one million.
# 2. Test for each prime:
# primes[i] + primes[i+1] +... primes[j] until the sume >= prime.
# if the sum == prime, end, if >, break and start again with primes[i+1].
def euler50(upperlimit):
primes = et.primesfrom2to(upperlimit)
primeset = set(primes)
primes_rev = primes[::-1]
print("len(primes)\t",len(primes))
print("primes[len(primes)/2]\t",primes[len(primes)/2])
print("primes[len(primes)-1]\t",primes[len(primes)-1])
print("sum(primes)\t",sum(primes))
print("sum(primes[0:len(primes)/2])\t",sum(primes[0:len(primes)/2]))
dict_c = {}
#input("break")
max_summs = 0
current = []
max_prime = 1
for i in range(0, len(primes_rev)):
prime = primes_rev[i]
set_tried =set()
## dict_c[prime] = []
for j in range(0,len(primes)):
suma = 0
start = j
list_primes = []
count = 0
while suma < prime and primes[j] != prime:
count += 1
suma += primes[j]
if suma in set_tried:
break
list_primes.append(primes[j])
if suma in primeset:
set_tried.add(suma)
if j >= len(primes) - i:
break
if suma == prime:
if count > max_summs:
max_summs = count
current = list_primes.copy()
max_prime = prime
try:
dict_c[prime].append(list_primes.copy())
except:
dict_c[prime] = list_primes.copy()
break
j += 1
print(max_summs)
## print(current)
print(max_prime)
def euler50b(upperlimit):
# 1. Find primes under one million.
# 2. Test for each prime:
# primes[i] + primes[i+1] +... primes[j] until the sume >= prime.
# if the sum == prime, end, if >, break and start again with primes[i+1].
#create the list of primes
primes = et.primesfrom2to(upperlimit)
primeset = set(primes)
primes_rev = primes[::-1]
print("len(primes)\t",len(primes))
print("primes[len(primes)/2]\t",primes[len(primes)/2])
print("primes[len(primes)-1]\t",primes[len(primes)-1])
## print("sum(primes)\t",sum(primes))
## print("sum(primes[0:len(primes)/2])\t",sum(primes[0:len(primes)/2]))
dict_c = {}
#input("break")
max_summs = 0
current = []
max_prime = 1
for i in range(0, len(primes_rev)):
prime = primes_rev[i]
set_tried =set()
## dict_c[prime] = []
for j in range(i+1,len(primes_rev)-1):
diff = prime
start = j
list_primes = []
count = 0
while diff > 0 and j <len(primes_rev):
count += 1
try:
diff -= primes_rev[j]
except:
print(diff,i,j,len(primes_rev))
if diff in set_tried:
break
list_primes.append(primes_rev[j])
if diff in primeset:
set_tried.add(diff)
## if j >= len(primes) - i:
## break
if diff == 0:
if count > max_summs:
max_summs = count
current = list_primes.copy()
max_prime = prime
try:
dict_c[prime].append(list_primes.copy())
except:
dict_c[prime] = list_primes.copy()
break
j += 1
print(max_summs)
## print(current)
print(max_prime)
print(dict_c[max_prime])
## maxa = 0
## dict_max =dict()
## for i in dict_c.keys():
## try:
## for j in range(0,len(dict_c[i])):
## tmp_max = len(dict_c[i][j])
## if tmp_max >= maxa:
## maxa = tmp_max
## try:
## m = dict_max[maxa]
## addme = max(m,i)
##
## except:
## pass
##
## dict_max[maxa] = i
## except:
## pass
##
##
## print(maxa)
## print(dict_max[maxa])
## print(dict_c[dict_max[maxa]])
def euler50c(upperlimit):
# 1. Find primes under one million.
# 2. Test for each prime:
# primes[i] + primes[i+1] +... primes[j] until the sume >= prime.
# if the sum == prime, end, if >, break and start again with primes[i+1].
#create the list of primes
primes = et.primesfrom2to(upperlimit)
primeset = set(primes)
primes_rev = primes[::-1]
print("len(primes)\t",len(primes))
print("primes[len(primes)/2]\t",primes[len(primes)/2])
print("primes[len(primes)-1]\t",primes[len(primes)-1])
## print("sum(primes)\t",sum(primes))
## print("sum(primes[0:len(primes)/2])\t",sum(primes[0:len(primes)/2]))
dict_c = {}
#input("break")
max_summs = 0
current = []
max_prime = 1
value_max = primes[len(primes)-1]
for i in range(0,len(primes)+1):
for j in range(len(primes)+1,i,-1):
## print(i,j)
if (j-i) < max_summs:
## print("breaking ",i,j)
break
else:
temp_l = primes[i:j]
temp_prime = sum(temp_l)
if temp_prime > value_max:
## print("Breaking\t temp_prime", temp_prime, value_max)
pass
else:
if temp_prime in primeset:
if len(temp_l)> max_summs:
max_summs = len(temp_l)
max_prime = temp_prime
try:
dict_c[temp_prime].append(temp_l.copy())
except:
dict_c[temp_prime] = temp_l.copy()
print(max_summs)
## print(current)
print(max_prime)
print(dict_c[max_prime])
def main():
start = time.time()
euler50c(1000)
end = time.time()
print(end-start)
main()