70 likes | 266 Views
質數 (Prime) 相關問題 (III) — 如何找出相對大的質數. Date: May 27, 2009 Introducer: Hsing-Yen Ann. 問題. 9,99 9 ,000,001 到 10,00 0 ,000,000 [10 10 -10 6 +1, 10 10 ] 這一百萬個數之間, 哪些數是質數? 使用 Sieve of Eratosthenes ( 篩法 ) ? 或者使用暴力法? Time 與 Space 均無法滿足。 解法: Segmented Sieve.
E N D
質數(Prime)相關問題 (III)— 如何找出相對大的質數 Date: May 27, 2009 Introducer: Hsing-Yen Ann
問題 • 9,999,000,001 到 10,000,000,000 [1010-106+1, 1010] 這一百萬個數之間,哪些數是質數? • 使用 Sieve of Eratosthenes (篩法)?或者使用暴力法?Time 與 Space 均無法滿足。 • 解法:Segmented Sieve
Segmented Sieve of Eratosthenes (1/4) • [1010-106+1, 1010] 之間,哪些數是質數? • 先用 Sieve of Eratosthenes 計算出[1, 105] 之間的質數 P = {2, 3, 5, 7, 11, 13, 17, 19, ..., 9967, 9973}這些是所有可能的因數 • 建立一個 boolean array isPrime[106],用來表示[1010-106+1, 1010] 之間的數,起始值均設為 true • 對於 P 中的每個質數 p,將 p 位於 [1010-106+1, 1010] 之間的倍數設定為 false
結論 • [1010-106+1, 1010] 之間,哪些數是質數? • {9999000001, 9999000029, 9999000047, 9999000083, ..., 9999999967}共 43453個質數 • 令範圍為[L, U],且令 D=U-L,則需要時間為: