user
Given a positive real number, if we truncate its continued fraction representation at a certain depth, we obtain a rational approximation to the real number. The sequence of successively better such approximations is its convergent sequence.

Problem:

* Given a positive rational number 

  m
  n
  {\displaystyle {\displaystyle {\frac {m}{n}}}}
  , specified by two positive integers 

  m
  ,
  n
  {\displaystyle {\displaystyle m,n}}
  , output its entire sequence of convergents.
* Given a quadratic real number 

  b


  a
  +
  m
  n
  >
  0
  {\displaystyle {\displaystyle {\frac {b{\sqrt {a}}+m}{n}}>0}}
  , specified by integers 

  a
  ,
  b
  ,
  m
  ,
  n
  {\displaystyle {\displaystyle a,b,m,n}}
  , where 

  a
  {\displaystyle {\displaystyle a}}
   is not a perfect square, output the first 

  k
  {\displaystyle {\displaystyle k}}
   convergents when given a positive number 

  k
  {\displaystyle {\displaystyle k}}
  .

The output format can be whatever is necessary to represent rational numbers, but it probably should be a 2-tuple of integers.

For example, given 

a
=
2
,
b
=
1
,
m
=
0
,
n
=
1
{\displaystyle {\displaystyle a=2,b=1,m=0,n=1}}
, since

2
=
1
+

1

2
+

1

2
+

1

2
+
⋱
{\displaystyle {\sqrt {2}}=1+{\cfrac {1}{2+{\cfrac {1}{2+{\cfrac {1}{2+\ddots }}}}}}}
the program should output 

(
1
,
1
)
,
(
3
,
2
)
,
(
7
,
5
)
,
(
17

/
12
)
,
(
41

/
29
)
,
…
{\displaystyle {\displaystyle (1,1),(3,2),(7,5),(17/12),(41/29),\dots }}
.

A simple check is to do this for the golden ratio **Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest\_v1/":): {\displaystyle \frac{\sqrt{5}+1}{2}}**
, that is, **Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest\_v1/":): {\displaystyle a=5, b=1, m = 1, n = 2 }**
, which should output 

(
1
,
1
)
,
(
2
,
1
)
,
(
3
,
2
)
,
(
5
,
3
)
,
(
8
,
5
)
,
…
{\displaystyle {\displaystyle (1,1),(2,1),(3,2),(5,3),(8,5),\dots }}
.

Print the results for 415/93, 649/200, 

2
{\displaystyle {\displaystyle {\sqrt {2}}}}
, **Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest\_v1/":): {\displaystyle \sqrt{5}}**
, and the golden ratio.

References and related tasks

* Wikipedia: Continued fraction
* Continued fraction
* Continued fraction/Arithmetic
* Continued fraction/Arithmetic/Construct from rational number
* Calkin-Wilf sequence
assistant
**Translation of**: Julia

```
# 20240210 Raku programming solution

sub convergents(Real $x is copy, Int $maxcount) {
   my @components = gather for ^$maxcount {
      my $fpart = $x - take $x.Int;
      $fpart == 0 ?? ( last ) !! ( $x = 1 / $fpart )
   }
   my ($numa, $denoma, $numb, $denomb) = 1, 0, @components[0], 1;
   return [ Rat.new($numb, $denomb) ].append: gather for @components[1..*] -> $comp {
      ( $numa, $denoma, $numb                , $denomb                  ) 
      = $numb, $denomb, $numa + $comp * $numb, $denoma + $comp * $denomb;
      take Rat.new($numb, $denomb);
   }
}

my @tests = [ "415/93", 415/93, "649/200", 649/200, "sqrt(2)", sqrt(2),
              "sqrt(5)", sqrt(5), "golden ratio", (sqrt(5) + 1) / 2     ];

say "The continued fraction convergents for the following (maximum 8 terms) are:";
for @tests -> $s, $x {
   say $s.fmt('%15s') ~ " = { convergents($x, 8).map: *.nude.join('/') } ";
}

```

Output:

```
The continued fraction convergents for the following (maximum 8 terms) are:
         415/93 = 4/1 9/2 58/13 415/93 
        649/200 = 3/1 13/4 159/49 649/200 
        sqrt(2) = 1/1 3/2 7/5 17/12 41/29 99/70 239/169 577/408 
        sqrt(5) = 2/1 9/4 38/17 161/72 682/305 2889/1292 12238/5473 51841/23184 
   golden ratio = 1/1 2/1 3/2 5/3 8/5 13/8 21/13 34/21 

```

You may Attempt This Online!