From 4d5392fe56a8b099025afbf006365604ed2aa979 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Mon, 29 Jun 2009 16:55:44 +0200 Subject: [PATCH] Add divisors to math.primes.factors --- basis/math/primes/factors/factors-docs.factor | 6 +++++- basis/math/primes/factors/factors-tests.factor | 4 +++- basis/math/primes/factors/factors.factor | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/basis/math/primes/factors/factors-docs.factor b/basis/math/primes/factors/factors-docs.factor index f9fe4d5dcb..b22d1ba1a5 100644 --- a/basis/math/primes/factors/factors-docs.factor +++ b/basis/math/primes/factors/factors-docs.factor @@ -1,7 +1,7 @@ USING: help.markup help.syntax math sequences ; IN: math.primes.factors -{ factors group-factors unique-factors } related-words +{ divisors factors group-factors unique-factors } related-words HELP: factors { $values { "n" "a positive integer" } { "seq" sequence } } @@ -21,3 +21,7 @@ HELP: unique-factors HELP: totient { $values { "n" "a positive integer" } { "t" integer } } { $description { "Return the number of integers between 1 and " { $snippet "n-1" } " that are relatively prime to " { $snippet "n" } "." } } ; + +HELP: divisors +{ $values { "n" "a positive integer" } { "seq" sequence } } +{ $description { "Return the ordered list of divisors of " { $snippet "n" } ", including 1 and " { $snippet "n" } "." } } ; diff --git a/basis/math/primes/factors/factors-tests.factor b/basis/math/primes/factors/factors-tests.factor index 8e2e10711a..eea59b6f9b 100644 --- a/basis/math/primes/factors/factors-tests.factor +++ b/basis/math/primes/factors/factors-tests.factor @@ -1,4 +1,4 @@ -USING: math.primes.factors tools.test ; +USING: math.primes.factors sequences tools.test ; { { 999983 999983 1000003 } } [ 999969000187000867 factors ] unit-test { { } } [ -5 factors ] unit-test @@ -8,3 +8,5 @@ USING: math.primes.factors tools.test ; { 0 } [ 1 totient ] unit-test { { 425612003 } } [ 425612003 factors ] unit-test { { 13 4253 15823 32472893749823741 } } [ 28408516453955558205925627 factors ] unit-test +{ { 1 2 3 4 6 8 12 24 } } [ 24 divisors ] unit-test +{ 24 } [ 360 divisors length ] unit-test diff --git a/basis/math/primes/factors/factors.factor b/basis/math/primes/factors/factors.factor index f5fa468687..439d55ee8d 100644 --- a/basis/math/primes/factors/factors.factor +++ b/basis/math/primes/factors/factors.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2007-2009 Samuel Tardieu. ! See http://factorcode.org/license.txt for BSD license. USING: arrays combinators kernel make math math.functions -math.primes sequences ; +math.primes math.ranges sequences sequences.product sorting ; IN: math.primes.factors { [ dup 2 < ] [ drop 0 ] } [ dup unique-factors [ 1 [ 1- * ] reduce ] [ product ] bi / * ] } cond ; foldable + +: divisors ( n -- seq ) + group-factors [ first2 [0,b] [ ^ ] with map ] map + [ product ] product-map natural-sort ;