# Generating a list of specific intermediate numbers associated with continued fractions

First of all, I don't know how ContinuedFraction[] or Convergents[] are implemeneted, and I think it is immaterial to my question.

for some number $x$ there's an associated continued fraction representation $[a_1,...,a_i,...]$ (finite or not), and, for this matter, two other sequences $[x_1,...,x_i,...]$ and $[d_1,...,d_i,...]$ defined like this:

As we follow the implementation of some $a_i$ into the fraction, further action is due on some number $x_{i+1}=n_{i+1}+d_{i+1}$. It consists of setting $a_{i+2}=n_{i+1}$, and repeating the action on $x_{i+2}=1/d_{i+1}=n_{i+2}+d_{i+2}$.

I am interested in an (possibly) efficient way of extracting the $[d_1,...,d_i,...]$ for some number $x$, preferably using functional programming in Mathematica. Any clue\lead would be very much appreciated!

I know I didn't give any base code to work with which is annoying, and I don't really expect anyone to come forth and solve someone else's problem from start to end. Having said that, hopefully, comments regarding this annoyance will be spared..

-
Maybe you could explain why ContinuedFraction doesn't answer your question. – bill s May 5 '13 at 12:57
@ bill s , ContinuedFraction is a final result. It's process consists of rounding numbers (among other things), and performing actions on remainders, which are rounded again. Therefore, it hides all the numbers involved in the process. Some of these numbers, I think, are useful for understanding the relations between decimal representation (just as an example of an "ordinary" representation) and this representation. – Dror May 5 '13 at 13:05

If I understand the question correctly, the fractional parts or remainders of the continued fraction process are sought.

cfRemainders[x_?NumericQ, iter_: Hold[$IterationLimit]] := NestWhileList[FractionalPart[1/#] &, FractionalPart[x], # != 0 &, 1, ReleaseHold[iter]]  One can decide for oneself how best to limit the nesting when x is irrational. Examples: cfRemainders[43/24] (* {19/24, 5/19, 4/5, 1/4, 0} *) cfRemainders[N@Pi, 5] (* {0.141593, 0.0625133, 0.996594, 0.00341723, 0.634591, 0.575818} *) Block[{$IterationLimit = 20},
N[cfRemainders[Sqrt[2]], 6]
]
(* {0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214,
0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214,
0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214} *)


One also has to be careful about the numerical issues in this process:

Block[{$IterationLimit = 20}, N[cfRemainders[Sqrt[2]]] ] (* {0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414214, 0.414213, 0.414219, 0.414183, 0.414394, 0.413165, 0.420342, 0.379017, 0.638402} *)  If additionally one wants the reciprocals of the even terms, one might process the list as follows: evenReciprocate[list_List] := Power[list, -(-1)^Range@Length@list] evenReciprocate[N[cfRemainders[Sqrt[2], 10], 6]] (* {0.414214, 2.41421, 0.414214, 2.41421, 0.414214, 2.41421, 0.414214, 2.41421, 0.414214, 2.41421, 0.414214} *)  - That's what I was after, thanks!. Another nice addition would be replacing even elements with their reciprocal.. – Dror May 5 '13 at 13:19 What does mean part : Hold[$IterationLimit]]? mathematica.stackexchange.com/questions/24690/… – Zakharia Stanley May 5 '13 at 15:21
@ZakhariaStanley It means the default value of iter will be the value \$IterationLimit at the time the function is called. See this answer. – Michael E2 May 5 '13 at 16:10