質問するログイン新規登録

回答編集履歴

2

修正

2018/09/06 17:07

投稿

katoy
katoy

スコア22328

answer CHANGED
@@ -35,12 +35,12 @@
35
35
  # for p in primes:
36
36
  # print(p)
37
37
 
38
- for _ in range(19990):
38
+ for _ in range(19999):
39
39
  next(primes)
40
40
 
41
41
  print(next(primes))
42
42
  ```
43
43
  実行例:
44
- ![イメージ説明](8d8768a3d9cbdb9b2eda77bfe626d248.png)
44
+ ![イメージ説明](f7d440d9962e71cfe93615682903e9ec.png)
45
45
 
46
46
  i病以下で処理できています。

1

追記

2018/09/06 17:07

投稿

katoy
katoy

スコア22328

answer CHANGED
@@ -6,4 +6,41 @@
6
6
  [https://codereview.stackexchange.com/questions/185219/](https://codereview.stackexchange.com/questions/185219/)
7
7
 
8
8
  - Calculating the Nth prime number
9
- [https://www.codeproject.com/Articles/1228398/Calculating-the-Nth-prime-number](https://www.codeproject.com/Articles/1228398/Calculating-the-Nth-prime-number)
9
+ [https://www.codeproject.com/Articles/1228398/Calculating-the-Nth-prime-number](https://www.codeproject.com/Articles/1228398/Calculating-the-Nth-prime-number)
10
+
11
+ Finding nth Prime using Python and Sieve of Eratosthenes のページの中のコードの一つを
12
+ 実際に試してみます。2000 編目でなく 20000 番目を求めてみました。
13
+ ```python3
14
+ def gen_primes():
15
+ D = {}
16
+ q = 2 # first integer to test for primality.
17
+
18
+ while True:
19
+ if q not in D:
20
+ # not marked composite, must be prime
21
+ yield q
22
+
23
+ # first multiple of q not already marked
24
+ D[q * q] = [q]
25
+ else:
26
+ for p in D[q]:
27
+ D.setdefault(p + q, []).append(p)
28
+ # no longer need D[q], free memory
29
+ del D[q]
30
+
31
+ q += 1
32
+
33
+ primes = gen_primes()
34
+
35
+ # for p in primes:
36
+ # print(p)
37
+
38
+ for _ in range(19990):
39
+ next(primes)
40
+
41
+ print(next(primes))
42
+ ```
43
+ 実行例:
44
+ ![イメージ説明](8d8768a3d9cbdb9b2eda77bfe626d248.png)
45
+
46
+ i病以下で処理できています。