jukaukor

ChudnovskyPi_julia.jl

Jun 13th, 2022 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. # Chudnovsky algorithm for Pi
  2. # https://en.wikipedia.org/wiki/Chudnovsky_algorithm
  3. # Juhani Kaukoranta 14.06.2022
  4. function Chudnovsky(digits)
  5. # digits : how many decimals of Pi to be calucate
  6. maxK = trunc(Int,digits*0.071+2) # max numbers of iterations
  7. prec = round(BigInt,log2(BigFloat(10.0)^(digits+8))) # bit precision
  8. setprecision(prec) # bit precision,not number precision
  9. K, M, L, X, S = big(6), big(1), big(13591409), big(1), big(13591409)
  10. for k = 1 : maxK
  11. M = div((big(K)^3 - 16*big(K))*M, k^3)
  12. L += big(545140134)
  13. X *= big(-262537412640768000)
  14. S += big(M)*big(L)/big(X)
  15. K += 12
  16. end
  17. pii = big(426880)*sqrt(big(10005)) / big(S)
  18. pi = string(pii)
  19. return(pi[1:digits+2])
  20. end
  21.  
Add Comment
Please, Sign In to add comment