Created
April 7, 2019 16:33
-
-
Save rockydd/4c166f54399707074bd658700710efb5 to your computer and use it in GitHub Desktop.
Calculate and print pi in haskell
This file contains hidden or 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
import Control.Monad | |
import System.IO | |
pi_ = g(1,0,1,1,3,3) where | |
g (q,r,t,k,n,l) = | |
if 4*q+r-t < n*t | |
then n : g (10*q, 10*(r-n*t), t, k, div (10*(3*q+r)) t - 10*n, l) | |
else g (q*k, (2*q+r)*l, t*l, k+1, div (q*(7*k+2)+r*l) (t*l), l+2) | |
digs = insertPoint digs' | |
where insertPoint (x:xs) = x:'.':xs | |
digs' = map (head . show) pi_ | |
main = do | |
hSetBuffering stdout $ BlockBuffering $ Just 80 | |
forM_ digs putChar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment