2

The initial mathematical problem is to solve for $x$ in:

$$x = \sqrt{19} + \frac{91}{\sqrt{19} + \frac{91}{\sqrt{19} + \frac{91}{\sqrt{19} + \frac{91}{\sqrt{19} + \frac{91}{x}}}}}$$

(Notice the $x$ in the right-hand-side.)

The following direct substitution indeed gives the correct answer:

Solve[x == Sqrt[19]+ 91/(Sqrt[19] + 91/(Sqrt[19] + 91/(Sqrt[19] + 91/(Sqrt[19] + 91/x)))),x]

$\left\{\left\{x\to \frac{1}{2} \left(\sqrt{19}-\sqrt{383}\right)\right\},\left\{x\to \frac{1}{2} \left(\sqrt{19}+\sqrt{383}\right)\right\}\right\}$

I'm interested in solving the problem using FromContinuedFraction. If I convert the problem by hand into the form for ContinuedFraction, I get:

Solve[x == FromContinuedFraction[{Sqrt[19],Sqrt[19]/91, Sqrt[19],Sqrt[19]/91, Sqrt[19],x/91}],x]

which yields the correct answer.

However, I'd like to automatically convert the right-hand-side of the initial equation into the form of ContinuedFraction--for this and related problems.

The obvious approach doesn't work:

ContinuedFraction[Sqrt[19] + 91/(Sqrt[19] + 91/(Sqrt[19] + 91/(Sqrt[19] + 91/(Sqrt[19] + 91/x))))]

