How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the Visual Basic .NET 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 Visual Basic .NET 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 Visual Basic .NET programming language

Source code in the visual programming language

Imports System, System.Numerics

Module Program
    Function IntSqRoot(ByVal valu As BigInteger, ByVal guess As BigInteger) As BigInteger
        Dim term As BigInteger : Do
            term = valu / guess
            If BigInteger.Abs(term - guess) <= 1 Then Exit Do
            guess += term : guess >>= 1
        Loop While True : Return guess
    End Function

    Function ISR(ByVal term As BigInteger, ByVal guess As BigInteger) As BigInteger
        Dim valu As BigInteger = term * guess : Do
            If BigInteger.Abs(term - guess) <= 1 Then Exit Do
            guess += term : guess >>= 1 : term = valu / guess
        Loop While True : Return guess
    End Function

    Function CalcAGM(ByVal lam As BigInteger, ByVal gm As BigInteger, ByRef z As BigInteger,
                     ByVal ep As BigInteger) As BigInteger
        Dim am, zi As BigInteger : Dim n As ULong = 1 : Do
            am = (lam + gm) >> 1 : gm = ISR(lam, gm)
            Dim v As BigInteger = am - lam
            zi = v * v * n : If zi < ep Then Exit Do
            z -= zi : n <<= 1 : lam = am
        Loop While True : Return am
    End Function

    Function BIP(ByVal exp As Integer, ByVal Optional man As ULong = 1) As BigInteger
        Dim rv As BigInteger = BigInteger.Pow(10, exp) : Return If(man = 1, rv, man * rv)
    End Function

    Sub Main(args As String())
        Dim d As Integer = 25000
        If args.Length > 0 Then
            Integer.TryParse(args(0), d)
            If d < 1 OrElse d > 999999 Then d = 25000
        End If
        Dim st As DateTime = DateTime.Now
        Dim am As BigInteger = BIP(d),
            gm As BigInteger = IntSqRoot(BIP(d + d - 1, 5),
                                         BIP(d - 15, Math.Sqrt(0.5) * 1.0E+15)),
             z As BigInteger = BIP(d + d - 2, 25),
             agm As BigInteger = CalcAGM(am, gm, z, BIP(d + 1)),
             pi As BigInteger = agm * agm * BIP(d - 2) / z
        Console.WriteLine("Computation time: {0:0.0000} seconds ",
                          (DateTime.Now - st).TotalMilliseconds / 1000)
        If args.Length > 1 OrElse d <= 1000 Then
            Dim s As String = pi.ToString()
            Console.WriteLine("{0}.{1}", s(0), s.Substring(1))
        End If
        If Diagnostics.Debugger.IsAttached Then Console.ReadKey()
    End Sub
End Module


  

You may also check:How to resolve the algorithm Snake step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Miranda programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Oz programming language
You may also check:How to resolve the algorithm Compiler/virtual machine interpreter step by step in the Fortran programming language
You may also check:How to resolve the algorithm String length step by step in the Maxima programming language