Primes by Trial Division - HCCS FORTH
7 January 2017
HCCS Forth has no PICK, no semantics for instantiating an infinite DO loop, and will not allow a word to wrap at 80 columns.
Extended fig-FORTH [ Atari 8-bits ]
G.R. Gaukel's 6509.FORTH [ C= B128/256 ]
CELLS is 2 *.
IPC FORTH [ HP Integral PC ]
CELLS is 4 *.
32767 CONSTANT MAXINT : CELLS 2* ; : #PRIME@ 2 + CELLS + ; : #PRIME >R DUP R> #PRIME@ @ ; : #PRIMES DUP 1 CELLS + @ ; : #PRIMES++ #PRIMES 1+ OVER 1 CELLS + ! ; : MAXPRIMES DUP @ ; : NOTPRIME DROP 0 ; : DIVIDES? MOD 0= ; : INITIALIZE DUP >R HERE DUP R> 2 + CELLS ALLOT ROT SWAP ! 0 OVER 1 CELLS + ! ; : ISPRIME! DUP . CR OVER #PRIMES #PRIME@ ! #PRIMES++ ; : ISPRIME? OVER #PRIMES 0 DO I #PRIME >R OVER R> DIVIDES? IF NOTPRIME LEAVE THEN LOOP ; : PRIMES DECIMAL INITIALIZE CR 2 ISPRIME! MAXINT 3 DO I ISPRIME? IF ISPRIME! ELSE DROP THEN MAXPRIMES >R #PRIMES R> = IF LEAVE THEN LOOP ;