Primes by Trial Division - HES VIC FORTH
6 October 2016
VIC FORTH lacks 2* and uses ENDIF instead of THEN. Otherwise the same as HCCS FORTH.
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 ENDIF LOOP ; : PRIMES INITIALIZE CR 2 ISPRIME! MAXINT 3 DO I ISPRIME? IF ISPRIME! ELSE DROP ENDIF MAXPRIMES >R #PRIMES R> = IF LEAVE ENDIF LOOP ;