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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic-geometric mean step by step in the Lambdatalk 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 Lambdatalk programming language

Source code in the lambdatalk programming language

{def eps 1e-15}
-> eps

{def agm
  {lambda {:a :g}
   {if {> {abs {- :a :g}} {eps}}
    then {agm {/ {+ :a :g} 2} 
              {sqrt {* :a :g}}}
    else :a }}} 
-> agm

{agm 1 {/ 1 {sqrt 2}}}
-> 0.8472130847939792

Multi-precision version using the lib_BN library

{BN.DEC 70} 
->   70 digits
{def EPS {BN./ 1 {BN.pow 10 45}}}
-> EPS 
 
{def AGM
  {lambda {:a :g}
   {if {= {BN.compare {BN.abs {BN.- :a :g}} {EPS}} 1}
    then {AGM {BN./ {BN.+ :a :g} 2}
              {BN.sqrt {BN.* :a :g}}}
    else :a }}}  
-> AGM

{AGM 1 {BN./ 1 {BN.sqrt 2}}}
-> 0.8472130847939790866064991234821916364814459103269421850605793726597339


  

You may also check:How to resolve the algorithm Babbage problem step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Digital root step by step in the Huginn programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Oforth programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Liberty BASIC programming language