99 questions/Solutions/37

From HaskellWiki
< 99 questions‎ | Solutions
Revision as of 19:46, 18 January 2014 by Henk-Jan van Tuyl (talk | contribs) (Added Category:Programming exercise spoilers)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

(**) Calculate Euler's totient function phi(m) (improved).

Given prime_factors_mult from problem 36, we get

totient m = product [(p - 1) * p ^ (c - 1) | (p, c) <- prime_factors_mult m]

This just uses a list comprehension to build each term of the product in the formula for phi, then multiplies them all.