Skip to content

Commit

Permalink
Merge branch 'for-slava' of git://github.com/samueltardieu/factor
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava Pestov committed May 21, 2011
2 parents 1fa9793 + 4f8e8e6 commit 7d7ca0e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion basis/math/primes/primes-docs.factor
Expand Up @@ -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" } }
Expand Down
5 changes: 5 additions & 0 deletions basis/math/primes/primes-tests.factor
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion basis/math/primes/primes.factor
@@ -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 ;
Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit 7d7ca0e

Please sign in to comment.