How to resolve the algorithm Almkvist-Giullera formula for pi step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Almkvist-Giullera formula for pi step by step in the Haskell programming language

Table of Contents

Problem Statement

The Almkvist-Giullera formula for calculating   1/π2   is based on the Calabi-Yau differential equations of order 4 and 5,   which were originally used to describe certain manifolds in string theory.

The formula is:

This formula can be used to calculate the constant   π-2,   and thus to calculate   π. Note that, because the product of all terms but the power of 1000 can be calculated as an integer, the terms in the series can be separated into a large integer term: multiplied by a negative integer power of 10:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Almkvist-Giullera formula for pi step by step in the Haskell programming language

This code implements the Almkvist-Giullera series, which is an algorithm for approximating the value of pi. The formula for the Nth term of the series is:

(532n² + 126n + 9) / 3 * (6n)! / 3n^6

The code first calculates the integral part of the Nth term of the series, which is the numerator of the fraction divided by the denominator. It then calculates the exponent of 10 in the Nth term, which is 3 minus 6 times the term number.

The Nth term of the series is then calculated by dividing the integral part by 10 raised to the exponent. The sum of the first N terms is calculated by summing the Nth terms for all values of n from 0 to N.

The approximation of pi from the first N terms is then calculated by taking the square root of 1 divided by the sum of the first N terms.

The code also defines some utility functions, including factorial and exponentiation for arbitrary-precision integers.

(532n² + 126n + 9) / 3 * (6n)! / 3n^6

This Haskell code evaluates a mathematical expression involving factorials and exponentiation. Let's break it down step by step:

1. Parentheses:

  • The expression is enclosed in parentheses, indicating that it should be evaluated as a whole.

2. Subexpression 1:

  • 532n² + 126n + 9
    • n is a variable representing a numeric value.
    • This part calculates a quadratic polynomial in terms of n.

3. Division by 3:

  • The result of the first subexpression is divided by 3.

4. Factorial of 6n:

  • (6n)! calculates the factorial of 6 times n.
    • The factorial operation (!) multiplies a number by all the positive integers less than itself.
    • For example, (6n)! is equal to 6n * 5n * 4n * 3n * 2n * n.

5. Division by 3n^6:

  • The result of the factorial is divided by 3n^6.
    • n^6 raises n to the power of 6.

6. Multiplication:

  • The results of the previous steps are multiplied together.

Putting it all together:

The code calculates the value of the following expression:

(532n² + 126n + 9) / 3 * (6n)! / 3n^6

This expression involves a quadratic polynomial, a factorial, and exponentiation. The result is a numeric value that depends on the value of n.

Source code in the haskell programming language

import Control.Monad
import Data.Number.CReal
import GHC.Integer
import Text.Printf

iterations = 52
main = do
  printf "N. %44s %4s %s\n" 
          "Integral part of Nth term" "×10^" "=Actual value of Nth term"

  forM_ [0..9] $ \n ->
    printf "%d. %44d %4d %s\n" n
                               (almkvistGiulleraIntegral n)
                               (tenExponent n)
                               (showCReal 50 (almkvistGiullera n))

  printf "\nPi after %d iterations:\n" iterations
  putStrLn $ showCReal 70 $ almkvistGiulleraPi iterations

-- The integral part of the Nth term in the Almkvist-Giullera series
almkvistGiulleraIntegral n =
  let polynomial  = (532 `timesInteger` n `timesInteger` n) `plusInteger` (126 `timesInteger` n) `plusInteger` 9
      numerator   = 32 `timesInteger` (facInteger (6 `timesInteger` n)) `timesInteger` polynomial
      denominator = 3 `timesInteger` (powInteger (facInteger n) 6)
   in numerator `divInteger` denominator

-- The exponent for 10 in the Nth term of the series
tenExponent n = 3 `minusInteger` (6 `timesInteger` (1 `plusInteger` n))

-- The Nth term in the series (integral * 10^tenExponent)
almkvistGiullera n = fromInteger (almkvistGiulleraIntegral n) / fromInteger (powInteger 10 (abs (tenExponent n)))

-- The sum of the first N terms
almkvistGiulleraSum n = sum $ map almkvistGiullera [0 .. n]

-- The approximation of pi from the first N terms
almkvistGiulleraPi n = sqrt $ 1 / almkvistGiulleraSum n

-- Utility: factorial for arbitrary-precision integers
facInteger n = if n `leInteger` 1 then 1 else n `timesInteger` facInteger (n `minusInteger` 1)

-- Utility: exponentiation for arbitrary-precision integers
powInteger 1 _ = 1
powInteger _ 0 = 1
powInteger b 1 = b
powInteger b e = b `timesInteger` powInteger b (e `minusInteger` 1)


  

You may also check:How to resolve the algorithm Anonymous recursion step by step in the Scala programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Lang programming language
You may also check:How to resolve the algorithm URL encoding step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Next highest int from digits step by step in the Wren programming language