2017-10-16 18:09:39 +02:00
|
|
|
This is a quick and dirty way to find prime numbers in a set.
|
|
|
|
|
|
|
|
````
|
|
|
|
{{
|
2017-10-23 16:53:44 +02:00
|
|
|
#2 'NextPrime var<n>
|
2017-10-16 18:09:39 +02:00
|
|
|
:extract (s-s)
|
2017-10-23 16:53:44 +02:00
|
|
|
[ @NextPrime dup-pair eq?
|
2017-10-16 18:09:39 +02:00
|
|
|
[ drop-pair TRUE ]
|
|
|
|
[ mod n:-zero? ] choose ] set:filter ;
|
|
|
|
---reveal---
|
|
|
|
:get-primes (s-s)
|
2017-10-23 16:53:44 +02:00
|
|
|
#2 !NextPrime
|
|
|
|
dup fetch [ extract &NextPrime v:inc ] times ;
|
2017-10-16 18:09:39 +02:00
|
|
|
}}
|
|
|
|
````
|
|
|
|
|
|
|
|
And a test:
|
|
|
|
|
|
|
|
````
|
2017-10-23 16:53:44 +02:00
|
|
|
:create-set (-a)
|
|
|
|
here #7000 , #2 #7002 [ dup , n:inc ] times drop ;
|
|
|
|
|
|
|
|
create-set get-primes [ putn sp ] set:for-each
|
2017-10-16 18:09:39 +02:00
|
|
|
````
|