Continued Fractions

The computation of the continued fraction of a rational number, the evaluation of its convergents
   and other operations can be carried out easily in Mathematica.  The following functions compute the  simple continued fraction of a rational number, evaluate a continued fraction and compute the convergents of  a continued fraction. A simple continued fraction  
   is denoted by  {a0,a1,a2,....,an}. 
   
    The function Cfraction[a,b] computes the continued fraction of a/b. Fraction determined the rational number represented by  the given continued fraction.  convergents  outputs a list of all the convergents of a continued fraction.
    
    Remark: We start the terms of the continued fraction with a0, while in Mathematica  the first element of a list  a  is   a1. This accounts for the difference in the formulas given in the text and in the  programs below.

Cfraction[a_,b_] :=
If[Mod[a,b]==0,Quotient[a,b],
   Flatten[{Quotient[a,b],Cfraction[b,Mod[a,b]]}]]

fraction[a_List]:= If[Length[a]==1, First[a],
     First[a]+1/fraction[Rest[a]]]

convergents[frac_List]:=
       Module[{p,q,n,c },
          n=Length[frac];
          p[-1]=1;
          p[0]=frac[[1]];
          q[-1]=0;
          q[0]=1;
          Do[p[i]= frac[[i+1]]p[i-1]+ p[i-2],{i,1,n-1}];
          Do[q[i]= frac[[i+1]]q[i-1]+ q[i-2],{i,1,n-1}];
             c=Table[p[i]/q[i] ,{i,0,n-1}] ;
               Return[c]]

Cfraction[257, 97]

fraction[{2,1,1,1,5,1,4}]

convergents[ Cfraction[257,97]]

convergents[{1,1,1,1,1,1,1,1}]