Computing Pi
Archimedes (250 BC)
Estimates using the perimeters of regular polygons inscribed in and circumscribed about a circle of diameter 1. In the function below, each iteration doubles the number of polygon sides.
function polygon(n)
# start with a hexagon
a = 2 √3
b = 3
for i in 1:n
a = (2 a b) / (a + b)
b = √(a b)
end
return (a + b) / 2
end
, good to 4 significant digits for a 96-sided polygon
Viete (1597)
function viete(numTerms)
product = 1
radicand = 0
for i in 1:numTerms
radicand = √(2.0 + radicand)
product = product × (radicand / 2)
end
return 2 / product
end
, good to 6 significant digits after 10 terms.
Leibnitz (1673)
Well, that converges slowly. Only 2 significant digits after 25 terms.
Ramanujan (1914)
First, let’s calculate to high precision:
That’s 24 significant digits from 3 terms. Impressive.
Chudonovsky (1987)
42 significant digits from the first 3 terms. Each term adds at least 14 digits.
Two of these methods needed high-precision square roots. We got them from a function for Square Roots via Newton’s Method.
function sqRt(a; ε = 1e-15)
x = √a # ~15 significant digits
while true
if |x² - a| ≤ ε return x
x = (x + a / x) / 2
end
end