5
$\begingroup$

The $n^{\text{th}}$ negative continued fraction convergent $x_n$ of a positive real $x$ is computed by the nested function \begin{align} x_n = k_1 - \frac{1}{k_2 - \frac{1}{k_3 - \dots - \tfrac{1}{k_n}}}, \end{align} where $k_1, \dots, k_n$ are positive integers for all $n \in \mathbb{N}$. To compute the first $n$ negative continued fraction coefficients, namely, $\{k_1, \dots, k_n \}$, of an arbitrary irrational $x$, I use the following example Mathematica code:

 x = Sqrt[2];
 n = 15;
 Map[#[[1, 1]] &, Rest[NestList[1/(Ceiling[#] - #) &, x, n]]]. 

which outputs $\{ 2, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4 \}$. However, this code is not suitable if $x$ is a rational, since the recurrence diverges when $\#$ is an integer. How might I modify the above code to allow for rational $x$ and output the finite list of coefficients of its complete negative continued fraction?

$\endgroup$
8
$\begingroup$

One can implement a general formula:

$$ x = a_1 + \cfrac{b}{a_2 + \cfrac{b}{a_3 + \cfrac{b}{\ddots + \cfrac{b}{a_n} } } } $$

continuedFraction[x_, n_, b_: 1] := 
  Sign[b] Reap[NestWhile[b/# - Sow@Floor[b/#] &, Abs[b]/x, # != 0 &, 1, n]][[2, 1]];

If b = 1 it is the regular continued fraction

ContinuedFraction[Sqrt[2], 10]
continuedFraction[Sqrt[2], 10]

{1, 2, 2, 2, 2, 2, 2, 2, 2, 2}

{1, 2, 2, 2, 2, 2, 2, 2, 2, 2}

If b = -1 it is the negative continued fraction

continuedFraction[Sqrt[2], 10, -1]

{2, 2, 4, 2, 4, 2, 4, 2, 4, 2}

It also works for rationals (it can has less then n elements)

continuedFraction[4/15, 10, -1]

{1, 2, 2, 3, 2, 2}

1 - 1/(2 - 1/(2 - 1/(3 - 1/(2 - 1/2))))

4/15

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.