
26 Aug
2007
26 Aug
'07
8:22 a.m.
2007/8/26, Yitzchak Gale
Bjorn Bringert wrote:
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]
Be careful with types - use Data.List.genericLength here instead of length. Otherwise, tm_silly n is wrong for n >= 13 (on my 32-bit machine) due to round-off error in the Int type.
Are you sure you really tested tm_silly ? length is perfectly enough to count the 0 in n! since the number of zeros don't go over the Int limit before n = 8_589_934_615 (though this solution will stack overflow due to the product much sooned than that). -- Jedaï