Periodic Continued Fraction

Author

Eric W. Weisstein
August 27, 2008

This notebook downloaded from http://mathworld.wolfram.com/notebooks/NumberTheory/PeriodicContinuedFraction.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/PeriodicContinuedFraction.html.

©2008 Wolfram Research, Inc. except for portions noted otherwise

Code

PeriodicCF[c_?VectorQ]:=First[c]
PeriodicCF[c_]:=Module[{k,n=Length[Last[c]]},First[c]+ContinuedFractionK[Piecewise[MapIndexed[{#1,Mod[k-1,n]==#2[[1]]-1}&,Last[c]]],{k,∞}]
]

Notation

<<NumberTheory`ContinuedFractions`

ContinuedFractionForm[{2,{1,1,1,4}}]

FromContinuedFraction[{2,{1,1,1,4}}]

PeriodicCF[{2,{1,1,1,4}}]//InputForm

2+

2+{(1/1),(1/(1+(1/1))),(1/(1+(1/(1+(1/1))))),(1/(1+(1/(1+(1/(1+(1/4))))))),(1/(1+(1/(1+(1/(1+(1/(4+(1/1)))))))))}

N[%]^2

Table[2+ContinuedFractionK[Piecewise[
   {{1, Mod[-1 + k, 4] == 0 || Mod[-1 + k, 4] == 1 || Mod[-1 + k, 4] == 2}, 
{4, Mod[-1 + k, 4] == 3}}, 0], 
  {k, n}],{n,6}]

Benchmark

ContinuedFraction[Sqrt[19]]

4+ContinuedFractionK[Piecewise[{{2,Mod[-1+n,6]==0},{1,Mod[-1+n,6]==1},{3,Mod[-1+n,6]==2},{1,Mod[-1+n,6]==3},{2,Mod[-1+n,6]==4},{8,Mod[-1+n,6]==5}},0],{n,Infinity}]//Timing

Table

Table[Print[{n,c=ContinuedFraction[Sqrt[n]],PeriodicCF[c]//Simplify//Timing}],{n,30}];

Length/@Last/@ContinuedFraction/@Sqrt/@Select[Range[2,20],!IntegerQ[Sqrt[#]]&]

l=Length/@Last/@ContinuedFraction/@Sqrt/@Range[500];//Timing

ListPlot[MapIndexed[{#2[[1]],#1}&,l],Joined->True,PlotStyle->Red,
AxesLabel->TraditionalForm/@{n,P[Sqrt[n]]}]

Quadratic surds with given periods

m=First/@Split[Sort[MapIndexed[{#1,#2[[1]]}&,l]],First[#1]==First[#2]&]

f=First/@m

max=Complement[Range[Max[f]],f][[1]]

Last/@Take[m,max]

If[Length[#]>10,Take[#,10],#]&/@(Flatten[Position[l,#]]&/@Range[10])//ColumnForm

Periodic

[(OverBar[a])]

Table[FromContinuedFraction[{{a}}],{a,5}]

a+ContinuedFractionK[1,a,{n,∞}]//FullSimplify

a+((2(a-(Sqrt[4+(a^2)])))/(Expand[(a+(Sqrt[4+(a^2)]))(a-(Sqrt[4+(a^2)]))]))//Simplify

Table[(1/2) (a+(Sqrt[4+(a^2)])),{a,5}]//Simplify

[0; 1, (OverBar[a])]

Table[FromContinuedFraction[{0,1,{a}}],{a,5}]

Table[2/(a+Sqrt[a^2+4])-a+1,{a,5}]//FullSimplify

1+1/ContinuedFractionK[a,{n,∞}]//Simplify

[1, (OverBar[a])]

Table[FromContinuedFraction[{1,{a}}],{a,5}]

1+ContinuedFractionK[a,{n,∞}]//FullSimplify

1+((2(a-(Sqrt[4+(a^2)])))/(Expand[(a+(Sqrt[4+(a^2)]))(a-(Sqrt[4+(a^2)]))]))//FullSimplify

Table[(1/2) (2-a+(Sqrt[4+(a^2)])),{a,5}]//FunctionExpand//FullSimplify

[a, (OverBar[2a])]

a+ContinuedFractionK[2a,{n,∞}]//FullSimplify

Table[FromContinuedFraction[{a,{2a}}],{a,5}]

ContinuedFractionForm[{1,{2}}]

FromContinuedFraction[{3,{6}}]

[(OverBar[a, b])]

a+ContinuedFractionK[If[Mod[n,2]==1,b,a],{n,∞}]

FromContinuedFraction[{{a,b}}]

With[{a=2,b=5},
{a+ContinuedFractionK[If[Mod[n,2]==1,b,a],{n,30}],((2 a)/(-a b+Sqrt[a b(a b+4)])),FromContinuedFraction[{{a,b}}],a+(1/(b+(1/(a+(1/(b+(1/a)))))))}//N
]

With[{a=7,b=5},
{a+ContinuedFractionK[If[Mod[n,2]==1,b,a],{n,30}],((2 a)/(-a b+Sqrt[a b(a b+4)])),FromContinuedFraction[{{a,b}}],a+(1/(b+(1/(a+(1/(b+(1/a)))))))}//N
]