How to resolve the algorithm Calculating the value of e step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Calculating the value of e step by step in the Haskell programming language

Table of Contents

Problem Statement

Calculate the value of   e.

(e   is also known as   Euler's number   and   Napier's constant.)

See details: Calculating the value of e

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Calculating the value of e step by step in the Haskell programming language

First Code:

  • The eApprox function takes an integer n and returns an approximation of the mathematical constant e obtained after n iterations.
  • It uses the scanl (*) 1 [1 ..] expression to generate a list of cumulative products from 1 to n, starting with an initial value of 1.
  • The (1 /) expression divides each element of the generated list by 1.
  • The take n expression takes the first n elements of the resulting list.
  • The sum function sums up the n elements to obtain the approximation.

Second Code:

  • This version of eApprox uses a different approach. It employs the foldr function to iteratively calculate the approximation.
  • The function \(x (fl, e) -> (,) <*> (e +) . (1 /) $ fl * x) takes a number x and a tuple (fl, e), where fl is the current factorial and e is the current approximation.
  • It returns a tuple where the first element is the new factorial fl * x and the second element is the new approximation e + (1 / fl * x).
  • The iterate function applies this function repeatedly to the initial value (1, (1, 1)) with the list [n, pred n .. 1] to compute the approximation.
  • The snd function extracts the second element of the resulting tuple, which is the approximation.

Third Code:

  • The approximatEs function generates a list of approximations to e using a loop.
  • It starts with the initial value (1, (1, 1)), where the first element is the approximation, and the second element is a tuple containing the factorial and the current iteration.
  • The iterate function repeatedly applies the following function to this initial value:
    • \(e, (i, n)) -> (,) . (e +) . (1 /) <*> (succ i,) $ i * n:
      • (succ i,) increments the current iteration i.
      • i * n multiplies the current iteration by the current factorial n.
      • (1 /) divides by the result of the previous step.
      • (e +) adds the result to the current approximation e.
      • (,) forms a new tuple with the updated approximation and the updated factorial and iteration.
  • The fst function extracts the first element of the resulting tuple, which is the list of approximations.

Source code in the haskell programming language

------ APPROXIMATION OF E OBTAINED AFTER N ITERATIONS ----

eApprox :: Int -> Double
eApprox n =
  (sum . take n) $ (1 /) <$> scanl (*) 1 [1 ..]

--------------------------- TEST -------------------------
main :: IO ()
main = print $ eApprox 20


------ APPROXIMATION OF E OBTAINED AFTER N ITERATIONS ----

eApprox n =
  snd $
    foldr
      ( \x (fl, e) ->
          (,) <*> (e +) . (1 /) $ fl * x
      )
      (1, 1)
      [n, pred n .. 1]

--------------------------- TEST -------------------------
main :: IO ()
main = print $ eApprox 20


{-# LANGUAGE TupleSections #-}

------------------- APPROXIMATIONS TO E ------------------

approximatEs :: [Double]
approximatEs =
  fst
    <$> iterate
      ( \(e, (i, n)) ->
          (,) . (e +) . (1 /) <*> (succ i,) $ i * n
      )
      (1, (1, 1))

--------------------------- TEST -------------------------
main :: IO ()
main = print $ approximatEs !! 17


  

You may also check:How to resolve the algorithm Magic squares of singly even order step by step in the Wren programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the COBOL programming language
You may also check:How to resolve the algorithm Combinations step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Function definition step by step in the Toka programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the F# programming language