
; Sieve of Eratosthenes
; print Prime numbers 

          a@/#  ; a array of primes
          b@/U  ; var
          c@@U  ; var
          d@:U  ; var
          t@IU  ; var
          z@VA  ; const zero     
          i@E:  ; const one (integer)

          FRACTIONS +0->z  ; initialize const z
          +z ->b ->c ->d   ; init vars to zero

; read d, the max prime to find, from papertape
; (should be <= 100)

          SUBROUTINE H ->d

; print CRLF and the max prime (an int)

          SUBROUTINE Y     ; CR LF
          SUBROUTINE Y     ; CR LF
          +d SUBROUTINE P  ; print d
          SUBROUTINE Y     ; CR LF
          SUBROUTINE Y     ; CR LF

; fill array with -1
          
          LOOP 100r         
          -i ->ar           
          REPEAT r

; remove multiples from array
; var b is the number being checked as prime
; var c is the first multiple of b 

          +i ->b           ; b=1
ENTRY A   
          +b +i ->b        ; b=b+1. 
          +b SUBROUTINE P  ; print b
          +b +b -d TESTA C ; if (b>=d/2) exit 
          ; as all prime candidates processed
          
          +b ->c           ; c=b. 
          
ENTRY B   +c +b ->c        ; c=c+b. 
          ; Now c is next multiple of b

          +c -d TESTA A    ; if (c>=d) then 
          ; goto ENTRY A because c is too big, 
          ; so we try next number

          +c +c ->t t->r   ; let index r = 2*c
          ; each entry in array a takes 2 short
          ; lines
                    
          +i ->ar          ; a[r]=1 flag this 
                           ; number as no-prime
          CONTROL B        ; goto ENTRY B to 
                           ; flag next multiple
                           ; of b

ENTRY C   SUBROUTINE Y     ; print CR LF
          SUBROUTINE Y

; print primes table

          +i ->b           ; b=1

ENTRY D   +b +i ->b
          +b -d TESTA E    ; end of array
          
          +b +b ->t t->r   ; let index r = 2*b
                   
          +ar TESTA D      ; if (a[r]>=0) then 
                           ; goto D because b 
                           ; flagged as not 
                           ; prime 
          +b SUBROUTINE P  ; b is a prime, 
                           ; so print it
          CONTROL D

; end of program, dynamic stop

ENTRY E   CONTROL E

WRITE 1 ; save this prog as autocode routine 1
START 1 ; run autocode routine 1

; Generates primes <= this value 
; (should be <= 100)
100+

