Add math.primes:nprimes

db4
Samuel Tardieu 2011-05-20 12:38:27 +02:00
parent 9bd1c1b771
commit 23fe76160a
3 changed files with 13 additions and 2 deletions

View File

@ -11,7 +11,11 @@ HELP: prime?
{ $values { "n" "an integer" } { "?" "a boolean" } }
{ $description "Test if an integer is a prime number." } ;
{ primes-upto primes-between } related-words
{ nprimes primes-upto primes-between } related-words
HELP: nprimes
{ $values { "n" "a non-negative integer" } { "seq" "a sequence" } }
{ $description "Return a sequence containing the " { $snippet "n" } " first primes numbers." } ;
HELP: primes-upto
{ $values { "n" "an integer" } { "seq" "a sequence" } }

View File

@ -8,6 +8,11 @@ IN: math.primes.tests
{ { 2 } } [ 2 primes-upto >array ] unit-test
{ { } } [ 1 primes-upto >array ] unit-test
{ { 999983 1000003 } } [ 999982 1000010 primes-between >array ] unit-test
{ { } } [ 0 nprimes ] unit-test
{ { 2 3 5 7 } } [ 4 nprimes ] unit-test
{ t } [ 1000 nprimes [ prime? ] all? ] unit-test
{ 1000 } [ 1000 nprimes length ] unit-test
{ 1000 } [ 1000 nprimes last primes-upto length ] unit-test
{ { 4999963 4999999 5000011 5000077 5000081 } }
[ 4999962 5000082 primes-between >array ] unit-test

View File

@ -1,6 +1,6 @@
! Copyright (C) 2007-2009 Samuel Tardieu.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators combinators.short-circuit fry kernel math
USING: combinators combinators.short-circuit fry kernel make math
math.bitwise math.functions math.order math.primes.erato
math.primes.erato.private math.primes.miller-rabin math.ranges
literals random sequences sets vectors ;
@ -66,6 +66,8 @@ PRIVATE>
: primes-upto ( n -- seq ) 2 swap primes-between ;
: nprimes ( n -- seq ) [ 2 swap [ dup , next-prime ] times ] { } make nip ;
: coprime? ( a b -- ? ) gcd nip 1 = ; foldable
: random-prime ( numbits -- p )