[Shootout-list] harmonic sum for ghc and perl
Einar Karttunen
ekarttun@cs.helsinki.fi
Thu, 03 Mar 2005 10:55:54 +0200
Greg Buchholz <sleepingsquirrel@yahoo.com> writes:
> Attached are implementations of the harmonic sum benchmark for GHC
> and Perl. The benchmark description should probably mention that the
> results should be rounded to 9 decimal places.
Here is a faster one for GHC - gives a 5x increase.
Compile with ghc -O2 -fexcess-precision a.hs -o a
import System(getArgs)
import Numeric
main = do [n] <- getArgs
putStrLn $ showFFloat (Just 9) (loop (read n) 0 0) ""
loop :: Int -> Double -> Double -> Double
loop 0 _ sum = sum
loop i d sum = loop (i-1) d' (sum + (1/d')) where d' = d+1
- Einar Karttunen