回答編集履歴
3
edit
test
CHANGED
@@ -95,3 +95,123 @@
|
|
95
95
|
print(len(ps))
|
96
96
|
|
97
97
|
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
いろいろと悲しみしかなかったといっておく…
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```python
|
106
|
+
|
107
|
+
import time
|
108
|
+
|
109
|
+
from math import sqrt
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def timer(func):
|
114
|
+
|
115
|
+
def itimer(*args, **kwargs):
|
116
|
+
|
117
|
+
st = time.time()
|
118
|
+
|
119
|
+
ret = func(*args, **kwargs)
|
120
|
+
|
121
|
+
print(time.time() - st)
|
122
|
+
|
123
|
+
return ret
|
124
|
+
|
125
|
+
return itimer
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
N = 10000
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
@timer
|
134
|
+
|
135
|
+
def f():
|
136
|
+
|
137
|
+
ps = [2]
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
def is_prime_countup(num, ps):
|
142
|
+
|
143
|
+
l = sqrt(num)
|
144
|
+
|
145
|
+
for p in ps:
|
146
|
+
|
147
|
+
if num % p == 0:
|
148
|
+
|
149
|
+
return False
|
150
|
+
|
151
|
+
if p > l:
|
152
|
+
|
153
|
+
return True
|
154
|
+
|
155
|
+
return True
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
i = 3
|
160
|
+
|
161
|
+
while len(ps) < N:
|
162
|
+
|
163
|
+
if is_prime_countup(i, ps):
|
164
|
+
|
165
|
+
ps.append(i)
|
166
|
+
|
167
|
+
i += 1
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
return ps
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
@timer
|
176
|
+
|
177
|
+
def g():
|
178
|
+
|
179
|
+
ps = [2]
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
def is_not_prime_countup(num, ps):
|
184
|
+
|
185
|
+
l = sqrt(num)
|
186
|
+
|
187
|
+
return any(num % p == 0 for p in ps if p <= l)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
i = 3
|
192
|
+
|
193
|
+
while True:
|
194
|
+
|
195
|
+
if not is_not_prime_countup(i, ps):
|
196
|
+
|
197
|
+
ps.append(i)
|
198
|
+
|
199
|
+
if len(ps) == N:
|
200
|
+
|
201
|
+
break
|
202
|
+
|
203
|
+
i += 1
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
return ps
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
ansf = f()
|
212
|
+
|
213
|
+
ansg = g()
|
214
|
+
|
215
|
+
assert all(i == j for i, j in zip(ansf, ansg))
|
216
|
+
|
217
|
+
```
|
2
edit
test
CHANGED
@@ -51,3 +51,47 @@
|
|
51
51
|
print(len(ps))
|
52
52
|
|
53
53
|
```
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
気持ち高速化してみる。
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
```python
|
62
|
+
|
63
|
+
from math import sqrt
|
64
|
+
|
65
|
+
ps = [2]
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
def is_not_prime_countup(num, ps):
|
70
|
+
|
71
|
+
l = sqrt(num)
|
72
|
+
|
73
|
+
return any(num % p == 0 for p in ps if p <= l)
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
i = 3
|
78
|
+
|
79
|
+
while True:
|
80
|
+
|
81
|
+
if not is_not_prime_countup(i, ps):
|
82
|
+
|
83
|
+
ps.append(i)
|
84
|
+
|
85
|
+
if len(ps) == 5000:
|
86
|
+
|
87
|
+
break
|
88
|
+
|
89
|
+
i += 1
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
print(ps)
|
94
|
+
|
95
|
+
print(len(ps))
|
96
|
+
|
97
|
+
```
|
1
edit
test
CHANGED
@@ -5,3 +5,49 @@
|
|
5
5
|
普通はこういうのを使います。
|
6
6
|
|
7
7
|
for文とif文で書けますよ。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
---
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
参考程度に。本当にあってるかな…
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
```python
|
20
|
+
|
21
|
+
ps = [2]
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
def is_prime_countup(num, ps):
|
26
|
+
|
27
|
+
for p in ps:
|
28
|
+
|
29
|
+
if num % p == 0:
|
30
|
+
|
31
|
+
return False
|
32
|
+
|
33
|
+
return True
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
i = 3
|
38
|
+
|
39
|
+
while len(ps) < 2000:
|
40
|
+
|
41
|
+
if is_prime_countup(i, ps):
|
42
|
+
|
43
|
+
ps.append(i)
|
44
|
+
|
45
|
+
i += 1
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
print(ps)
|
50
|
+
|
51
|
+
print(len(ps))
|
52
|
+
|
53
|
+
```
|