
 1000  *---------------------------------
 1010  * SIEVE PROGRAM:
 1020  * CALCULATES FIRST 1899 PRIMES IN .74 SECONDS!
 1030  *
 1040  * INSPIRED BY JIM GILBREATH
 1050  *   (SEE BYTE MAGAZINE, 9/81, PAGES 180-198.)
 1060  * AND BY WILLIAM ROBERT SAVOIE
 1070  *   4405 DELASHMITT RD. APT 15
 1080  *   HIXSON, TENN  37343
 1090  *---------------------------------
 1100  ARRAY  .EQ $3500    FLAG BYTE ARRAY
 1110  SIZE   .EQ 8192     SIZE OF FLAG ARRAY
 1120  *---------------------------------
 1130  * PAGE-ZERO VARIABLES
 1140  *---------------------------------
 1150  A.PNTR .EQ $06,07   POINTER TO FLAG ARRAY FOR OUTER LOOP
 1160  B.PNTR .EQ $08,09   POINTER TO FLAG ARRAY FOR INNER LOOP
 1170  PRIME  .EQ $1B,1C   LATEST PRIME NUMBER
 1180  COUNT  .EQ $1D,1E   # OF PRIMES SO FAR
 1190  TIMES  .EQ $1F      COUNT LOOP
 1200  *---------------------------------
 1210  * APPLE ROM ROUTINES USED
 1220  *---------------------------------
 1230  PRINTN .EQ $F940    PRINT 2 BYTE NUMBER FROM MONITOR
 1240  HOME   .EQ $FC58    CLEAR VIDEO
 1250  CR     .EQ $FD8E    CARRIAGE RETURN
 1260  LINE   .EQ $FD9E    PRINT "-"
 1270  BELL   .EQ $FBE2    SOUND BELL WHEN DONE
 1280  *---------------------------------
 1290  * RUN PROGRAM 100 TIMES FOR ACCURATE TIME MEASUREMENTS!
 1300  *---------------------------------
 1310  START  JSR HOME     CLEAR SCREEN
 1320         LDA #100     LOOP 100 TIMES
 1330         STA TIMES    SET COUNTER
 1340  .1     JSR GENERATE.PRIMES
 1350         LDA $400     TOGGLE SCREEN FOR VISIBLE INDICATOR
 1360         EOR #$80     OF ACTION
 1370         STA $400
 1380         DEC TIMES
 1390         BNE .1       LOOP
 1400         JSR BELL     READ WATCH!
 1410         LDY COUNT+1  GET HI BYTE OF COUNT
 1420         LDX COUNT
 1430         JSR PRINTN   PRINT PRIMES FOUND
 1440         RTS
 1450  *---------------------------------
 1460  *      GENERATE THE PRIMES
 1470  *---------------------------------
 1480  GENERATE.PRIMES
 1490         LDY #0       CLEAR INDEX
 1500         STY COUNT    CLEAR COUNT VARIABLE
 1510         STY COUNT+1
 1520         STY A.PNTR   SET UP POINTER FOR OUTER LOOP
 1530         LDA /ARRAY
 1540         STA A.PNTR+1
 1550         LDA #1       LOAD WITH ONE
 1560         LDX /SIZE      NUMBER OF PAGES TO STORE IN
 1570  *---------------------------------
 1580  * SET EACH ELEMENT IN ARRAY TO ONE
 1590  *---------------------------------
 1600  .1     STA (A.PNTR),Y  SET FLAG TO 1
 1610         INY          NEXT LOCATION
 1620         BNE .1       GO 256 TIMES
 1630         INC A.PNTR+1 POINT AT NEXT PAGE
 1640         DEX          NEXT PAGE
 1650         BNE .1       MORE PAGES
 1660  *---------------------------------
 1670  * SCAN ENTIRE ARRAY, LOOKING FOR A PRIME
 1680  *---------------------------------
 1690         LDA /ARRAY   SET A.PNTR TO BEGINNING AGAIN
 1700         STA A.PNTR+1
 1710  .2     LDY #0       CLEAR INDEX
 1720         LDA (A.PNTR),Y  LOOK AT NEXT FLAG
 1730         BEQ .6       NOT PRIME, ADVANCE POINTER
 1740  *---------------------------------
 1750  * CALCULATE CURRENT INDEX INTO FLAG ARRAY
 1760  *---------------------------------
 1770         SEC
 1780         LDA A.PNTR+1
 1790         SBC /ARRAY
 1800         TAX          SAVE HI-BYTE OF INDEX
 1810         LDA A.PNTR   LO-BYTE OF INDEX
 1820  *---------------------------------
 1830  * CALCULATE NEXT PRIME NUMBER WITH P=I+I+3
 1840  *---------------------------------
 1850         ASL          DOUBLE THE INDEX
 1860         TAY
 1870         TXA          HI-BYTE OF INDEX
 1880         ROL
 1890         TAX
 1900         TYA          NOW ADD 3
 1910         ADC #3
 1920         STA PRIME
 1930         BCC .3
 1940         INX
 1950  .3     STX PRIME+1
 1960  *---------------------------------
 1970  * FOLLOWING 4 LINES CHANGE ALGORITHM SLIGHTLY
 1980  * TO SPEED IT UP FROM .93 TO .74 SECONDS
 1990  *---------------------------------
 2000         TXA          TEST HIGH BYTE
 2010         BNE .5       PRIME > SQRT(16384)
 2020         CPY #127
 2030         BCS .5       PRIME > SQRT(16384)
 2040  *---------------------------------
 2050  * NOW CLEAR EVERY P-TH ENTRY AFTER P
 2060  *---------------------------------
 2070         LDY #0
 2080         LDA A.PNTR   USE CURRENT OUTER POINTER FOR INNER POINTER
 2090         STA B.PNTR
 2100         LDA A.PNTR+1
 2110         STA B.PNTR+1
 2120         CLC          BUMP ARRAY POINTER BY P
 2130  .4     LDA B.PNTR   BUMP TO NEXT SLOT
 2140         ADC PRIME
 2150         STA B.PNTR
 2160         LDA B.PNTR+1
 2170         ADC PRIME+1
 2180         STA B.PNTR+1
 2190         CMP /ARRAY+SIZE     SEE IF BEYOND END OF ARRAY
 2200         BCS .5       YES, FINISHED CLEARING
 2210         TYA          NO, CLEAR ENTRY IN ARRAY
 2220         STA (B.PNTR),Y
 2230         BEQ .4       ...ALWAYS
 2240  *---------------------------------
 2250  * NOW COUNT PRIMES FOUND  (C=C+1)
 2260  *---------------------------------
 2270  .5
 2280  *      JSR PRINTP   PRINT PRIME
 2290         INC COUNT
 2300         BNE .6 
 2310         INC COUNT+1
 2320  *---------------------------------
 2330  * ADVANCE OUTER POINTER AND TEST IF FINISHED
 2340  *---------------------------------
 2350  .6     INC A.PNTR
 2360         BNE .7
 2370         INC A.PNTR+1
 2380  .7     LDA A.PNTR+1
 2390         CMP /ARRAY+SIZE
 2400         BCC .2
 2410         RTS
 2420  *---------------------------------
 2430  * OPTIONAL PRINT PRIME SUBROUTINE
 2440  *---------------------------------
 2450  PRINTP LDY PRIME+1  HI BYTE 
 2460         LDX PRIME
 2470         JSR PRINTN   PRINT DECIMAL VAL
 2480         JSR LINE     VIDEO "-" OUT
 2490         RTS

