2
$\begingroup$

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..

$\endgroup$
2
  • $\begingroup$ Maybe you could explain why ContinuedFraction doesn't answer your question. $\endgroup$
    – bill s
    May 5, 2013 at 12:57
  • $\begingroup$ @ 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. $\endgroup$
    – user76568
    May 5, 2013 at 13:05

1 Answer 1

2
$\begingroup$

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} *)
$\endgroup$
3
  • $\begingroup$ That's what I was after, thanks!. Another nice addition would be replacing even elements with their reciprocal.. $\endgroup$
    – user76568
    May 5, 2013 at 13:19
  • $\begingroup$ What does mean part : Hold[$IterationLimit]]? mathematica.stackexchange.com/questions/24690/… $\endgroup$ May 5, 2013 at 15:21
  • 1
    $\begingroup$ @ZakhariaStanley It means the default value of iter will be the value $IterationLimit at the time the function is called. See this answer. $\endgroup$
    – Michael E2
    May 5, 2013 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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