retroforth/example/Primes.forth
crc 1fc919ff7b fix example bug
FossilOrigin-Name: 2ab398e6752ea3da5585f0ab46c71e3f9e752f31ffd9cff282b002127cb86cd4
2019-01-03 14:18:43 +00:00

24 lines
460 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 ] set:filter ;
---reveal---
:get-primes (s-s)
#2 !NextPrime
dup fetch [ extract &NextPrime v:inc ] times ;
}}
~~~
And a test:
~~~
:create-set (-a)
here #3000 , #2 #3002 [ dup , n:inc ] times drop ;
create-set get-primes [ n:put sp ] set:for-each
~~~