1 ! Copyright (C) 2007-2009 Samuel Tardieu.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators command-line io kernel math
4 math.functions math.parser math.primes.pollard-rho-brent
5 ranges math.statistics namespaces sequences
6 sequences.product sets sorting splitting ;
7 IN: math.primes.factors
9 : factors ( n -- seq ) pollard-rho-brent-factors ; flushable
11 : group-factors ( n -- seq ) factors histogram sort-keys ; flushable
13 : unique-factors ( n -- seq ) factors members ; flushable
17 { [ dup 2 < ] [ drop 0 ] }
18 [ dup unique-factors [ 1 [ 1 - * ] reduce ] [ product ] bi / * ]
21 : divisors ( n -- seq )
25 group-factors dup empty? [
26 [ first2 [0..b] [ ^ ] with map ] map
27 [ product ] product-map natural-sort
31 : unix-factor ( string -- )
34 [ factors [ number>string ] map join-words print ] bi*
36 "factor: `" "' is not a valid positive integer" surround print
39 : run-unix-factor ( -- )
41 [ readln [ unix-factor t ] [ f ] if* ] loop