]> gitweb.factorcode.org Git - factor.git/commitdiff
New module math.primes
authorSamuel Tardieu <sam@rfc1149.net>
Thu, 27 Dec 2007 02:59:39 +0000 (03:59 +0100)
committerSamuel Tardieu <sam@rfc1149.net>
Thu, 27 Dec 2007 15:52:16 +0000 (16:52 +0100)
extra/math/primes/authors.txt [new file with mode: 0644]
extra/math/primes/primes-docs.factor [new file with mode: 0644]
extra/math/primes/primes-tests.factor [new file with mode: 0644]
extra/math/primes/primes.factor [new file with mode: 0644]
extra/math/primes/summary.txt [new file with mode: 0644]

diff --git a/extra/math/primes/authors.txt b/extra/math/primes/authors.txt
new file mode 100644 (file)
index 0000000..f3b0233
--- /dev/null
@@ -0,0 +1 @@
+Samuel Tardieu
diff --git a/extra/math/primes/primes-docs.factor b/extra/math/primes/primes-docs.factor
new file mode 100644 (file)
index 0000000..1077659
--- /dev/null
@@ -0,0 +1,30 @@
+USING: help.markup help.syntax ;
+IN: math.primes
+
+{ next-prime prime? } related-words
+
+HELP: next-prime
+{ $values { "n" "a positive integer" } { "p" "a prime number" } }
+{ $description "Return the next prime number greater than " { $snippet "n" } "." } ;
+
+HELP: prime?
+{ $values { "n" "an integer" } { "?" "a boolean" } }
+{ $description "Test if an integer is a prime number." } ;
+
+{ lprimes lprimes-from primes-upto primes-between } related-words
+
+HELP: lprimes
+{ $values { "list" "a lazy list" } }
+{ $description "Return a sorted list containing all the prime numbers." } ;
+
+HELP: lprimes-from
+{ $values { "n" "an integer" } { "list" "a lazy list" } }
+{ $description "Return a sorted list containing all the prime numbers greater or equal to " { $snippet "n" } "." } ;
+
+HELP: primes-upto
+{ $values { "n" "an integer" } { "seq" "a sequence" } }
+{ $description "Return a sequence containing all the prime numbers smaller or equal to " { $snippet "n" } "." } ;
+
+HELP: primes-between
+{ $values { "low" "an integer" } { "high" "an integer" } { "seq" "a sequence" } }
+{ $description "Return a sequence containing all the prime numbers between " { $snippet "low" } " and " { $snippet "high" } "." } ;
diff --git a/extra/math/primes/primes-tests.factor b/extra/math/primes/primes-tests.factor
new file mode 100644 (file)
index 0000000..b1bcf79
--- /dev/null
@@ -0,0 +1,10 @@
+USING: arrays math.primes tools.test lazy-lists ;
+
+{ 1237 } [ 1234 next-prime ] unit-test
+{ f t } [ 1234 prime? 1237 prime? ] unit-test
+{ { 2 3 5 7 11 13 17 19 23 29 } } [ 10 lprimes ltake list>array ] unit-test
+{ { 101 103 107 109 113 } } [ 5 100 lprimes-from ltake list>array ] unit-test
+{ { 1000117 1000121 } } [ 2 1000100 lprimes-from ltake list>array ] unit-test
+{ { 999983 1000003 } } [ 2 999982 lprimes-from ltake list>array ] unit-test
+{ { 2 3 5 7 } } [ 10 primes-upto >array ] unit-test
+{ { 999983 1000003 } } [ 999982 1000010 primes-between >array ] unit-test
diff --git a/extra/math/primes/primes.factor b/extra/math/primes/primes.factor
new file mode 100644 (file)
index 0000000..68ab5b3
--- /dev/null
@@ -0,0 +1,49 @@
+! Copyright (C) 2007 Samuel Tardieu.
+! See http://factorcode.org/license.txt for BSD license.
+USING: combinators kernel lazy-lists math math.functions math.miller-rabin
+       math.primes.list math.ranges sequences sorting ;
+IN: math.primes
+
+<PRIVATE
+
+: find-prime-miller-rabin ( n -- p )
+  dup miller-rabin [ 2 + find-prime-miller-rabin ] unless ; foldable
+
+PRIVATE>
+
+: next-prime ( n -- p )
+  dup 999983 < [
+    primes-under-million [ [ <=> ] binsearch 1+ ] keep nth
+  ] [
+    next-odd find-prime-miller-rabin
+  ] if ; foldable
+
+: prime? ( n -- ? )
+  dup 1000000 < [
+    dup primes-under-million [ <=> ] binsearch* =
+  ] [
+    miller-rabin
+  ] if ; foldable
+
+: lprimes ( -- list )
+  0 primes-under-million seq>list
+  1000003 [ 2 + find-prime-miller-rabin ] lfrom-by
+  lappend ;
+
+: lprimes-from ( n -- list )
+  dup 3 < [ drop lprimes ] [  1- next-prime [ next-prime ] lfrom-by ] if ;
+
+: primes-upto ( n -- seq )
+  {
+    { [ dup 2 < ] [ drop { } ] }
+    { [ dup 1000003 < ]
+      [ primes-under-million [ [ <=> ] binsearch 1+ 0 swap ] keep <slice> ] }
+    { [ t ]
+      [ primes-under-million 1000003 lprimes-from
+        rot [ <= ] curry lwhile list>array append ] }
+  } cond ; foldable
+
+: primes-between ( low high -- seq )
+  primes-upto
+  >r 1- next-prime r>
+  [ [ <=> ] binsearch ] keep [ length ] keep <slice> ; foldable
diff --git a/extra/math/primes/summary.txt b/extra/math/primes/summary.txt
new file mode 100644 (file)
index 0000000..41b4197
--- /dev/null
@@ -0,0 +1,2 @@
+Prime numbers test and generation
+