bringert at cs.chalmers.se
Fri Aug 24 03:45:44 EDT 2007
On Aug 24, 2007, at 9:18 , Arie Groeneveld wrote:
> 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
> 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'.
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
More information about the Haskell-Cafe