73493f92f4
FossilOrigin-Name: c9ea59275146e3ccddbcfc3c8e2aa12e0c1f51f6d54c9674cf7d9a3b0500bf7c
24 lines
460 B
Forth
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 #7000 , #2 #7002 [ dup , n:inc ] times drop ;
|
|
|
|
create-set get-primes [ n:put sp ] set:for-each
|
|
~~~
|