does anyone have a program for the period length of a continued fraction?
- To: mathgroup at smc.vnet.net
- Subject: [mg23216] does anyone have a program for the period length of a continued fraction?
- From: "Matthew Herman" <Henayni at hotmail.com>
- Date: Mon, 24 Apr 2000 01:12:23 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
I am working on a program for pell's equation, and I need this.
In the meantime, here is a program for the {a1,a2...an} list of a finite
or infinite continued fraction and another for the convergents. If
anyone has any comments or suggestions.. let me know. FullExtendedGCD is
taken from Stan Wagon's "Mathematica in Action".
contfrac[a_,b_]:=
If[IntegerQ[a =
b],Part[FullExtendedGCD[a,b],2],Floor[NestList[r,a,b-1]]]
r[x_] := 1/(x - Floor[x])
FullExtendedGCD[a_, b_] := FullExtendedGCD[a, b, {a}, {}]
FullExtendedGCD[a_, b_, r_, q_] := FullExtendedGCD[
b, Mod[a,b], Append[r, b], Append[q, Floor[a/b]]]
FullExtendedGCD[a_, 0, r_, q_] :=
Block[{s = {1, 0}, t = {0, 1}},
Scan[(AppendTo[t, t[[-2]] - t[[-1]] #];
AppendTo[s, s[[-2]] - s[[-1]] #]) &, q];
{r, q, s, t}]
(conv[a_, b_] :=
Table[p[a, b, k]/q[a, b, k], {k, 1, Length[contfrac[a, b]]}]
p[a_,b_,n_]:=Part[contfrac[a,b],n]p[a,b,n-1]+p[a,b,n-2]
p[a_,b_,2]:=Part[contfrac[a,b],1]*Part[contfrac[a,b],2]+1
p[a_,b_,1]:=Part[contfrac[a,b],1]
q[a_,b_,1]:=1
q[a_,b_,2]:=Part[contfrac[a,b],2]
q[a_,b_,n_]:=Part[contfrac[a,b],n]q[a,b,n-1]+q[a,b,n-2]
thanks,
matt