The following function counts how often I can divide one number by another:
divs n p = if (n `mod` p == 0) then 1 + divs (n `div` p) p else 0
Is there a shorter way to write divs?
My variant would be:
factors :: Integral i => i -> i -> Int
factors f =
length . takeWhile (== 0) .
map (`mod` f) . iterate (`div` f)
The iterate/map pattern is very useful for many problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With