ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to Compute the Arithmetic-Geometric Mean with JavaScript

Updated on October 13, 2016
calculus-geometry profile image

TR Smith is a product designer and former teacher who uses math in her work every day.

What Is the Arithmetic-Geometric Mean?

The arithmetic-geometric mean (abbreviated AGM) is a hybrid between the arithmetic mean and geometric mean. To compute the AGM of two numbers, A and B, you first find their arithmetic and geometric means, thereby generating a new pair of numbers, (A+B)/2 and sqrt(AB). Then you find the arithmetic and geometric means of those new numbers to generate another pair, and so on. As the number of iterations goes to infinity, the numbers you generate converge. The convergent value is defined as the AGM.

Finding the AGM of two numbers to a specified degree of accuracy by hand is cumbersome, even with a calculator, but can be easily carried out with a simple computer program written in a common programming language such as JavaScript, a web programming language. For those who have never programmed before, JavaScript is ideal because the programs can be run in any web browser (Chrome, Firefox, Safari, IE, etc.). An internet connection is not even necessary.

If you already know something about writing a loop program and don't want the long explanation, you can skip directly to the code. Otherwise, if you're a novice you can read the full tutorial.

What Is a Loop in Programming?

A loop is a programming term for an algorithm that repeats itself until a condition for stopping is met. An everyday task like washing dishes can be thought of as a loop; you take a dish from the sink and wash it (the operation), repeating the process until there are no more dishes in the sink (the stopping condition). Some mathematical functions are computed algorithmically as loops, for example, the arithmetic-geometric mean (AGM) of two numbers.

Writing a Loop in JS to Compute the AGM of 2 Numbers

To begin writing the JS program, you first specify the two numbers whose AGM you want to compute. For instance, let's find the arithmetic geometric mean of 32 and 98. Let's call these two numbers A and B respectively,

var A = 32;
var B = 98;

Next, we initialize another pair of variables which will be run through the loop. It doesn't matter what their initial values are, so long as they are differ by more than 0.000001, which will be explained in a moment. Let's call these two dummy variables X and Y,

var X = 0;
var Y = 1;

The AGM algorithm can run through infinitely many iterations, however, we want our loop to stop at some point. Since the generated pairs converge, we should specify the stopping condition to occur when the difference between the numbers is very small, say less than or equal to 0.000001. In other words, the loop runs while the difference is greater than 0.000001. In the language of JavaScript, this condition is

while ( Math.abs(X-Y)>0.000001 ) { }

Inside the braces { } we put the mathematical looping algorithm that will run over and over until the difference between X and Y becomes smaller and smaller, less than or equal to 0.000001. This is

