I want to find all prime factors of a given number using only list comprehension method and/or .
(function composition operator) in Haskell. I specifically want to avoid a recursive solution.
For example, pfactors 120
must produce [2,2,2,3,5]
output.
I tried:
pfactors n = [p | p <- [2..n], n `mod` p == 0, [d | d <- [1..p], p `mod` d == 0] == [1,p]]
But when I call pfactors 120
, the result is [2,3,5]
, not all prime factors.
mod
p == 0, [d | d <- [1..p], pmod
d == 0] == [1,p]] – Tensionn
, but as your example clearly shows, some factors occur more than once, and even if you did enumerate values several times, you wouldn't know how many times you have to do it to get all the multiple factors.. – Physician