Presentation is loading. Please wait.

Presentation is loading. Please wait.

質數 (Prime) 相關問題 (III) — 如何找出相對大的質數 Date: May 27, 2009 Introducer: Hsing-Yen Ann.

Similar presentations


Presentation on theme: "質數 (Prime) 相關問題 (III) — 如何找出相對大的質數 Date: May 27, 2009 Introducer: Hsing-Yen Ann."— Presentation transcript:

1 質數 (Prime) 相關問題 (III) — 如何找出相對大的質數 Date: May 27, 2009 Introducer: Hsing-Yen Ann

2 2 問題 9,999,000,001 到 10,000,000,000 [10 10 -10 6 +1, 10 10 ] 這一百萬個數之間, 哪些數是質數? 使用 Sieve of Eratosthenes ( 篩法 ) ? 或者使用暴力法? Time 與 Space 均無法滿足。 解法: Segmented Sieve

3 3 Segmented Sieve of Eratosthenes (1/4) [10 10 -10 6 +1, 10 10 ] 之間,哪些數是質數? 先用 Sieve of Eratosthenes 計算出 [1, 10 5 ] 之間的質數 P = {2, 3, 5, 7, 11, 13, 17, 19,..., 9967, 9973} 這些是所有可能的因數 建立一個 boolean array isPrime[10 6 ] ,用來表示 [10 10 -10 6 +1, 10 10 ] 之間的數,起始值均設為 true 對於 P 中的每個質數 p ,將 p 位於 [10 10 -10 6 +1, 10 10 ] 之間的倍數設定為 false

4 4 Segmented Sieve of Eratosthenes (2/4) p=2

5 5 Segmented Sieve of Eratosthenes (3/4) p=3

6 6 Segmented Sieve of Eratosthenes (4/4) p=5

7 7 結論 [10 10 -10 6 +1, 10 10 ] 之間,哪些數是質數? {9999000001, 9999000029, 9999000047, 9999000083,..., 9999999967} 共 43453 個質數 令範圍為 [ L, U ] ,且令 D=U-L , 則需要時間為 :


Download ppt "質數 (Prime) 相關問題 (III) — 如何找出相對大的質數 Date: May 27, 2009 Introducer: Hsing-Yen Ann."

Similar presentations


Ads by Google