How to resolve the algorithm Arithmetic-geometric mean step by step in the LiveCode programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic-geometric mean step by step in the LiveCode programming language

Table of Contents

Problem Statement

Write a function to compute the arithmetic-geometric mean of two numbers.

The arithmetic-geometric mean of two numbers can be (usefully) denoted as

a g m

( a , g )

{\displaystyle \mathrm {agm} (a,g)}

, and is equal to the limit of the sequence: Since the limit of

a

n

g

n

{\displaystyle a_{n}-g_{n}}

tends (rapidly) to zero with iterations, this is an efficient method. Demonstrate the function by calculating:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic-geometric mean step by step in the LiveCode programming language

Source code in the livecode programming language

function agm aa,g
    put abs(aa-g) into absdiff 
    put (aa+g)/2 into aan
    put sqrt(aa*g) into gn
    repeat while abs(aan - gn) < absdiff
        put abs(aa-g) into absdiff 
        put (aa+g)/2 into aan
        put sqrt(aa*g) into gn
        put aan into aa
        put gn into g
    end repeat
    return aa
end agm

put agm(1, 1/sqrt(2))
-- ouput
-- 0.847213

  

You may also check:How to resolve the algorithm Bitwise operations step by step in the Ecstasy programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Maxima programming language
You may also check:How to resolve the algorithm Palindromic gapful numbers step by step in the Phix programming language
You may also check:How to resolve the algorithm Range expansion step by step in the Go programming language
You may also check:How to resolve the algorithm Sort three variables step by step in the SenseTalk programming language