MuPAD
Pro Computing Essentials - Examples
Example 9.3 - Continued Fractions
Here is the procedure to calculate the coefficients of a continued fraction for the two given numbers n and m, with a finite number of steps.
contFract := proc(n:Type::PosInt,
m:Type::PosInt,
steps:Type::PosInt)
local list, i, result, remainder;
begin
list:=[];
i:=1;
while i < steps do
i:=i+1;
result:=(n div m);
remainder:=(n mod m);
list:=list.[result];
if remainder<>0 and i<steps then
n:= m;
m:= remainder;
else
return(list)
end;
end;
end:
contFract(41319,3340,10)
[12, 2, 1, 2, 3, 2, 26, 2]
Now, we can check if we got the right result.
numlib::contfrac(41319/3340)
12 + 1/(2 + 1/(1 + 1/(2 + 1/(3 + 1/(2 + 1/(26 + 1/
(2 + 1/...)))))))
|