Annotation of researchv10no/cmd/primes/primes.c, revision 1.1

1.1     ! root        1: static double big = 7.20575940379e16;
        !             2: #define ptsiz 50
        !             3: #define tabsiz 1000
        !             4: #define tsiz8 8000
        !             5: #define whsiz 48
        !             6: double nn;
        !             7: char table[tabsiz];
        !             8: int pt[] = {
        !             9:          2,  3,  5,  7, 11, 13, 17, 19, 23, 29,
        !            10:         31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
        !            11:         73, 79, 83, 89, 97,101,103,107,109,113,
        !            12:        127,131,137,139,149,151,157,163,167,173,
        !            13:        179,181,191,193,197,199,211,223,227,229,
        !            14: };
        !            15: double wheel[] = {
        !            16:        10, 2, 4, 2, 4, 6, 2, 6, 4, 2,
        !            17:         4, 6, 6, 2, 6, 4, 2, 6, 4, 6,
        !            18:         8, 4, 2, 4, 2, 4, 8, 6, 4, 6,
        !            19:         2, 4, 6, 2, 6, 6, 4, 2, 4, 6,
        !            20:         2, 6, 4, 2, 4, 2,10, 2,
        !            21: };
        !            22: char bittab[] = {
        !            23:        1, 2, 4, 8, 16, 32, 64, 128,
        !            24: };
        !            25: 
        !            26: main(argc, argp)
        !            27: int argc;
        !            28: char *argp[];
        !            29: {
        !            30:        register i;
        !            31:        double k, temp, v;
        !            32:        double limit = big;
        !            33:        double atof(), sqrt();
        !            34: 
        !            35:        if(argc > 1){
        !            36:                nn = atof(argp[1]);
        !            37:        }else{
        !            38:                scanf("%lf", &nn);
        !            39:        }
        !            40:        if(argc > 2){
        !            41:                limit = atof(argp[2]);
        !            42:                if(limit < nn) exit(0);
        !            43:                if(limit > big) ouch();
        !            44:        }
        !            45:        if((nn<0) || (nn>big)) ouch();
        !            46:        if(nn == 0) exit();
        !            47: 
        !            48:        if(nn < 230.){
        !            49:                for(i=0; i<ptsiz; i++){
        !            50:                        if(pt[i] < nn) continue;
        !            51:                        if(pt[i] > limit) exit(0);
        !            52:                        printf("%d\n", pt[i]);
        !            53:                }
        !            54:        nn = 230.;
        !            55:        }
        !            56: 
        !            57:        modf(nn/2, &temp);
        !            58:        nn = 2.*temp + 1;
        !            59: /*
        !            60:  *     clear the sieve table.
        !            61:  */
        !            62:        while(1){
        !            63:                for(i=0;i<tabsiz;i++)
        !            64:                        table[i] = 0;
        !            65: /*
        !            66:  *     run the sieve.
        !            67:  */
        !            68:                v = sqrt(nn+tsiz8);
        !            69:                mark(nn,(double)3);
        !            70:                mark(nn,(double)5);
        !            71:                mark(nn,(double)7);
        !            72:                for(i=0,k=11;k<=v;k=k+wheel[i]){
        !            73:                        mark(nn,k);
        !            74:                        if(++i == whsiz) i = 0;
        !            75:                }
        !            76: /*
        !            77:  *     now get the primes from the table
        !            78:  *     and print them.
        !            79:  */
        !            80:                for(i=0; i<tsiz8; i += 2){
        !            81:                        if(table[i>>3] & bittab[i&07]) continue;
        !            82:                        temp = nn + i;
        !            83:                        if(temp > limit) exit(0);
        !            84:                        printf("%.0f\n", temp);
        !            85:                }
        !            86:                nn += tsiz8;
        !            87:        }
        !            88: }
        !            89: 
        !            90: mark(nn,k)
        !            91: double nn,k;
        !            92: {
        !            93:        double t1;
        !            94:        register j;
        !            95:        register k1 = k;
        !            96: 
        !            97:        modf(nn/k, &t1);
        !            98:        j = k*t1 - nn;
        !            99:        if(j < 0)
        !           100:                j += k1;
        !           101:        for( ; j < tsiz8; j += k1){
        !           102:                table[j>>3] |= bittab[j&07];
        !           103:        }
        !           104: }
        !           105: 
        !           106: ouch()
        !           107: {
        !           108:        printf("OUCH!\n");
        !           109:        exit(1);
        !           110: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.