How to resolve the algorithm Continued fraction step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Continued fraction step by step in the Julia programming language

Table of Contents

Problem Statement

The task is to write a program which generates such a number and prints a real representation of it. The code should be tested by calculating and printing the square root of 2, Napier's Constant, and Pi, using the following coefficients: For the square root of 2, use

a

0

= 1

{\displaystyle a_{0}=1}

then

a

N

= 2

{\displaystyle a_{N}=2}

.

b

N

{\displaystyle b_{N}}

is always

1

{\displaystyle 1}

. For Napier's Constant, use

a

0

= 2

{\displaystyle a_{0}=2}

, then

a

N

= N

{\displaystyle a_{N}=N}

.

b

1

= 1

{\displaystyle b_{1}=1}

then

b

N

= N − 1

{\displaystyle b_{N}=N-1}

. For Pi, use

a

0

= 3

{\displaystyle a_{0}=3}

then

a

N

= 6

{\displaystyle a_{N}=6}

.

b

N

= ( 2 N − 1

)

2

{\displaystyle b_{N}=(2N-1)^{2}}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Continued fraction step by step in the Julia programming language

The code you provided is a Julia program that computes the continued fraction approximations of some mathematical constants. The cf function takes three arguments: the initial value of the continued fraction, the sequence of coefficients for the numerator, and the sequence of coefficients for the denominator. The function computes the continued fraction approximation by repeatedly multiplying the current value of the fraction by the next coefficient in the numerator and denominator sequences. The function stops when the current approximation is close enough to the true value of the constant, as determined by the isapprox function.

The out function is a helper function that prints the name of the constant and its approximation.

The foreach function iterates over the key-value pairs in the dictionary, calling the out function on each pair.

The output of the program is as follows:

√2: 1.41421356237 ≈ 1.41421356237
ℯ: 2.71828182845 ≈ 2.71828182845
π: 3.14159265359 ≈ 3.14159265359

Source code in the julia programming language

using .Iterators: countfrom, flatten, repeated, zip
using .MathConstants: 
using Printf

function cf(a₀, a, b = repeated(1))
	m = BigInt[a₀ 1; 1 0]
    for (aᵢ, bᵢ) ∈ zip(a, b)
        m *= [aᵢ 1; bᵢ 0]
        isapprox(m[1]/m[2], m[3]/m[4]; atol = 1e-12) && break
    end
	m[1]/m[2]
end

out((k, v)) = @printf "%2s: %.12f ≈ %.12f\n" k v eval(k)

foreach(out, (
    :(2) => cf(1, repeated(2)),
    :    => cf(2, countfrom(), flatten((1, countfrom()))),
    :π    => cf(3, repeated(6), (k^2 for k ∈ countfrom(1, 2)))))


  

You may also check:How to resolve the algorithm Flipping bits game step by step in the Python programming language
You may also check:How to resolve the algorithm Animation step by step in the Prolog programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the C# programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the QB64 programming language
You may also check:How to resolve the algorithm Benford's law step by step in the Fortran programming language