# How can we generate this more general form of the continued fraction? [duplicate]

How can I write Mathematica code for this continued fraction with alternating terms?

How can we generate this more general form of the above-continued fraction?

$$x+\cfrac{a}{y+\cfrac{a^2}{x+\cfrac{a^3}{y+\cfrac{a^4}{x+\cfrac{a^5}{y+\cfrac{a^6}{x+\cdots}}}}}}$$

• Yes thank you so much for your help. May 16 '20 at 20:26

Once again, you can use ContinuedFractionK[]:

With[{n = 7},
x + ContinuedFractionK[a^k, Piecewise[{{y, Mod[k, 2] == 1}}, x], {k, 1, n}]]


$$x+\cfrac{a}{y+\cfrac{a^2}{x+\cfrac{a^3}{y+\cfrac{a^4}{x+\cfrac{a^5}{y+\cfrac{a^6}{x+\cfrac{a^7}{y}}}}}}}$$

z = {x, y};
i = 1;
n = 6;
Style[x + Nest[a^(n - i++)/(z[[Mod[i, 2, 1]]] + #) &, …, n - 1], 32,
ScriptSizeMultipliers -> 1]


• Thank you very much. May 16 '20 at 20:27