retroforth/example/primes.retro
crc 9e03717deb normalize names for examples (with a couple of exceptions), closes #38
FossilOrigin-Name: 088675e452ed86a712563c8b2597fe4d47da59bdea0e40becdd1e028a84c47b0
2021-01-24 01:13:04 +00:00

24 lines
455 B
Forth

This is a quick and dirty way to find prime numbers in a set.
~~~
{{
#2 'NextPrime var-n
:extract (s-s)
[ @NextPrime dup-pair eq?
[ drop-pair TRUE ]
[ mod n:-zero? ] choose ] a:filter ;
---reveal---
:get-primes (s-s)
#2 !NextPrime
dup fetch [ extract &NextPrime v:inc ] times ;
}}
~~~
And a test:
~~~
:create-set (-a)
here #1000 , #2 #1002 [ dup , n:inc ] times drop ;
create-set get-primes [ n:put sp ] a:for-each
~~~