# How julia calculates pi?

**URL:** <https://discourse.julialang.org/t/how-julia-calculates-pi/87094>\
**Category:** Offtopic\
**Created:** [September 11, 2022, 7:09pm UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094 "2022-09-11T19:09:06Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [September 13, 2022, 11:16am UTC](https://discourse.julialang.org/t/how-julia-calculates-pi/87094/17 "2022-09-13T11:16:41Z")

</div>

Indeed! Adapting a [previous reference](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/68) to this concept on this forum:

```julia
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
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.)

---

_[View the full topic](https://discourse.julialang.org/t/how-julia-calculates-pi/87094)._