while ( Math.abs(X-Y)>0.000001 ) {
X = (A+B)/2;
Y = Math.sqrt(A*B);
A = (X+Y)/2;
B = Math.sqrt(X*Y);

What this means in plain English is that while the absolute value of the difference between X and Y is greater than 0.000001, the following computations will be made: First, X will be set equal to the arithmetic mean of A and B, and Y will be set equal to the geometric mean of A and B. This replaces their initial values of 0 and 1. Then, the value of A will be replaced by the arithmetic mean of X and Y, and the value of B will be replaced by the geometric mean of X and Y. This replaces their initial values of 32 and 98.

This process variable of reassignment repeats over and over until the generated values of X and Y become closer and closer together, i.e., until their difference is 0.000001 or less.

Neither the final value of X nor final value of Y will be the AGM of 32 and 98, but either one will be a very close approximation. In the display of the final answer, you can output X, Y, their arithmetic average, or their geometric average (or any other mathematical average). Let's assign the new variable F to be the final output, defined as the arithmetic mean of the final values of X and Y rounded to five places.

var F = Math.round(100000*(X+Y)/2)/100000;

You can use a command such as document.write to display the final answer. For example, the final the arithmetic-geometric mean can be displayed with the command


This whole routine is an example of a "while" loop in JavaScript. Another way to write repeating routines is with "for" loops. If you compile the example program above, you will find the arithmetic-geometric mean of 32 and 98 is about 60.41618 rounded to five decimal places. Next we will show how to write up this loop so that it can be compiled on your browser.

How to Compile and Execute a JavaScript Loop Program

Since JavaScript is a client-sided web programming language (as opposed to a server-side language like PHP or ASPX) all you need to run a JS program is a web browser. To compile and run this JS loop program for computing the AGM of two numbers, open up Notepad or any other plain text editor (not word processing program such as Word or Open Office) and paste the following code.



var A = 32;
var B = 98;
var X = 0;
var Y = 1;

while ( Math.abs(X-Y)>0.000001 ) {
X = (A+B)/2;
Y = Math.sqrt(A*B);
A = (X+Y)/2;
B = Math.sqrt(X*Y);

var F = Math.round(100000*(X+Y)/2)/100000;




Next, save the document as an html file. You may need to use quotes around the file name. You can name it "agm-calculator.html" or any other name, just be sure to include the .html part, or else the file will be saved as plain text and it will not run as a program.

Now save the program to your computer. Right click on the file and select "open with" and choose your favorite web browser. A new tab or window will open with the arithmetic-and geometric mean in the top left corner.

If you've done everything correctly, you should see a blank webpage with nothing but the number 60.41618 in the upper left corner. This is the AGM of 32 and 98.

If you want to compute the AGM of another set of numbers, open the file in Notepad and change the values of A and B to other numbers besides 32 and 98. For example, if you set A = 1 and B = 3 and run the program again, you will obtain 1.86362 as the AGM of 1 and 3.

Mathematical Properties of the Arithmetic-Geometric Mean

The AGM of two numbers is a good way to illustrate loop programs because there is no closed form formula for the AGM; computing it algorithmically is actually the only way to calculate it! The AGM is one type (the most well-studied type) of iterated mean, a mean that is generated by taking two different averages of two numbers and iterating this process over and over. (Curiously, if you iterated the arithmetic and harmonic means together, i.e., compute the arithmetic-harmonic mean, you would end up with the geometric mean.)

The AGM has several interesting mathematical properties. If we let the function AGM(m, n) stand for the arithmetic-geometric mean of two numbers m and n, then the following are true:

  • sqrt(mn) ≤ AGM(m, n) ≤ (m+n)/2. Equality occurs when m = n.
  • AGM(m, m) = m. In other words, the AGM of a number and itself returns the number.
  • AGM(m, n) = AGM(n, m). In other words, it doesn't matter the order since the function is symmetric.
  • AGM(km, kn) = k*AGM(m, n). In other words, if you multiply m and n by the same factor k, the AGM of km and kn is equal to the AGM of m and n multiplied by k.
  • Following the identity above, n*AGM(m/n, 1) = AGM(m, n)
  • AGM(0, n) = 0

A deeper mathematical property of the AGM is its relation to the complete elliptic integral of the first kind, a transcendental function defined as an integral with no closed-form antiderivative. If K(c) is the complete elliptic integral function of the first kind with modulus c, then

AGM(m, n) = [ π(m+n) ] / [ 4*K((m-n)/(m+n)) ]

With this relation, the AGM algorithm can be reworked to compute K(c) for any value of c between 0 and 1. Using m = 1, n = r, c = (1-r)/(1+r) and r = (1-c)/(1+c) gives you

AGM(1, r) = [ π(1 + r) ] / [ 4*K((1-r)/(1+r)) ]
[ 2π/(1+c) ] / [ 4*AGM(1, (1-c)/(1+c) ) ] = K(c)
K(c) = π / [ 2*AGM(1+c, 1-c) ]

The arithmetic-geometric mean can also be defined by the pair of recursive equations

An+1 = (An + Bn)/2
Bn+1 = sqrt(An*Bn)

where A0 and B0 are the two numbers whose AGM you wish to calculate. This pair of simultaneous equations can be simplified into a single non-linear second order recursive equation

An+2 = [ An + sqrt( 2An+1An - An2 ) ] / 2


    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article