# 99 questions/Solutions/36

### From HaskellWiki

< 99 questions | Solutions(Difference between revisions)

(One intermediate revision by one user not shown) | |||

Line 9: | Line 9: | ||

using <tt>primeFactors</tt> from problem 35 to generate the list of prime factors in ascending order, and <tt>encode</tt> from problem 10 to compress this list to a list of numbers paired with their multiplicity. |
using <tt>primeFactors</tt> from problem 35 to generate the list of prime factors in ascending order, and <tt>encode</tt> from problem 10 to compress this list to a list of numbers paired with their multiplicity. |
||

+ | |||

+ | |||

+ | Without relying on <tt>encode</tt> from problem 10, but using <tt>group</tt> from <tt>Data.List</tt>: |
||

+ | |||

+ | <haskell> |
||

+ | prime_factors_mult = map encode . group . primeFactors |
||

+ | where encode xs = (head xs, length xs) |
||

+ | </haskell> |
||

+ | |||

+ | |||

+ | [[Category:Programming exercise spoilers]] |

## Latest revision as of 19:45, 18 January 2014

(**) Determine the prime factors of a given positive integer.

Construct a list containing the prime factors and their multiplicity.

prime_factors_mult n = map swap $ encode $ primeFactors n where swap (x,y) = (y,x)

using `primeFactors` from problem 35 to generate the list of prime factors in ascending order, and `encode` from problem 10 to compress this list to a list of numbers paired with their multiplicity.

Without relying on `encode` from problem 10, but using `group` from `Data.List`:

prime_factors_mult = map encode . group . primeFactors where encode xs = (head xs, length xs)