Skip to content

Instantly share code, notes, and snippets.

@CarstenKoenig
Created March 25, 2014 06:12
Show Gist options
  • Save CarstenKoenig/9756074 to your computer and use it in GitHub Desktop.
Save CarstenKoenig/9756074 to your computer and use it in GitHub Desktop.
Help for a G+er
{-# LANGUAGE BangPatterns #-}
piCalc :: Int -> Float
piCalc iterations = step * loop 0 iterations
where step = 1.0 / fromIntegral iterations
loop a i =
if i == 0 then a
else
let x = (fromIntegral i - 0.5) * step
!a' = a + (4.0 / (1.0 + x*x))
in loop a' (i-1)
main :: IO ()
main = do
putStrLn "please input the number of steps: "
x <- getLine
let iterations = read x
print $ piCalc iterations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment