JB PLANJOB,:xxxx
#
#     PLAN 'PRIMES'
#
IN SOURCE PPRI(1/PLAN),T////
#PRO           PPRI80/PRIMES
#
#
#              FIND/PRINT PRIME NUMBERS USING SIEVE OF ERATOSTHENES
#
#
#DEF           TABSIZE=250         [ SIEVE SIZE - RANGE 2..32500
#                                  [ EACH BIT (OF THE 24 BITS IN A WORD)
#                                  [ REPRESENTS A NUMBER FROM 0 UPWARDS
#DEF           TOPNUM=TABSIZE*24   [ HIGHEST NUMBER TO BE CHECKED
#
#
#UPP           SIEVE(TABSIZE)
#
#
#LOW
LPHOF          2/0,0,1,/LPHOF+4.3,#11
LPLINE         2/0,0,45,/LPLINE+4.3,#42
               44H                   XXXXXXX IS A PRIME NUMBER
LPHEAD         2/0,0,57,/LPHEAD+4.3,#41
               36H           LIST OF PRIME NUMBERS ON
HEAD           20HXX/XX/XX AT XX/XX/XX
HIGHNUM        TOPNUM
LOCATIONS      TABSIZE-1
#
#
#PRO
#ENT           0
#
      ALLOT 0  2                   [ ALLOCATE *LP0
      LDXC  0  9                   [ FETCH REPLY
      BCC      *+3                 [ J IF OKAY
      SUSWT    2HLP                [ HALT:- LP
      BRN      *-4                 [ TRY AGAIN
#
#     PRINT HEADING
#
      GIVE  4  1                   [ GET CHARACTER DATE
      STO   4  HEAD                [    AND STORE IN HEADING
      STO   5  HEAD+1
      GIVE  4  2                   [ GET CHARACTER TIME
      STO   4  HEAD+3              [    AND STORE IN HEADING
      STO   5  HEAD+4
      PERI  0  LPHOF               [ THROW TO HOF
      PERI  0  LPHEAD              [ PRINT HEADING
      SUSBY 0  2                   [ WAIT FOR TRANSFER
#
#     INITIALISE SIEVE - MARK ALL ENTRIES PRIME EXCEPT FOR 0 AND 1
#
      LDX   2  '/SIEVE'            [ X2 := PTR SIVE TABLE
      LDX   4  '#17777777'
      STO   4  0(2)                [ INITIALISE 1ST TABLE ENTRY
      NGN   4  1
      LDX   7  LOCATIONS           [ X7 := COUNT OF ADDITIONAL ENTRIES
      ADN   2  1
      STO   4  0(2)                [ INITIALISE TABLE ENTRY
      BCT   7  *-2                 [ REPEAT
      LDN   1  0                   [ X1 := CURRENT PRIME CANDIDATE
#
#     MAIN LOOP CHECKING SIEVE ENTRIES FOR PRIME NUMBERS
#
LOOP  ADN   1  1                   [ NEXT NUMBER
      BXGE  1  HIGHNUM,FIN         [ JMP, COMPLETE TABLE CHECKED
      LDX   3  1                   [ X3 := CURRENT NUMBER TO TEST
      DVS   2  '24'                [ CONVERT TO WORD/BIT
      ADX   3  '/SIEVE'            [ X3:= PTR, X2:= BIT NUMBER
      LDCT  4  #400                [ SET UP MASK FOR NUMBER IN TABLE
      SRC   4  0(2)                [ ROTATE TO CORRECT POSITION
      ANDX  4  0(3)                [ TEST CURRENT NUMBER
      BZE   4  LOOP                [ J IF NOT PRIME
      LDX   7  1                   [ X7 := CURRENT PRIME NUMBER
#
#     PRIME NUMBER FOUND - PRINT IT
#
      STO   7  4                   [ STORE PRIME FOR OUTPUT
      MPY   4  '#32657713'         [ TIMES CONVERSION FACTOR
      LDCT  3  #607                [ SET CT/MOD TO '7/0.3'
      MODE     1                   [ SET ZERO SUPPRESSION MODE
      CBD   4  LPLINE+9(3)         [ CONVERT DIGIT
      BCHX  3  *-1                 [ REPEAT FOR 5 DIGITS
      PERI  0  LPLINE              [ PRINT PRIME NUMBER
      SUSBY 0  2                   [ WAIT FOR TRANSFER
#
#     SECONDARY LOOP - MARK MULTIPLES OF CURRENT PRIME AS NON-PRIME
#
MARK  ADX   7  1                   [ X7 := NEXT MULTIPLE
      BXGE  7  HIGHNUM,LOOP        [ JMP, COMPLETE TABLE CHECKED
      LDX   3  7                   [ X3 := CURRENT NUMBER TO TEST
      DVS   2  '24'                [ CONVERT TO WORD/BIT
      ADX   3  '/SIEVE'            [ X3:= PTR, X2:= BIT NUMBER
      NGNC  4  1                   [ SET UP MASK FOR NUMBER IN TABLE
      SRC   4  0(2)                [ ROTATE TO CORRECT POSITION
      ANDS  4  0(3)                [ MARK NUMBER NOT PRIME
      BRN      MARK                [ REPEAT
#
#     END OF RUN
#
FIN   PERI  0  LPHOF               [ THROW TO HOF
      SUSBY 0  2                   [ WAIT FOR TRANSFER
      REL   0  2                   [ RELEASE *LP0
      DEL      2HOK                [ DELETE:- OK
#
#END
////
PLANCOMP *IDPPRI
LF PPRICOMPLIST,*LP,PA
LO PROGRAM PPRI
CE !
AS *LP0,!
LF !,*LP,PA
ER !
EN 0
LD
EJ ALL
****
