! 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 ; 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