diff --git a/basis/bit-arrays/bit-arrays-tests.factor b/basis/bit-arrays/bit-arrays-tests.factor index 24c27fa15b..1de49d353d 100644 --- a/basis/bit-arrays/bit-arrays-tests.factor +++ b/basis/bit-arrays/bit-arrays-tests.factor @@ -78,3 +78,5 @@ IN: bit-arrays.tests } bit-array>integer ] unit-test [ 49 ] [ 49 dup set-bits [ ] count ] unit-test + +[ HEX: 100 ] [ ?{ f f f f f f f f t } bit-array>integer ] unit-test diff --git a/basis/bit-arrays/bit-arrays.factor b/basis/bit-arrays/bit-arrays.factor index 18796fbfed..f1ba71ce1e 100644 --- a/basis/bit-arrays/bit-arrays.factor +++ b/basis/bit-arrays/bit-arrays.factor @@ -83,7 +83,7 @@ M: bit-array byte-length length 7 + -3 shift ; ] if ; : bit-array>integer ( bit-array -- n ) - 0 swap underlying>> dup length [ + 0 swap underlying>> dup length [ alien-unsigned-1 swap 8 shift bitor ] with each ; diff --git a/extra/math/primes/authors.txt b/basis/math/primes/authors.txt similarity index 100% rename from extra/math/primes/authors.txt rename to basis/math/primes/authors.txt diff --git a/extra/math/primes/erato/authors.txt b/basis/math/primes/erato/authors.txt similarity index 100% rename from extra/math/primes/erato/authors.txt rename to basis/math/primes/erato/authors.txt diff --git a/extra/math/primes/erato/erato-docs.factor b/basis/math/primes/erato/erato-docs.factor similarity index 100% rename from extra/math/primes/erato/erato-docs.factor rename to basis/math/primes/erato/erato-docs.factor diff --git a/extra/math/primes/erato/erato-tests.factor b/basis/math/primes/erato/erato-tests.factor similarity index 100% rename from extra/math/primes/erato/erato-tests.factor rename to basis/math/primes/erato/erato-tests.factor diff --git a/extra/math/primes/erato/erato.factor b/basis/math/primes/erato/erato.factor similarity index 100% rename from extra/math/primes/erato/erato.factor rename to basis/math/primes/erato/erato.factor diff --git a/extra/math/primes/erato/summary.txt b/basis/math/primes/erato/summary.txt similarity index 100% rename from extra/math/primes/erato/summary.txt rename to basis/math/primes/erato/summary.txt diff --git a/extra/math/primes/factors/authors.txt b/basis/math/primes/factors/authors.txt similarity index 100% rename from extra/math/primes/factors/authors.txt rename to basis/math/primes/factors/authors.txt diff --git a/extra/math/primes/factors/factors-docs.factor b/basis/math/primes/factors/factors-docs.factor similarity index 100% rename from extra/math/primes/factors/factors-docs.factor rename to basis/math/primes/factors/factors-docs.factor diff --git a/extra/math/primes/factors/factors-tests.factor b/basis/math/primes/factors/factors-tests.factor similarity index 100% rename from extra/math/primes/factors/factors-tests.factor rename to basis/math/primes/factors/factors-tests.factor diff --git a/basis/math/primes/factors/factors.factor b/basis/math/primes/factors/factors.factor new file mode 100644 index 0000000000..05d6b26010 --- /dev/null +++ b/basis/math/primes/factors/factors.factor @@ -0,0 +1,29 @@ +! Copyright (C) 2007-2009 Samuel Tardieu. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays combinators kernel make math math.primes sequences ; +IN: math.primes.factors + + + +: group-factors ( n -- seq ) + [ 2 [ over 1 > ] [ write-factor next-prime ] [ ] while 2drop ] { } make ; + +: unique-factors ( n -- seq ) group-factors [ first ] map ; + +: factors ( n -- seq ) group-factors [ first2 swap ] map concat ; + +: totient ( n -- t ) + { + { [ dup 2 < ] [ drop 0 ] } + [ dup unique-factors [ 1 [ 1- * ] reduce ] [ product ] bi / * ] + } cond ; foldable diff --git a/extra/math/primes/factors/summary.txt b/basis/math/primes/factors/summary.txt similarity index 100% rename from extra/math/primes/factors/summary.txt rename to basis/math/primes/factors/summary.txt diff --git a/extra/math/primes/primes-docs.factor b/basis/math/primes/primes-docs.factor similarity index 67% rename from extra/math/primes/primes-docs.factor rename to basis/math/primes/primes-docs.factor index 516b081624..c7dbc950e8 100644 --- a/extra/math/primes/primes-docs.factor +++ b/basis/math/primes/primes-docs.factor @@ -11,15 +11,7 @@ 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" } "." } ; +{ primes-upto primes-between } related-words HELP: primes-upto { $values { "n" "an integer" } { "seq" "a sequence" } } diff --git a/basis/math/primes/primes-tests.factor b/basis/math/primes/primes-tests.factor new file mode 100644 index 0000000000..db738399ef --- /dev/null +++ b/basis/math/primes/primes-tests.factor @@ -0,0 +1,9 @@ +USING: arrays math.primes tools.test ; + +{ 1237 } [ 1234 next-prime ] unit-test +{ f t } [ 1234 prime? 1237 prime? ] unit-test +{ { 2 3 5 7 } } [ 10 primes-upto >array ] unit-test +{ { 999983 1000003 } } [ 999982 1000010 primes-between >array ] unit-test + +{ { 4999963 4999999 5000011 5000077 5000081 } } +[ 4999962 5000082 primes-between >array ] unit-test diff --git a/extra/math/primes/primes.factor b/basis/math/primes/primes.factor similarity index 69% rename from extra/math/primes/primes.factor rename to basis/math/primes/primes.factor index fa42d7385a..807ebf097b 100644 --- a/extra/math/primes/primes.factor +++ b/basis/math/primes/primes.factor @@ -1,7 +1,7 @@ -! Copyright (C) 2007 Samuel Tardieu. +! Copyright (C) 2007-2009 Samuel Tardieu. ! See http://factorcode.org/license.txt for BSD license. -USING: combinators kernel lists.lazy math math.functions -math.miller-rabin math.order math.primes.erato math.ranges sequences ; +USING: combinators kernel math math.functions math.miller-rabin +math.order math.primes.erato math.ranges sequences ; IN: math.primes : next-prime ( n -- p ) next-odd [ dup really-prime? ] [ 2 + ] [ ] until ; foldable -: lprimes ( -- list ) 2 [ next-prime ] lfrom-by ; - -: lprimes-from ( n -- list ) - dup 3 < [ drop lprimes ] [ 1- next-prime [ next-prime ] lfrom-by ] if ; - : primes-between ( low high -- seq ) [ dup 3 max dup even? [ 1 + ] when ] dip 2 [ prime? ] filter diff --git a/extra/math/primes/summary.txt b/basis/math/primes/summary.txt similarity index 100% rename from extra/math/primes/summary.txt rename to basis/math/primes/summary.txt diff --git a/extra/math/primes/factors/factors.factor b/extra/math/primes/factors/factors.factor deleted file mode 100644 index 282c46c82e..0000000000 --- a/extra/math/primes/factors/factors.factor +++ /dev/null @@ -1,40 +0,0 @@ -! Copyright (C) 2007-2009 Samuel Tardieu. -! See http://factorcode.org/license.txt for BSD license. -USING: arrays kernel lists make math math.primes sequences ; -IN: math.primes.factors - - [ - swap uncons swap [ pick call ] dip swap (factors) - ] [ 3drop ] if ; inline recursive - -: decompose ( n quot -- seq ) [ lprimes rot (factors) ] { } make ; inline - -PRIVATE> - -: factors ( n -- seq ) [ (factor) ] decompose ; flushable - -: group-factors ( n -- seq ) [ (count) ] decompose ; flushable - -: unique-factors ( n -- seq ) [ (unique) ] decompose ; flushable - -: totient ( n -- t ) - dup 2 < [ - drop 0 - ] [ - dup unique-factors [ 1 [ 1- * ] reduce ] [ product ] bi / * - ] if ; foldable diff --git a/extra/math/primes/lists/authors.txt b/extra/math/primes/lists/authors.txt new file mode 100644 index 0000000000..f3b0233f74 --- /dev/null +++ b/extra/math/primes/lists/authors.txt @@ -0,0 +1 @@ +Samuel Tardieu diff --git a/extra/math/primes/lists/lists-docs.factor b/extra/math/primes/lists/lists-docs.factor new file mode 100644 index 0000000000..ecab12ceb7 --- /dev/null +++ b/extra/math/primes/lists/lists-docs.factor @@ -0,0 +1,10 @@ +USING: help.markup help.syntax ; +IN: math.primes.lists + +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" } "." } ; diff --git a/extra/math/primes/lists/lists-tests.factor b/extra/math/primes/lists/lists-tests.factor new file mode 100644 index 0000000000..3bd7d70365 --- /dev/null +++ b/extra/math/primes/lists/lists-tests.factor @@ -0,0 +1,6 @@ +USING: lists.lazy math.primes.lists tools.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 diff --git a/extra/math/primes/lists/lists.factor b/extra/math/primes/lists/lists.factor new file mode 100644 index 0000000000..13f314f6ba --- /dev/null +++ b/extra/math/primes/lists/lists.factor @@ -0,0 +1,9 @@ +! Copyright (C) 2007-2009 Samuel Tardieu. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel lists.lazy math math.primes ; +IN: math.primes.lists + +: lprimes ( -- list ) 2 [ next-prime ] lfrom-by ; + +: lprimes-from ( n -- list ) + dup 3 < [ drop lprimes ] [ 1- next-prime [ next-prime ] lfrom-by ] if ; diff --git a/extra/math/primes/lists/summary.txt b/extra/math/primes/lists/summary.txt new file mode 100644 index 0000000000..39a780a26b --- /dev/null +++ b/extra/math/primes/lists/summary.txt @@ -0,0 +1 @@ +Infinite stream of prime numbers through lazy lists diff --git a/extra/math/primes/primes-tests.factor b/extra/math/primes/primes-tests.factor deleted file mode 100644 index b0b25285c0..0000000000 --- a/extra/math/primes/primes-tests.factor +++ /dev/null @@ -1,14 +0,0 @@ -USING: arrays math.primes tools.test lists.lazy ; - -{ 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 - -{ { 4999963 4999999 5000011 5000077 5000081 } } -[ 4999962 5000082 primes-between >array ] -unit-test diff --git a/extra/project-euler/007/007.factor b/extra/project-euler/007/007.factor index f2b659fe94..f40108e4d7 100644 --- a/extra/project-euler/007/007.factor +++ b/extra/project-euler/007/007.factor @@ -1,6 +1,6 @@ ! Copyright (c) 2007 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. -USING: lists math math.primes ; +USING: lists math math.primes.lists ; IN: project-euler.007 ! http://projecteuler.net/index.php?section=problems&id=7 diff --git a/extra/project-euler/134/134.factor b/extra/project-euler/134/134.factor index 7bdf17ef68..e00e86865d 100644 --- a/extra/project-euler/134/134.factor +++ b/extra/project-euler/134/134.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2007 Samuel Tardieu. ! See http://factorcode.org/license.txt for BSD license. USING: arrays kernel lists lists.lazy math.algebra math math.functions - math.order math.primes math.ranges project-euler.common sequences ; + math.order math.primes.lists math.ranges project-euler.common sequences ; IN: project-euler.134 ! http://projecteuler.net/index.php?section=problems&id=134