2017-10-16 18:09:39 +02:00
|
|
|
This is a quick and dirty way to find prime numbers in a set.
|
|
|
|
|
2018-04-25 18:57:28 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
2020-08-09 05:27:42 +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 ]
|
2019-04-24 16:02:15 +02:00
|
|
|
[ mod n:-zero? ] choose ] a:filter ;
|
2017-10-16 18:09:39 +02:00
|
|
|
---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
|
|
|
}}
|
2018-04-25 18:57:28 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And a test:
|
|
|
|
|
2018-04-25 18:57:28 +02:00
|
|
|
~~~
|
2017-10-23 16:53:44 +02:00
|
|
|
:create-set (-a)
|
2019-08-12 19:00:59 +02:00
|
|
|
here #1000 , #2 #1002 [ dup , n:inc ] times drop ;
|
2017-10-23 16:53:44 +02:00
|
|
|
|
2019-04-24 16:02:15 +02:00
|
|
|
create-set get-primes [ n:put sp ] a:for-each
|
2018-04-25 18:57:28 +02:00
|
|
|
~~~
|