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
|
|
|
{{
|
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 ]
|
2019-02-21 05:19:14 +01:00
|
|
|
[ mod n:-zero? ] choose ] array: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-01-03 15:18:43 +01:00
|
|
|
here #3000 , #2 #3002 [ dup , n:inc ] times drop ;
|
2017-10-23 16:53:44 +02:00
|
|
|
|
2019-02-21 05:19:14 +01:00
|
|
|
create-set get-primes [ n:put sp ] array:for-each
|
2018-04-25 18:57:28 +02:00
|
|
|
~~~
|