diff --git a/extra/math/primes/factors/authors.txt b/extra/math/primes/factors/authors.txt new file mode 100644 index 0000000000..f3b0233f74 --- /dev/null +++ b/extra/math/primes/factors/authors.txt @@ -0,0 +1 @@ +Samuel Tardieu diff --git a/extra/math/primes/factors/factors-docs.factor b/extra/math/primes/factors/factors-docs.factor new file mode 100644 index 0000000000..2238420d32 --- /dev/null +++ b/extra/math/primes/factors/factors-docs.factor @@ -0,0 +1,20 @@ +USING: help.markup help.syntax ; +IN: math.primes.factors + +{ factors count-factors unique-factors } related-words + +HELP: factors +{ $values { "n" "a positive integer" } { "seq" "a sequence" } } +{ $description { "Factorize an integer and return an ordered list of factors, possibly repeated." } } ; + +HELP: count-factors +{ $values { "n" "a positive integer" } { "seq" "a sequence" } } +{ $description { "Return a sequence of pairs representing each factor in the number and its corresponding power." } } ; + +HELP: unique-factors +{ $values { "n" "a positive integer" } { "seq" "a sequence" } } +{ $description { "Return an ordered list of unique prime factors." } } ; + +HELP: totient +{ $values { "n" "a positive integer" } { "t" "an integer" } } +{ $description { "Return the number of integers between 1 and " { $snippet "n-1" } " relatively prime to " { $snippet "n" } "." } } ; diff --git a/extra/math/primes/factors/factors-tests.factor b/extra/math/primes/factors/factors-tests.factor new file mode 100644 index 0000000000..71bdd56a81 --- /dev/null +++ b/extra/math/primes/factors/factors-tests.factor @@ -0,0 +1,6 @@ +USING: math.primes.factors tools.test ; + +{ { 999983 999983 1000003 } } [ 999969000187000867 factors ] unit-test +{ { { 999983 2 } { 1000003 1 } } } [ 999969000187000867 count-factors ] unit-test +{ { 999983 1000003 } } [ 999969000187000867 unique-factors ] unit-test +{ 999967000236000612 } [ 999969000187000867 totient ] unit-test diff --git a/extra/math/primes/factors/factors.factor b/extra/math/primes/factors/factors.factor new file mode 100644 index 0000000000..b841d49f7d --- /dev/null +++ b/extra/math/primes/factors/factors.factor @@ -0,0 +1,41 @@ +! Copyright (C) 2007 Samuel Tardieu. +! See http://factorcode.org/license.txt for BSD license. +USING: arrays kernel lazy-lists math math.primes namespaces sequences ; +IN: math.primes.factors + + [ swap uncons >r pick call r> swap (factors) ] [ 3drop ] if ; + +: (decompose) ( n quot -- seq ) + [ lprimes rot (factors) ] { } make ; + +PRIVATE> + +: factors ( n -- seq ) + [ (factor) ] (decompose) ; foldable + +: count-factors ( n -- seq ) + [ (count) ] (decompose) ; foldable + +: unique-factors ( n -- seq ) + [ (unique) ] (decompose) ; foldable + +: totient ( n -- t ) + dup 2 < [ + drop 0 + ] [ + [ unique-factors dup 1 [ 1- * ] reduce swap product / ] keep * + ] if ; foldable diff --git a/extra/math/primes/factors/summary.txt b/extra/math/primes/factors/summary.txt new file mode 100644 index 0000000000..1440dddc7f --- /dev/null +++ b/extra/math/primes/factors/summary.txt @@ -0,0 +1 @@ +Prime factors decomposition