# Deriving formulas for continued fraction expansions

A version of a continued fraction expansion of a rational number $$r\in \mathbb Q$$ is defined as \begin{align} r =[a_0,a_1,a_2,\ldots,a_k]= a_0 - \frac{1}{a_1 - \frac{1}{a_2 - \dots - \tfrac{1}{a_k}}} \end{align} for integers $$a_i\leq-2$$.

For some given rational number $$r$$ there is an easy algorithm based on a variation of the Euclidean algorithm to determine the continued fraction expansion $$[a_0,\ldots,a_k]$$ of $$r$$. An implementation is for example described here.

However, there are such beautiful formulas as

$$\begin{equation} -\frac{qt-1}{q(t-1)-1}=[\underbrace{-2,\ldots,-2}_{(t-2)-\text{times}},-3,\underbrace{-2,\ldots,-2}_{(q-2)-\text{times}}] \end{equation}$$

for integers $$t,q\geq2$$. Such formulas can be derived via the above mentioned algorithm.

My question is if there exist a way to derive such formulas via Mathematica, i.e. given for example the expression $$-\frac{qt-1}{q(t-1)-1}$$ is there a way to derive its expression as $$[\underbrace{-2,\ldots,-2}_{(t-2)-\text{times}},-3,\underbrace{-2,\ldots,-2}_{(q-2)-\text{times}}]$$ via Mathematica?

I know the function ContinuedFractionK which is inverse (for a slightly different version of continued fraction expansions: with plus instead of minus) to what I am looking for.

Try FromContinuedFraction

f[t_, q_] := { FromContinuedFraction[Join[ConstantArray[-2, t - 2], {-3},ConstantArray[-2, q - 2] ]], -((q t - 1)/(q (t - 1) - 1))}


but there seems to be somthing wrong with your formula:

f[4, 5]
(*{-(229/94), -(19/14)}*)

• Thank you for the answer. As far as I understood FromContinuedFraction is using a different convention for the continued fraction (+ instead of -). I think that is why the wrong results occur. Please correct me if I am wrong, but it seems to me that your code is checking the specific formula for explicit values of t and q. My question is however about creating the formulas. Aug 6 '20 at 14:34
• @MarcKegel The different convention might give a different sign, not more, I think. Could you please give me an example of your formula? Perhaps ContinuedFractionK, which can be applied to symbols, is what you are looking for? Aug 6 '20 at 15:34
• Sure. Here is an example: Take $t=2$ and $q=3$. Then $-(qt-1)/(q(t-1)-1)=-5/2$. The continued fraction with - gives $[-3,-2]=-3-1/-2=-5/2$. The continued fraction with + gives $-3+1/-2=-7/2$. ContinuedFractionK seems to be the inverse to what I am looking for. It starts with a continued fraction and derives the rational number out of it and it works with symbols. I want to go the reverse way. Start with a rational number depending on variables and find its continued fraction expansion depending on the same variables. Aug 6 '20 at 17:56
• Apply the Equivalence Transformation here to express it as a simple continued fraction (all numerators +1), then apply ContinuedFractionK. Sep 9 '20 at 12:53
• @DanielW i understand that the equivalence transformation is converting the continued fraction expansions with - to the continued fraction expansions with + and that ContinuedFractionK converts a continued fraction into a rational. But I do not see how this solves the problem. Could you give me a few more hints? Sep 10 '20 at 14:45