! 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 math.ranges sequences sequences.product sorting ; IN: math.primes.factors : group-factors ( n -- seq ) dup prime? [ 1 2array 1array ] [ (group-factors) ] if ; flushable : unique-factors ( n -- seq ) group-factors [ first ] map ; flushable : factors ( n -- seq ) group-factors [ first2 swap ] map concat ; flushable : totient ( n -- t ) { { [ 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 ;