purescript-contrib/purescript-book

Ch4 Solution suggestion: factorize

tokdaniel opened this issue · 3 comments

factorize :: Int -> Array Int
factorize n = do
   p <- filter isPrime (1 .. (n `quot` 2))
   guard $ n `mod` p == 0
   [p]

Square root of n would be even better, that would require dealing with float to int conversion, so this is easier.

to pass the tests you need

factorize :: Int -> Array Int
factorize n = do
   p <- reverse $ filter isPrime (1 .. (n `quot` 2))
   guard $ n `mod` p == 0
   [p]

But its kind of unreasonable to test for order here.

This looks fine to me, except I think factorize 4 should be [2, 2], given that the exercise asks for "the array of integers whose product is n". It probably is worth amending the tests so that the order doesn't matter as well.

Ah never mind, we were actually planning to revert the change which introduced this exercise already anyway, see #208.