• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            A Za, A Za, Fighting...

            堅信:勤能補拙

            線性篩法求質數(素數)表

            參考:
            http://www.cnblogs.com/coeus/articles/1541722.html

            原理:
            1. 任何一個合數都可以表示成一個質數和一個數的乘積
            2. 假設A是一個合數,且A = x * y,這里x也是一個合數,那么有:
                   A = x * y; (假設y質數,x合數)
                   x = a * b; (假設a是質數,且a < x)
             ->  A = a * b * y = a * Z (Z = b * y)
            即一個合數(x)與一個質數(y)的乘積可以表示成一個更大的合數(Z)與一個更小的質數(a)的乘積
            這也是理解代碼中 if(i%primes[j] == 0)break;的關鍵
            例如: 如果i = 8; 那么由于i%2 == 0; 因此對于i=8就只需要檢查primes[1]即可,因為對于大于primes[1]的質數,像3,有:
                    8*3 = 2*4*3 = 12*2
            也就是說24(8*3=24)并不需要在8時檢查,在12時才檢查 

            代碼:
             1 /*
             2  * Problem:
             3  * given an upper bound like U(integer), print all the primes between 0-U
             4  *
             5  * Points:
             6  * this's a O(n) algorithm, amazing
             7  */
             8 #include<stdio.h>
             9 #include<stdlib.h>
            10 #include<string.h>
            11 #define MAX_N 250000
            12 int N, hash[MAX_N];
            13 int pcount, primes[MAX_N];
            14 
            15 void
            16 linear_selection()
            17 {
            18     int i, j;
            19     primes[pcount++= 1;
            20     for(i=2; i<=N; i++) {
            21         if(!hash[i])
            22             primes[pcount++= i;
            23         for(j=1; j<pcount && i*primes[j]<=N; j++) {
            24             hash[i*primes[j]] = 1;
            25             if(i%primes[j] == 0)
            26                 break;
            27         }
            28     }
            29 }
            30 
            31 int
            32 main(int argc, char **argv)
            33 {
            34     int i;
            35     while(1) {
            36         printf("Enter the upper boundary: ");
            37         scanf("%d"&N);
            38         if(!N)
            39             break;
            40         memset(hash, 0sizeof(hash));
            41         pcount = 0;
            42         linear_selection();
            43         for(i=0; i<pcount; i++)
            44             printf("%d\n", primes[i]);
            45     }
            46 }

            posted on 2010-10-17 18:19 simplyzhao 閱讀(355) 評論(0)  編輯 收藏 引用 所屬分類: G_其他

            導航

            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            超级97碰碰碰碰久久久久最新| 精品无码久久久久久尤物| 国产99久久久国产精品~~牛| 97久久国产亚洲精品超碰热| 久久久久国产精品熟女影院| 久久精品国产精品青草| 久久久久国色AV免费看图片| 波多野结衣久久精品| 好属妞这里只有精品久久| 久久e热在这里只有国产中文精品99 | 欧美亚洲国产精品久久| 91精品国产91久久综合| 色综合久久久久综合99| 少妇精品久久久一区二区三区| 亚洲一区二区三区日本久久九| 性做久久久久久久久浪潮| 欧美亚洲另类久久综合| 一本综合久久国产二区| 日本免费一区二区久久人人澡 | 一级A毛片免费观看久久精品| 狠狠色丁香久久婷婷综| 奇米影视7777久久精品人人爽| 亚洲乱亚洲乱淫久久| 亚洲va中文字幕无码久久| 94久久国产乱子伦精品免费 | 精品人妻伦九区久久AAA片69| 人妻精品久久久久中文字幕69| 精品久久久久中文字幕一区| 69国产成人综合久久精品| 久久久久久av无码免费看大片| 国产精品久久久久久久| 一本色道久久88精品综合| 久久无码人妻精品一区二区三区| 国产成人久久精品激情| 久久人人爽爽爽人久久久| 亚洲午夜久久久| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 国产成人久久精品区一区二区| 久久久久人妻一区精品色| 久久久老熟女一区二区三区| 久久精品黄AA片一区二区三区|