retroforth/example/Primes.forth
crc ab1a93a1a8 fix an issue that was causing memory corruption in the example/Primes.forth
FossilOrigin-Name: 7ea7b110de04a5e330a4ff2dc9bc99414607b4daa8818c945890107ca95ad030
2017-10-23 14:53:44 +00:00

24 lines
463 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 #7000 , #2 #7002 [ dup , n:inc ] times drop ;
create-set get-primes [ putn sp ] set:for-each
````