[Haskell-cafe] factorising to prime numbers

kahl at cas.mcmaster.ca kahl at cas.mcmaster.ca
Fri Feb 9 09:42:08 EST 2007


 > 
 > If you look at the definition of 'powers' you'll note it's infinite. So
 > there's no easy way to take the product of this list, if I don't know
 > how many items to take from it.

So you need finite lists.

 > 
 > -- how many of the prime p are in the unique factorisation
 > -- of the integer n?
 > f 0 _ = 0
 > f n p | n `mod` p == 0 = 1 + f (n `div` p) p
 >       | otherwise = 0
 > 
 > powers n = map (f n) primes

Extremely high-level hint:
Move from div to divMod (i.e., let f return a pair),
and add List.unfoldr (or takeWhile) capacities to map.

;-)



Wolfram


More information about the Haskell-Cafe mailing list