How to resolve the algorithm Continued fraction step by step in the Maxima programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Continued fraction step by step in the Maxima 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 Maxima programming language
Source code in the maxima programming language
cfeval(x) := block([a, b, n, z], a: x[1], b: x[2], n: length(a), z: 0,
for i from n step -1 thru 2 do z: b[i]/(a[i] + z), a[1] + z)$
cf_sqrt2(n) := [cons(1, makelist(2, i, 2, n)), cons(0, makelist(1, i, 2, n))]$
cf_e(n) := [cons(2, makelist(i, i, 1, n - 1)), append([0, 1], makelist(i, i, 1, n - 2))]$
cf_pi(n) := [cons(3, makelist(6, i, 2, n)), cons(0, makelist((2*i - 1)^2, i, 1, n - 1))]$
cfeval(cf_sqrt2(20)), numer; /* 1.414213562373097 */
% - sqrt(2), numer; /* 1.3322676295501878*10^-15 */
cfeval(cf_e(20)), numer; /* 2.718281828459046 */
% - %e, numer; /* 4.4408920985006262*10^-16 */
cfeval(cf_pi(20)), numer; /* 3.141623806667839 */
% - %pi, numer; /* 3.115307804568701*10^-5 */
/* convergence is much slower for pi */
fpprec: 20$
x: cfeval(cf_pi(10000))$
bfloat(x - %pi); /* 2.4999999900104930006b-13 */
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the Forth programming language
You may also check:How to resolve the algorithm AKS test for primes step by step in the Scheme programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Wireworld step by step in the Wren programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Pict programming language