Shootout/Takfp

From HaskellWiki
Jump to navigation Jump to search

A ShootoutEntry for takfp

Shorter entry

Shortest entry in any language. Faster than old entry (by cranking up gcc)

{-# OPTIONS -O2 -optc-O3 #-}
-- http://shootout.alioth.debian.org/
--
-- GHC version of floating point Tak function
-- compile with ghc -O2 -fexcess-precision -o takfp takfp.hs
-- contributed by Greg Buchholz, optimized by Einar Karttunen and Don Stewart

import System

main = print . (\n -> tak (3*n) (2*n) n) . read . head =<< getArgs

tak x y z = if y>=x then z::Float else tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y)

Current entry

Refactor musasabi's code. Still uses too much space.

import System

main = print . (\n -> tak (3*n) (2*n) n) . read . head =<< getArgs

tak x y z | y >= x    = z :: Float
          | otherwise = tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y)

Old entry

-- http://shootout.alioth.debian.org/
--
-- GHC version of floating point Tak function
-- compile with ghc -O2 -fexcess-precision -o takfp takfp.hs
-- contributed by Greg Buchholz, optimized by Einar Karttunen

import System(getArgs)

main = do n <- getArgs >>= readIO.head
          print $ tak (3*n) (2*n) n

tak :: Float -> Float -> Float -> Float
tak x y z | y>=x      = z
          | otherwise = tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y)