This is a COBOL program written for the AS/400.
Generates all prime numbers from 1 to 6000000.
Is extremely fast.
Contains a simple but fast square root algorithm.
IDENTIFICATION DIVISION. * * Prime Number Generator * By Wendell Clifton * Written 05/01/1997 * * Written for the AS/400. * Includes a nifty square root routine. * PROGRAM-ID. UWLC0103UP. AUTHOR. WENDELL CLIFTON ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT PRTOUT ASSIGN TO PRINTER-QSYSPRT. DATA DIVISION. FILE SECTION. FD PRTOUT RECORD CONTAINS 132 CHARACTERS LABEL RECORDS ARE STANDARD DATA RECORD IS PRTOUT-REC. 01 PRTOUT-REC PIC X(132). WORKING-STORAGE SECTION. 01 PRTOUT-NAME PIC X(08) VALUE "PRTOUT". 01 PRTOUT-NAME-DT PIC X(60) VALUE SPACES. 01 PRTOUT-STATUS. 05 PRTOUT-OPEN-SW PIC X(01) VALUE ZEROES. 01 PRTOUT-LN-COUNT PIC 9(02) VALUE ZEROES. 01 PRTOUT-PG-COUNT PIC 9(06) VALUE ZEROES. 01 PRTOUT-FSPC-LINES PIC 9(02) VALUE ZEROES. / 01 PRM-TABLE. 05 PRM-TBL PIC X(01) OCCURS 3000000. * 01 WS-FIELDS. 05 ZOFF PIC X(01) VALUE "0". 05 ZON PIC X(01) VALUE "1". * 05 PRM-BASE PIC S9(05) COMP-3. 05 PRM-STEP PIC S9(05) COMP-3. 05 PRM-SUB PIC S9(07) COMP-3. * 05 PRM-HIGH PIC S9(07) COMP-3 VALUE 6000000. 05 PRM-HALF PIC S9(07) COMP-3. 05 PRM-SQRT PIC S9(05) COMP-3. * 05 SQR-ROOT PIC S9(07) COMP-3. 05 SQR-NMBR PIC S9(07) COMP-3. 05 SQR-DONE PIC S9(07) COMP-3. 05 SQR-DIFF PIC S9(07) COMP-3 VALUE 2. * 01 PRT-REC. 05 PRT-NBR PIC Z(08) OCCURS 16. / PROCEDURE DIVISION. MAINLINE SECTION. MAINLINE-SECTION. * * Find square root of high number. * MOVE PRM-HIGH TO SQR-ROOT, SQR-DONE PERFORM UNTIL SQR-DONE < SQR-DIFF COMPUTE SQR-NMBR = PRM-HIGH / SQR-ROOT COMPUTE SQR-DONE = SQR-ROOT - SQR-NMBR COMPUTE SQR-ROOT = (SQR-ROOT + SQR-NMBR) / 2 END-PERFORM * * Setup prime number generator. * MOVE SQR-ROOT TO PRM-SQRT COMPUTE PRM-HALF = PRM-HIGH / 2 * * Prime Number Generator * MOVE SPACES TO PRM-TABLE MOVE ZEROES TO PRM-BASE PERFORM VARYING PRM-STEP FROM 3 BY 2 UNTIL PRM-STEP > PRM-SQRT ADD 1 TO PRM-BASE IF PRM-TBL (PRM-BASE) = SPACES ADD PRM-BASE, PRM-STEP GIVING PRM-SUB PERFORM VARYING PRM-SUB FROM PRM-SUB BY PRM-STEP UNTIL PRM-SUB > PRM-HALF MOVE ZON TO PRM-TBL (PRM-SUB) END-PERFORM END-IF END-PERFORM * * Print prime numbers * PERFORM OPEN-OUTPUT-PRTOUT * MOVE SPACES TO PRT-REC MOVE PRM-SQRT TO PRT-NBR (1) MOVE PRT-REC TO PRTOUT-REC PERFORM WRITE-PRTOUT PERFORM WRITE-PRTOUT * MOVE 1 TO PRT-NBR (1) MOVE 2 TO PRT-NBR (2) MOVE 2 TO PRM-STEP PERFORM VARYING PRM-SUB FROM 1 BY 1 UNTIL PRM-SUB > PRM-HALF IF PRM-TBL (PRM-SUB) = SPACES ADD 1 TO PRM-STEP COMPUTE PRT-NBR (PRM-STEP) = PRM-SUB + PRM-SUB + 1 IF PRM-STEP = 16 MOVE ZEROES TO PRM-STEP MOVE PRT-REC TO PRTOUT-REC PERFORM WRITE-PRTOUT MOVE SPACES TO PRT-REC END-IF END-IF END-PERFORM IF PRM-STEP > ZEROES MOVE PRT-REC TO PRTOUT-REC PERFORM WRITE-PRTOUT END-IF * PERFORM CLOSE-PRTOUT GOBACK. / OPEN-OUTPUT-PRTOUT. MOVE ZEROS TO PRTOUT-STATUS. OPEN OUTPUT PRTOUT. MOVE ZON TO PRTOUT-OPEN-SW. MOVE SPACES TO PRTOUT-REC. MOVE 0 TO PRTOUT-PG-COUNT. MOVE 0 TO PRTOUT-LN-COUNT. * OPEN-PURGE-OUTPUT-PRTOUT. MOVE ZEROS TO PRTOUT-STATUS. OPEN OUTPUT PRTOUT. MOVE ZON TO PRTOUT-OPEN-SW. MOVE SPACES TO PRTOUT-REC. MOVE 0 TO PRTOUT-PG-COUNT. MOVE 0 TO PRTOUT-LN-COUNT. * ADVANCE-PRTOUT-PAGE. WRITE PRTOUT-REC BEFORE ADVANCING PAGE. MOVE SPACES TO PRTOUT-REC. ADD 1 TO PRTOUT-PG-COUNT. MOVE 0 TO PRTOUT-LN-COUNT. * ADVANCE-PRTOUT-LINE. WRITE PRTOUT-REC BEFORE ADVANCING PRTOUT-FSPC-LINES. MOVE SPACES TO PRTOUT-REC. ADD PRTOUT-FSPC-LINES TO PRTOUT-LN-COUNT. * WRITE-PRTOUT. WRITE PRTOUT-REC BEFORE ADVANCING 1 LINE. MOVE SPACES TO PRTOUT-REC. ADD 1 TO PRTOUT-LN-COUNT. * CLOSE-PRTOUT. CLOSE PRTOUT. MOVE ZOFF TO PRTOUT-OPEN-SW.
Wendell's Wonderland: Wendell's Home Page Wendell's Prime Numbers Page: The prime numbers page |
Updated 05/21/1999 |
Send questions or comments to: Wendell Clifton ![]() wclifton@rocketmail.com |
May 6, 1999. |