This Haskell program computes π (pi) to an arbitrary number of digits using the Chudnovsky algorithm, a highly efficient formula based on Ramanujan-type series. It leverages GMP (GNU Multiple Precision Arithmetic Library) for high-precision integer arithmetic via Haskell’s FFI interface.
mpz_sqrt
for precise square root evaluation.The Chudnovsky formula for π is:
This implementation:
bs
.(p, q, t)
for each interval.Compile and run using:
gcc -c cbits/wrappers.c -o cbits/wrappers.o
ghc -O2 pi.hs cbits/wrappers.o -o pi
./pi 1000
This will output π to 1000 digits.
π to 100 digits is: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170
Abhrankan Chakrabarti