How can I automatically extract the terms in the right-hand-side and place them in a list appropriate for ContinuedFraction and FromContinuedFraction? Note that we cannot solve for $x$ and then express $x$ as a continued fraction because $x$ is a term within the continued fraction.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • 1
    Solve[x == Nest[91/# + Sqrt[19] &, x, 5]]? – cvgmt Aug 05 '21 at 03:09
  • 1
    I like your solution ($+1$)... but it isn't for my problem. How do I extract the desired list of coefficients needed for ContinuedFraction? – David G. Stork Aug 05 '21 at 03:18
  • I don't understand your question. What is the purpose of converting the right side of the equation to FromContinuedFraction[...]? It can be done by using some code automatically, but why? – Somos Aug 06 '21 at 01:45
  • @Somos: I want to solve the original equation by the method Solve[x = ContinuedFraction[...],x] as an alternate method, partly to show students the power of ContinuedFraction[]. Indeed, one can use it in the "hand crafted" case I showed. But for large, complex continued fractions, converting the CF "by hand" is arduous and error prone. Hence I'd like to be able to perform this (i.e., get the list of coefficients) automatically. – David G. Stork Aug 06 '21 at 01:59

2 Answers2

2

Not easy since getting the depth is awkward. You can write the equation as:

x == 91/ContinuedFractionK[91, If[n == 6, x, Sqrt[19]], {n, 6}]

You can put in dummy constants $a,b$ and use an If to switch to x at depth 6:

a/ContinuedFractionK[a, If[n == 6, x, b], {n, 6}]

You could use this form to SolveAlways which finds the coefficients that make it match the rhs of your equation - though I have to do Last as SolveAlways returns zero solutions:

sol = Last@
  SolveAlways[
   a/ContinuedFractionK[a, If[n == 6, x, b], {n, 6}] == expr[[2]], x]

Then you can solve by putting together the above:

Values@Flatten@
  Solve[x == (a/ContinuedFractionK[a, If[n == 6, x, b], {n, 6}]) /. 
    sol, x]

Unfortunately, none of that seems to be as convenient and general as you'd like. Also simply using ContinuedFraction instead of ContinuedFractionK is not going to work because ContinuedFraction has 1 as the numerators, not some custom number.

flinty
  • 25,147
  • 2
  • 20
  • 86
1

From this answer:

There is the code:

n = {n1, n2, n3, n4, n5};
d = {d2, d3, d4, d5, x};
f[y_, {m_, d_}] := m/(d + y);
d1 + Fold[f, Last@n/Last@d, Reverse@Most@Transpose@{n, d}]

which gives the output:

$$\text{d1}+\frac{\text{n1}}{\text{d2}+\frac{\text{n2}}{\text{d3}+\frac{\text{n3}}{\text{d4}+\frac{\text{n4}}{\text{d5}+\frac{\text{n5}}{x}}}}}$$

The code above can be modified into:

Clear[x, y, nn, n, j, d, d0, nnn];
nnn = 5;
Table[numerators = 
  Table[ToExpression[StringJoin["n", ToString[j]]], {j, 1, nn}];
 denominators = 
  Flatten[{Table[
     ToExpression[StringJoin["d", ToString[j]]], {j, 2, nn}], x}];
 f[y_, {m_, denominators_}] := m/(denominators + y);
 Solve[x == 
   d1 + Fold[f, Last@numerators/Last@denominators, 
     Reverse@Most@Transpose@{numerators, denominators}], x], {nn, 1, 
  nnn}]

which gives the output:

{{{x -> 1/2 (d1 - Sqrt[d1^2 + 4 n1])}, {x -> 
    1/2 (d1 + Sqrt[d1^2 + 4 n1])}}, {{x -> (
    d1 d2 + n1 - n2 - Sqrt[4 d1 d2 n2 + (-d1 d2 - n1 + n2)^2])/(
    2 d2)}, {x -> (
    d1 d2 + n1 - n2 + Sqrt[4 d1 d2 n2 + (-d1 d2 - n1 + n2)^2])/(
    2 d2)}}, {{x -> (1/(
    2 (d2 d3 + n2)))(d1 d2 d3 + d3 n1 + d1 n2 - d2 n3 - 
      Sqrt[(-d1 d2 d3 - d3 n1 - d1 n2 + d2 n3)^2 - 
       4 (d2 d3 + n2) (-d1 d2 n3 - n1 n3)])}, {x -> (1/(
    2 (d2 d3 + n2)))(d1 d2 d3 + d3 n1 + d1 n2 - d2 n3 + 
      Sqrt[(-d1 d2 d3 - d3 n1 - d1 n2 + d2 n3)^2 - 
       4 (d2 d3 + n2) (-d1 d2 n3 - n1 n3)])}}, {{x -> (d1 d2 d3 d4 + 
       d3 d4 n1 + d1 d4 n2 + d1 d2 n3 + n1 n3 - d2 d3 n4 - 
       n2 n4 - \[Sqrt]((-d1 d2 d3 d4 - d3 d4 n1 - d1 d4 n2 - 
            d1 d2 n3 - n1 n3 + d2 d3 n4 + n2 n4)^2 - 
          4 (d2 d3 d4 + d4 n2 + d2 n3) (-d1 d2 d3 n4 - d3 n1 n4 - 
             d1 n2 n4)))/(2 (d2 d3 d4 + d4 n2 + 
         d2 n3))}, {x -> (d1 d2 d3 d4 + d3 d4 n1 + d1 d4 n2 + 
       d1 d2 n3 + n1 n3 - d2 d3 n4 - 
       n2 n4 + \[Sqrt]((-d1 d2 d3 d4 - d3 d4 n1 - d1 d4 n2 - 
            d1 d2 n3 - n1 n3 + d2 d3 n4 + n2 n4)^2 - 
          4 (d2 d3 d4 + d4 n2 + d2 n3) (-d1 d2 d3 n4 - d3 n1 n4 - 
             d1 n2 n4)))/(2 (d2 d3 d4 + d4 n2 + 
         d2 n3))}}, {{x -> (d1 d2 d3 d4 d5 + d3 d4 d5 n1 + 
       d1 d4 d5 n2 + d1 d2 d5 n3 + d5 n1 n3 + d1 d2 d3 n4 + d3 n1 n4 +
        d1 n2 n4 - d2 d3 d4 n5 - d4 n2 n5 - 
       d2 n3 n5 - \[Sqrt]((-d1 d2 d3 d4 d5 - d3 d4 d5 n1 - 
            d1 d4 d5 n2 - d1 d2 d5 n3 - d5 n1 n3 - d1 d2 d3 n4 - 
            d3 n1 n4 - d1 n2 n4 + d2 d3 d4 n5 + d4 n2 n5 + 
            d2 n3 n5)^2 - 
          4 (d2 d3 d4 d5 + d4 d5 n2 + d2 d5 n3 + d2 d3 n4 + 
             n2 n4) (-d1 d2 d3 d4 n5 - d3 d4 n1 n5 - d1 d4 n2 n5 - 
             d1 d2 n3 n5 - n1 n3 n5)))/(2 (d2 d3 d4 d5 + d4 d5 n2 + 
         d2 d5 n3 + d2 d3 n4 + n2 n4))}, {x -> (d1 d2 d3 d4 d5 + 
       d3 d4 d5 n1 + d1 d4 d5 n2 + d1 d2 d5 n3 + d5 n1 n3 + 
       d1 d2 d3 n4 + d3 n1 n4 + d1 n2 n4 - d2 d3 d4 n5 - d4 n2 n5 - 
       d2 n3 n5 + \[Sqrt]((-d1 d2 d3 d4 d5 - d3 d4 d5 n1 - 
            d1 d4 d5 n2 - d1 d2 d5 n3 - d5 n1 n3 - d1 d2 d3 n4 - 
            d3 n1 n4 - d1 n2 n4 + d2 d3 d4 n5 + d4 n2 n5 + 
            d2 n3 n5)^2 - 
          4 (d2 d3 d4 d5 + d4 d5 n2 + d2 d5 n3 + d2 d3 n4 + 
             n2 n4) (-d1 d2 d3 d4 n5 - d3 d4 n1 n5 - d1 d4 n2 n5 - 
             d1 d2 n3 n5 - n1 n3 n5)))/(2 (d2 d3 d4 d5 + d4 d5 n2 + 
         d2 d5 n3 + d2 d3 n4 + n2 n4))}}}

from which it should be possible to spot the general pattern if you vary the variable nnn=5.

Mats Granvik
  • 1,159
  • 5
  • 18
  • Well... "from which it should be possible to spot the general pattern if you vary the variable." I appreciate your effort ($+1$), but "spotting the general pattern" simply eludes me, and generalizing to arbitrary problems seems tricky indeed. Regardless, after all this work we don't get the list of terms needed for ContinuedFraction (or ContinuedFractionK). – David G. Stork Aug 05 '21 at 18:23
  • Try setting nnn=9 or greater. To me there is a pattern, at least initially. The key seems to be to sort the factors in the terms according to index regardless of letter d or n. – Mats Granvik Aug 05 '21 at 18:38