Q2.5.2The arithmetic-geometric mean
Write a while loop to calculate the arithmetic-geometric mean (AGM) of two positive real numbers, \(x\) and \(y\), defined as the limit of the sequences:
\[\begin{align*}
a_{n+1} &= \textstyle \frac{1}{2}(a_n + b_n)\\
b_{n+1} &= \sqrt{a_n b_n},
\end{align*}\]
starting with \(a_0 = x\), \(b_0 = y\). Both sequences converge to the same number, denoted \(\mathrm{agm}(x,y)\). Use your loop to determine Gauss's constant, \(G = 1/\mathrm{agm}(1,\sqrt{2})\).
Show solution
The following code calculates Gauss's constant to 14 decimal places.
>>> import math
>>> tol = 1.e-14
>>> an, bn = 1., math.sqrt(2)
>>> while abs(an - bn) > tol:
... an, bn = (an + bn) / 2, math.sqrt(an * bn)
...
>>> print('G = {:.14f}'.format(1/an))
G = 0.83462684167407