Primes by Trial Division - COBOL
25 April 2017
ACUCOBOL-85
WS-COUNT usage is DISPLAY.
Micro Focus COBOL for UNIX
Remove or comment out the entire IDENTIFICATION DIVISION paragraph.
RM/COBOL
WS-COUNT usage is DISPLAY. Requires an ENVIRONMENT DIVISION paragraph. (See also: Nevada COBOL)
VAX COBOL for VMS
WS-COUNT usage is DISPLAY. Compile with /ANSI_FORMAT.
IDENTIFICATION DIVISION. PROGRAM-ID. NPRIMES. AUTHOR. bear. DATE-WRITTEN. April 25, 2017. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM PIC 9(5) USAGE IS COMP VALUE 100. *01 WS-NUM PIC 9(5) USAGE IS COMP VALUE 1000. *01 WS-NUM PIC 9(5) USAGE IS COMP VALUE 10000. 01 WS-FOUND PIC 9(5) USAGE IS COMP VALUE ZERO. 01 WS-COUNT PIC 9(6) USAGE IS COMP VALUE 2. 01 WS-DIV PIC 9(6) USAGE IS COMP. 01 WS-REM PIC 9(6) USAGE IS COMP. 01 WS-PRIMES USAGE IS COMP. 05 WS-PRIME PIC 9(6) OCCURS 100 TIMES INDEXED BY I. * 05 WS-PRIME PIC 9(6) OCCURS 1000 TIMES INDEXED BY I. * 05 WS-PRIME PIC 9(6) OCCURS 10000 TIMES INDEXED BY I. PROCEDURE DIVISION. A000-MAIN. PERFORM C000-ISPRIME. PERFORM B000-CHECKPRIME THRU C000-ISPRIME UNTIL WS-FOUND IS EQUAL TO WS-NUM. STOP RUN. B000-CHECKPRIME. PERFORM D000-DIVIDES VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WS-FOUND. C000-ISPRIME. ADD 1 TO WS-FOUND. DISPLAY WS-COUNT. MOVE WS-COUNT TO WS-PRIME(WS-FOUND). ADD 1 TO WS-COUNT. D000-DIVIDES. DIVIDE WS-COUNT BY WS-PRIME(I) GIVING WS-DIV REMAINDER WS-REM. IF WS-REM IS EQUAL TO ZERO PERFORM D001-NEXTNUM. D001-NEXTNUM. ADD 1 TO WS-COUNT. SET I TO 1. SET I DOWN BY 1.