54 lines
1.6 KiB
COBOL
54 lines
1.6 KiB
COBOL
IDENTIFICATION DIVISION.
|
|
PROGRAM-ID. SIEVE.
|
|
* REMARKS. BYTE magazine benchmark.
|
|
* REMARKS. MS Cobol limits array sizes to 4095.
|
|
* REMARKS. Expected prime count: 1026.
|
|
ENVIRONMENT DIVISION.
|
|
CONFIGURATION SECTION.
|
|
DATA DIVISION.
|
|
WORKING-STORAGE SECTION.
|
|
01 MISC.
|
|
03 I PIC 9(4) COMP.
|
|
03 PRIME PIC 9(4) COMP.
|
|
03 K PIC 9(4) COMP.
|
|
03 TOTAL-PRIME-COUNT PIC 9(4) COMP.
|
|
02 SIEVETABLE.
|
|
04 FLAGS PIC 9 COMP OCCURS 4095 TIMES.
|
|
01 NUM-DISP PIC 9999.
|
|
|
|
PROCEDURE DIVISION.
|
|
MAIN.
|
|
PERFORM ITER-ROUTINE 10 TIMES.
|
|
MOVE TOTAL-PRIME-COUNT TO NUM-DISP.
|
|
DISPLAY NUM-DISP ' primes'.
|
|
STOP RUN.
|
|
|
|
ITER-ROUTINE.
|
|
MOVE ZEROES TO TOTAL-PRIME-COUNT.
|
|
PERFORM TFR VARYING I FROM 1 BY 1 UNTIL I = 4095.
|
|
PERFORM DCP THRU DCE VARYING I FROM 0 BY 1 UNTIL I = 4094.
|
|
|
|
TFR.
|
|
MOVE 1 TO FLAGS(I).
|
|
|
|
DCP.
|
|
IF FLAGS( I + 1 ) = 0
|
|
GO TO DCE.
|
|
COMPUTE PRIME = I + I + 3.
|
|
COMPUTE K = I + PRIME.
|
|
|
|
FIRST1.
|
|
IF K > 4095 GO TO NEXT1.
|
|
MOVE 0 TO FLAGS( K + 1 ).
|
|
COMPUTE K = PRIME + K.
|
|
GO TO FIRST1.
|
|
|
|
NEXT1.
|
|
ADD 1 TO TOTAL-PRIME-COUNT.
|
|
* MOVE PRIME TO NUM-DISP.
|
|
* DISPLAY 'FOUND PRIME = ' NUM-DISP.
|
|
|
|
DCE.
|
|
EXIT.
|
|
|