import Data.List import Data.Maybe -- Computes digits of pi using the formula -- pi = 4/1 - 4/3 + 4/5 - 4/7 + ... -- In each iteration, -- a = the numerator of the fraction that estimates the next group of digits -- b = the denominator of the fraction that estimates the next group of digits -- c = the numerator of th next term in the sum -- d = the place of the digits being computed -- e = the estimate of the digits being computed -- If we get the same estimate twice in a row, then we have -- found the correct digits and we can move on to the next set s = map (\(_,_,_,_,x) -> x) $ iterate r (0,1,1,1,Nothing) where r (a,b,c,d,e) = let g = a * d + 4 * b * c h = b * d i = g `div` h in if Just i == e then (10000000000 * (g `mod` h),h,(-10000000000)*c,d+2,Nothing) else (g,h,-c,d+2,Just i) -- 3, 1415926535, 8979323846, ... d = [ fromJust $ genericIndex s i | i <- [0..], isNothing $ genericIndex s (i+1)] -- prints 8979323846 main = print (d!!2)