ab1a93a1a8
FossilOrigin-Name: 7ea7b110de04a5e330a4ff2dc9bc99414607b4daa8818c945890107ca95ad030
24 lines
463 B
Forth
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
|
|
````
|