Primes by Trial Division - GraFORTH
8 October 2016
GraFORTH is a graphics-oriented FORTH for the Apple ][ with gratuitous changes to the standard vocabulary to make it easier for neophytes to manage. All definitions must be entered as a single line.
There are many unfortunate omissions (LEAVE, ALLOT, CONSTANT) but like valFORTH, it has a 1-based PICK, and no semantics for instantiating an infinite DO loop.
2816 VARIABLE USERAREA 32767 VARIABLE MAXINT : LEAVE POP PULL DUP PUSH PUSH ; : CELLS 2 * ; : #PRIME@ 2 + CELLS + ; : #PRIME PUSH DUP PULL #PRIME@ PEEKW ; : #PRIMES DUP 1 CELLS + PEEKW ; : #PRIMES++ #PRIMES 1 + OVER 1 CELLS + POKEW ; : MAXPRIMES DUP PEEKW ; : NOTPRIME DROP 0 ; : DIVIDES? MOD 0 = ; : INITIALIZE USERAREA SWAP OVER POKEW 0 OVER 1 CELLS + POKEW ; : ISPRIME! DUP . CR OVER #PRIMES #PRIME@ POKEW #PRIMES++ ; : ISPRIME? OVER #PRIMES 0 DO I #PRIME 3 PICK SWAP DIVIDES? IF NOTPRIME LEAVE THEN LOOP ; : PRIMES INITIALIZE CR 2 ISPRIME! MAXINT 3 DO I ISPRIME? IF ISPRIME! ELSE DROP THEN MAXPRIMES PUSH #PRIMES PULL = IF LEAVE THEN LOOP ;