
On Aug 24, 2007, at 9:18 , Arie Groeneveld wrote:
Hi,
I defined several functions for calculating the number of trailing zero's of n!
tm = sum . takeWhile(>0) . iterate f . f where f = flip div 5
tm1 n = sum . takeWhile(>0) . map (div n . (5^)) $ [1..] tm2 n = sum . takeWhile(>0) . map (div n) $ iterate ((*)5) 5 tm3 = sum . takeWhile(>0) . flip map (iterate ((*)5) 5) . div
Questions:
Which one is the most elegant one generally speaking? Which one is most natural in Haskell? Is there more 'beauty' to possible?
My personal choice is 'tm'. I like 'tm3' (a revised version of tm2) in terms of pointlessness and not having a 'where', but I think it's a bit contrived because of the 'flip'.
Comments?
Here's a much more inefficient version, but it has the merit of being very easy to understand: tm_silly n = length $ takeWhile (=='0') $ reverse $ show $ product [1..n] /Björn