How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the RATFOR programming language
How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the RATFOR 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 RATFOR programming language
Source code in the ratfor programming language
# This implementation assumes the I/O facilities of gfortran, and so
# is not suited to f2c as the FORTRAN77 compiler.
function r2cf (N1, N2)
implicit none
integer N1, N2
integer r2cf
integer r
# We will use division with rounding towards zero, which is the
# native integer division method of FORTRAN77.
r2cf = N1 / N2
r = mod (N1, N2)
N1 = N2
N2 = r
end
subroutine wrr2cf (N1, N2) # Write r2cf results.
implicit none
integer N1, N2
integer r2cf
integer digit, M1, M2
integer sep
write (*, '(I0, "/", I0, " => ")', advance = "no") N1, N2
M1 = N1
M2 = N2
sep = 0
while (M2 != 0)
{
digit = r2cf (M1, M2)
if (sep == 0)
{
write (*, '("[", I0)', advance = "no") digit
sep = 1
}
else if (sep == 1)
{
write (*, '("; ", I0)', advance = "no") digit
sep = 2
}
else
{
write (*, '(", ", I0)', advance = "no") digit
}
}
write (*, '("]")', advance = "yes")
end
program demo
implicit none
call wrr2cf (1, 2)
call wrr2cf (3, 1)
call wrr2cf (23, 8)
call wrr2cf (13, 11)
call wrr2cf (22, 7)
call wrr2cf (-151, 77)
call wrr2cf (14142, 10000)
call wrr2cf (141421, 100000)
call wrr2cf (1414214, 1000000)
call wrr2cf (14142136, 10000000)
call wrr2cf (31, 10)
call wrr2cf (314, 100)
call wrr2cf (3142, 1000)
call wrr2cf (31428, 10000)
call wrr2cf (314285, 100000)
call wrr2cf (3142857, 1000000)
call wrr2cf (31428571, 10000000)
call wrr2cf (314285714, 100000000)
end
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the C++ programming language
You may also check:How to resolve the algorithm Logical operations step by step in the HicEst programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Spelling of ordinal numbers step by step in the Rust programming language