How julia calculates pi?

Indeed! Adapting a previous reference to this concept on this forum:

F(x) = [(x[1]+x[2])/2, √(x[1]x[2]), (x[1]-x[2])/2, x[1]^2 - x[2]^2]

Fⁿ(x,n) = ∘(fill(F,n)...)(x)

Q(n) = [Fⁿ([1,1/√BigFloat(2)],i) for i=1:n]

Pi(n) = (P = Q(n); 4*(P[n][1])^2/(1 - sum(2^(j)*P[j][4] for j=2:n)))

This function has impressive convergence:

julia> Pi(2) - π
-0.001013403067624990151312114303679572423729161862157485331457898962233648251246769

julia> Pi(3) - π
-7.376250956313298951296807109882732176029503026415405107486381639065704112971525e-09

julia> Pi(4) - π
-1.831306084776389098159438945396588851876138902814342684477531468625531008649221e-19

julia> Pi(5) - π
-5.472109145689941832748533178964178489138195886835923610924029609286031340771535e-41

julia> Pi(6) - π
3.109020679834000065139086670608143845601760131197061298608533249260693049138474e-76

julia> Pi(7) - π
3.109020679834000065139086670608143845601760131197061298608533249260693049138474e-76

(It exhausts the BigFloat precision after 6 iterations.)

4 Likes