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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Continued fraction step by step in the Erlang 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 Erlang programming language

Source code in the erlang programming language

-module(continued).
-compile([export_all]).

pi_a (0) -> 3;
pi_a (_N) -> 6.

pi_b (N) ->
    (2*N-1)*(2*N-1).

sqrt2_a (0) ->
    1;
sqrt2_a (_N) ->
    2.

sqrt2_b (_N) ->
    1.

nappier_a (0) ->
    2;
nappier_a (N) ->
    N.

nappier_b (1) ->
    1;
nappier_b (N) ->
    N-1.

continued_fraction(FA,_FB,0) -> FA(0);
continued_fraction(FA,FB,N) ->
    continued_fraction(FA,FB,N-1,FB(N)/FA(N)).

continued_fraction(FA,_FB,0,Acc) -> FA(0) + Acc;
continued_fraction(FA,FB,N,Acc) ->
    continued_fraction(FA,FB,N-1,FB(N)/ (FA(N) + Acc)).

test_pi (N) ->
    continued_fraction(fun pi_a/1,fun pi_b/1,N).                                                                                                                                                                                                                                                                              

test_sqrt2 (N) ->
    continued_fraction(fun sqrt2_a/1,fun sqrt2_b/1,N).

test_nappier (N) ->
    continued_fraction(fun nappier_a/1,fun nappier_b/1,N).


29> continued:test_pi(1000).
3.141592653340542
30> continued:test_sqrt2(1000).
1.4142135623730951
31> continued:test_nappier(1000).
2.7182818284590455


  

You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Elixir programming language
You may also check:How to resolve the algorithm Dining philosophers step by step in the Python programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Red programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Logo programming language
You may also check:How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the J programming language