Algorithm: Print table of 500 primes

来源:互联网 发布:手机淘宝联盟登录失效 编辑:程序博客网 时间:2024/06/06 17:57

Algorithm P

Algorithm P (Print table of 500 primes). This algorithm has two distinct
parts: Steps P1-P8 prepare an internal table of 500 primes, and steps P9-P11
print the answer in the form shown above. The latter part of the program uses
two “buffers,” in which line images are formed; while one buffer is being printed,
the other is being filled.
P1. [Start table.] Set PRIME[1] <– 2, N <– 3, J <– 1. (In this program, N runs
through the odd numbers that are candidates for primes; J keeps track of
how many primes have been found so far.)
P2. [N is prime.] Set J <– J + 1, PRIME[J] <– N.
P3. [500 found?] If J = 500, go to step P9.
P4. [Advance N.] Set N <– N + 2.
P5. [K <– 2.] Set K <– 2. (PRIME[K] will run through the possible prime divisors
of N.)
P6. [PRIME[K] \N?] Divide N by PRIME[K]; let Q be the quotient and R the
remainder. If R = 0 (hence N is not prime), go to P4.
P7. [PRIME[K] large?] If Q <= PRIME[K], go to P2. (In such a case, N must
be prime; the proof of this fact is interesting and a little unusual – see
exercise 6.)
P8. [Advance K.] Increase K by 1, and go to P6.
P9. [Print title.] Now we are ready to print the table. Advance the printer
to the next page. Set BUFFER[0] to the title line and print this line. Set
B <– 1, M <– 1.
P10. [Set up line.] Put PRIME[M], PRIME[50 + M], …, PRIME[450 + M] into
BUFFER[B] in the proper format.
P11. [Print line.] Print BUFFER[B]; set B <– 1 - B (thereby switching to the
other buffer); and increase M by 1. If M <= 50, return to P10; otherwise the
algorithm terminates. |


Flow diagram

这里写图片描述


Java program

/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 12/17/13 * Time: 6:52 PM * :)~ * Print table of 500 primes:ALGORITHMS */public class Main {    public static void main(String[] args) {        int[] PRIME = new int[501];        PRIME[1] = 2;        int N=3;        int J=1;        /*Kernel of the Algorithm*/        do{            J++;                        /*P2*/            PRIME[J] = N;            if(J == 500){                 /*P3*/                break;            }            do{                N+=2;                        /*P4*/                int K=2;                     /*P5*/                int Q, R;                     /*P6*/                do{                    Q = N/PRIME[K];                    R = N%PRIME[K];                    if(R == 0){                        break;                    }                    if(Q <= PRIME[K]){         /*P7*/                        break;                    }                    K++;                      /*P8*/                }while (true);                if(R!=0 && Q <= PRIME[K]){                    break;                }            }while (true);        }while (true);        /*Output the 500 PRIMES*/        System.out.println("FIRST FIVE HUNDRED PRIMES:");        System.out.print("   ");        for(int i=1; i<=10; i++){            System.out.print(String.format("%5s", i));        }        System.out.println();        System.out.println();        for(int M=1; M<=50; M++){            System.out.print(String.format("%3s", M+":"));            for(int j=0; j<500; j+=50){                System.out.print(String.format("%5s", PRIME[j+M]));            }            System.out.println();        }    }}

Outputs

FIRST FIVE HUNDRED PRIMES:       1    2    3    4    5    6    7    8    9   10 1:    2  233  547  877 1229 1597 1993 2371 2749 3187 2:    3  239  557  881 1231 1601 1997 2377 2753 3191 3:    5  241  563  883 1237 1607 1999 2381 2767 3203 4:    7  251  569  887 1249 1609 2003 2383 2777 3209 5:   11  257  571  907 1259 1613 2011 2389 2789 3217 6:   13  263  577  911 1277 1619 2017 2393 2791 3221 7:   17  269  587  919 1279 1621 2027 2399 2797 3229 8:   19  271  593  929 1283 1627 2029 2411 2801 3251 9:   23  277  599  937 1289 1637 2039 2417 2803 325310:   29  281  601  941 1291 1657 2053 2423 2819 325711:   31  283  607  947 1297 1663 2063 2437 2833 325912:   37  293  613  953 1301 1667 2069 2441 2837 327113:   41  307  617  967 1303 1669 2081 2447 2843 329914:   43  311  619  971 1307 1693 2083 2459 2851 330115:   47  313  631  977 1319 1697 2087 2467 2857 330716:   53  317  641  983 1321 1699 2089 2473 2861 331317:   59  331  643  991 1327 1709 2099 2477 2879 331918:   61  337  647  997 1361 1721 2111 2503 2887 332319:   67  347  653 1009 1367 1723 2113 2521 2897 332920:   71  349  659 1013 1373 1733 2129 2531 2903 333121:   73  353  661 1019 1381 1741 2131 2539 2909 334322:   79  359  673 1021 1399 1747 2137 2543 2917 334723:   83  367  677 1031 1409 1753 2141 2549 2927 335924:   89  373  683 1033 1423 1759 2143 2551 2939 336125:   97  379  691 1039 1427 1777 2153 2557 2953 337126:  101  383  701 1049 1429 1783 2161 2579 2957 337327:  103  389  709 1051 1433 1787 2179 2591 2963 338928:  107  397  719 1061 1439 1789 2203 2593 2969 339129:  109  401  727 1063 1447 1801 2207 2609 2971 340730:  113  409  733 1069 1451 1811 2213 2617 2999 341331:  127  419  739 1087 1453 1823 2221 2621 3001 343332:  131  421  743 1091 1459 1831 2237 2633 3011 344933:  137  431  751 1093 1471 1847 2239 2647 3019 345734:  139  433  757 1097 1481 1861 2243 2657 3023 346135:  149  439  761 1103 1483 1867 2251 2659 3037 346336:  151  443  769 1109 1487 1871 2267 2663 3041 346737:  157  449  773 1117 1489 1873 2269 2671 3049 346938:  163  457  787 1123 1493 1877 2273 2677 3061 349139:  167  461  797 1129 1499 1879 2281 2683 3067 349940:  173  463  809 1151 1511 1889 2287 2687 3079 351141:  179  467  811 1153 1523 1901 2293 2689 3083 351742:  181  479  821 1163 1531 1907 2297 2693 3089 352743:  191  487  823 1171 1543 1913 2309 2699 3109 352944:  193  491  827 1181 1549 1931 2311 2707 3119 353345:  197  499  829 1187 1553 1933 2333 2711 3121 353946:  199  503  839 1193 1559 1949 2339 2713 3137 354147:  211  509  853 1201 1567 1951 2341 2719 3163 354748:  223  521  857 1213 1571 1973 2347 2729 3167 355749:  227  523  859 1217 1579 1979 2351 2731 3169 355950:  229  541  863 1223 1583 1987 2357 2741 3181 3571

Reference

<< The Art of Computer Programming: Fundamental Algorithms >> VOLUME 1, DONALD E. KNUTH

0 0
原创粉丝点击