How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the Wren programming language

Table of Contents

Problem Statement

Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate

π

{\displaystyle \pi }

. With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing:

π

4

a g m

( 1 , 1

/

2

)

2

1 −

n

1

2

n + 1

(

a

n

2

g

n

2

)

{\displaystyle \pi ={\frac {4;\mathrm {agm} (1,1/{\sqrt {2}})^{2}}{1-\sum \limits {n=1}^{\infty }2^{n+1}(a{n}^{2}-g_{n}^{2})}}}

This allows you to make the approximation, for any large   N:

π ≈

4

a

N

2

1 −

k

1

N

2

k + 1

(

a

k

2

g

k

2

)

{\displaystyle \pi \approx {\frac {4;a_{N}^{2}}{1-\sum \limits {k=1}^{N}2^{k+1}(a{k}^{2}-g_{k}^{2})}}}

The purpose of this task is to demonstrate how to use this approximation in order to compute a large number of decimals of

π

{\displaystyle \pi }

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the Wren programming language

Source code in the wren programming language

import "./big" for BigRat

var digits = 500
var an = BigRat.one
var bn = BigRat.half.sqrt(digits)
var tn = BigRat.half.square
var pn = BigRat.one
while (pn <= digits) {
    var prevAn = an
    an = (bn + an) * BigRat.half
    bn = (bn * prevAn).sqrt(digits)
    prevAn = prevAn - an
    tn = tn - (prevAn.square * pn)
    pn = pn + pn
}
var pi = (an + bn).square / (tn * 4)
System.print(pi.toDecimal(digits, false))


  

You may also check:How to resolve the algorithm Juggler sequence step by step in the Wren programming language
You may also check:How to resolve the algorithm Loops/For step by step in the HolyC programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Unix/ls step by step in the 8080 Assembly programming language