Created
March 25, 2014 06:12
-
-
Save CarstenKoenig/9756074 to your computer and use it in GitHub Desktop.
Help for a G+er
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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