[Haskell-cafe] Re: MonadFix

apfelmus apfelmus at quantentunnel.de
Thu Dec 20 05:07:25 EST 2007

Joost Behrends wrote:
> since about three weeks i am learning Haskell now. One of my first exercises is
> to decompose an Integer into its primefactors.

How about separating the candidate prime numbers from the recursion

   factorize :: Integer -> [Integer]
   factorize = f primes'
      primes' = 2:[3,5..]
      f (p:ps) n
         | r == 0    = p : f (p:ps) q
         | p*p > n   = [n]
         | otherwise = f ps n
         (q,r) = n `divMod` p

For a faster factorization, just plug in a better  primes' .


More information about the Haskell-Cafe mailing list