回答編集履歴

2

修正

2018/09/06 17:07

投稿

katoy
katoy

スコア22324

test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
 
74
74
 
75
- for _ in range(19990):
75
+ for _ in range(19999):
76
76
 
77
77
  next(primes)
78
78
 
@@ -84,7 +84,7 @@
84
84
 
85
85
  実行例:
86
86
 
87
- ![イメージ説明](8d8768a3d9cbdb9b2eda77bfe626d248.png)
87
+ ![イメージ説明](f7d440d9962e71cfe93615682903e9ec.png)
88
88
 
89
89
 
90
90
 

1

追記

2018/09/06 17:07

投稿

katoy
katoy

スコア22324

test CHANGED
@@ -15,3 +15,77 @@
15
15
  - Calculating the Nth prime number
16
16
 
17
17
  [https://www.codeproject.com/Articles/1228398/Calculating-the-Nth-prime-number](https://www.codeproject.com/Articles/1228398/Calculating-the-Nth-prime-number)
18
+
19
+
20
+
21
+ Finding nth Prime using Python and Sieve of Eratosthenes のページの中のコードの一つを
22
+
23
+ 実際に試してみます。2000 編目でなく 20000 番目を求めてみました。
24
+
25
+ ```python3
26
+
27
+ def gen_primes():
28
+
29
+ D = {}
30
+
31
+ q = 2 # first integer to test for primality.
32
+
33
+
34
+
35
+ while True:
36
+
37
+ if q not in D:
38
+
39
+ # not marked composite, must be prime
40
+
41
+ yield q
42
+
43
+
44
+
45
+ # first multiple of q not already marked
46
+
47
+ D[q * q] = [q]
48
+
49
+ else:
50
+
51
+ for p in D[q]:
52
+
53
+ D.setdefault(p + q, []).append(p)
54
+
55
+ # no longer need D[q], free memory
56
+
57
+ del D[q]
58
+
59
+
60
+
61
+ q += 1
62
+
63
+
64
+
65
+ primes = gen_primes()
66
+
67
+
68
+
69
+ # for p in primes:
70
+
71
+ # print(p)
72
+
73
+
74
+
75
+ for _ in range(19990):
76
+
77
+ next(primes)
78
+
79
+
80
+
81
+ print(next(primes))
82
+
83
+ ```
84
+
85
+ 実行例:
86
+
87
+ ![イメージ説明](8d8768a3d9cbdb9b2eda77bfe626d248.png)
88
+
89
+
90
+
91
+ i病以下で処理できています。