1 ! Copyright (C) 2007-2009 Samuel Tardieu.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs combinators command-line io kernel make
4 math math.functions math.parser math.primes math.ranges
5 namespaces sequences sequences.product sorting ;
6 IN: math.primes.factors
10 : count-factor ( n d -- n' c )
11 [ 1 ] 2dip [ /i ] keep
12 [ dupd /mod zero? ] curry [ nip [ 1 + ] dip ] while drop
15 : write-factor ( n d -- n' d' )
17 [ [ count-factor ] keep swap 2array , ] keep
18 ! If the remainder is a prime number, increase d so that
19 ! the caller stops looking for factors.
20 over prime? [ drop dup ] when
23 : (group-factors) ( n -- seq )
26 [ 2dup sq < ] [ write-factor next-prime ] until
27 drop dup 2 < [ drop ] [ 1 2array , ] if
32 : group-factors ( n -- seq )
33 dup prime? [ 1 2array 1array ] [ (group-factors) ] if ; flushable
35 : unique-factors ( n -- seq ) group-factors keys ; flushable
37 : factors ( n -- seq )
38 group-factors [ first2 swap <array> ] map concat ; flushable
42 { [ dup 2 < ] [ drop 0 ] }
43 [ dup unique-factors [ 1 [ 1 - * ] reduce ] [ product ] bi / * ]
46 : divisors ( n -- seq )
50 group-factors dup empty? [
51 [ first2 [0,b] [ ^ ] with map ] map
52 [ product ] product-map natural-sort
56 : unix-factor ( string -- )
59 [ factors [ number>string ] map " " join print ] bi*
61 "factor: `" "' is not a valid positive integer" surround print
64 : run-unix-factor ( -- )
66 [ readln [ unix-factor t ] [ f ] if* ] loop