How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the zkl programming language
How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the zkl programming language
Table of Contents
Problem Statement
The purpose of this task is to write a function
r 2 c f
(
i n t
{\displaystyle {\mathit {r2cf}}(\mathrm {int} }
N
1
,
i n t
{\displaystyle N_{1},\mathrm {int} }
N
2
)
{\displaystyle N_{2})}
, or
r 2 c f
(
F r a c t i o n
{\displaystyle {\mathit {r2cf}}(\mathrm {Fraction} }
N )
{\displaystyle N)}
, which will output a continued fraction assuming: The function should output its results one digit at a time each time it is called, in a manner sometimes described as lazy evaluation. To achieve this it must determine: the integer part; and remainder part, of
N
1
{\displaystyle N_{1}}
divided by
N
2
{\displaystyle N_{2}}
. It then sets
N
1
{\displaystyle N_{1}}
to
N
2
{\displaystyle N_{2}}
and
N
2
{\displaystyle N_{2}}
to the determined remainder part. It then outputs the determined integer part. It does this until
a b s
(
N
2
)
{\displaystyle \mathrm {abs} (N_{2})}
is zero. Demonstrate the function by outputing the continued fraction for:
2
{\displaystyle {\sqrt {2}}}
should approach
[ 1 ; 2 , 2 , 2 , 2 , … ]
{\displaystyle [1;2,2,2,2,\ldots ]}
try ever closer rational approximations until boredom gets the better of you: Try : Observe how this rational number behaves differently to
2
{\displaystyle {\sqrt {2}}}
and convince yourself that, in the same way as
3.7
{\displaystyle 3.7}
may be represented as
3.70
{\displaystyle 3.70}
when an extra decimal place is required,
[ 3 ; 7 ]
{\displaystyle [3;7]}
may be represented as
[ 3 ; 7 , ∞ ]
{\displaystyle [3;7,\infty ]}
when an extra term is required.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the zkl programming language
Source code in the zkl programming language
fcn r2cf(nom,dnom){ // -->Walker (iterator)
Walker.tweak(fcn(state){
nom,dnom:=state;
if(dnom==0) return(Void.Stop);
n,d:=nom.divr(dnom);
state.clear(dnom,d);
n
}.fp(List(nom,dnom))) // partial application (light weight closure)
}
fcn r2cf2(nom,dnom){ // -->Generator (heavy weight Walker)
Utils.Generator(fcn(nom,dnom){
while(dnom){
n,d:=nom.divr(dnom); nom,dnom=dnom,d;
vm.yield(n);
}
Void.Stop;
},nom,dnom)
}
foreach nom,dnom in (T(T(1,2), T(3,1), T(23,8), T(13,11), T(22,7),
T(14142,10000), T(141421,100000), T(1414214,1000000),
T(14142136,10000000))){
r2cf(nom,dnom).walk(25).println(); // print up to 25 numbers
}
You may also check:How to resolve the algorithm Permutations step by step in the Wren programming language
You may also check:How to resolve the algorithm Array length step by step in the SmallBASIC programming language
You may also check:How to resolve the algorithm Runge-Kutta method step by step in the Maxima programming language
You may also check:How to resolve the algorithm Permutations/Rank of a permutation step by step in the Raku programming language
You may also check:How to resolve the algorithm Reflection/Get source step by step in the Lingo programming language