HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Shootout/Takfp

< Shootout

Categories: Code

A ShootoutEntry for takfp

1 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 = putStrLn . show . (\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)

2 Current entry

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

import System
 
main = putStrLn . show . (\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)

3 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
          putStrLn $ show $ 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)

Retrieved from "http://haskell.org/haskellwiki/Shootout/Takfp"

This page has been accessed 795 times. This page was last modified 02:25, 8 October 2006. Recent content is available under a simple permissive license.