]> gitweb.factorcode.org Git - factor.git/commitdiff
Add divisors to math.primes.factors
authorSamuel Tardieu <sam@rfc1149.net>
Mon, 29 Jun 2009 14:55:44 +0000 (16:55 +0200)
committerSamuel Tardieu <sam@rfc1149.net>
Mon, 29 Jun 2009 14:56:00 +0000 (16:56 +0200)
basis/math/primes/factors/factors-docs.factor
basis/math/primes/factors/factors-tests.factor
basis/math/primes/factors/factors.factor

index f9fe4d5dcbacee61a8f3e0903a3719ade14fb168..b22d1ba1a511964c832aa518920e1733c62da473 100644 (file)
@@ -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" } "." } } ;
index 8e2e10711a3766e80034f9e895b2c061b12acab8..eea59b6f9b53009326bb3211d410e2429880ca0c 100644 (file)
@@ -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
index f5fa468687f1f38eb5d5a98906bd1fee8adca2e4..439d55ee8d405a2e947eff19c3067d8fd151aa66 100644 (file)
@@ -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
 
 <PRIVATE
@@ -41,3 +41,7 @@ PRIVATE>
         { [ 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 ;