Please see my posting in Stackexchange. Infinite precision. Java BigInteger.
I used a Surd Storage type for infinite precision of the square root of n.
$${\frac{b \sqrt {n} + d}{c} = } $$
$$\frac {b c \sqrt {n} - c d + a_i c^2}{b^2 n - d^2 - (a_i c)^2 + 2 a_i c d} $$
The floor value of the sqrt(n) is only used once. After which the remaining iterations are stored as a surd type. This avoids the rounding errors seen in other algorithms and infinite (memory constrained) resolution can be achieved.
$$a_0 = floor \sqrt{n}$$
$$ a_i = \frac{b_i a_0 + d_i}{c_i} $$
$$ b_{i+1} = b_i c$$
$$ c_{i+1} = b_i^2 n - d_i^2 - (a_i c_i)^2 + 2 a_i c_i d_i $$
$$ d_{i+1} = a_i * (c_i)^2 - c_i * d_i $$
$$ g = gcd(b_{i+1} , c_{i+1} , d_{i+1})$$
$$ b_{i+1}=\frac{b_{i+1}}{g} , c_{i+1}=\frac{c_{i+1}}{g} , d_{i+1}=\frac{d_{i+1}}{g} $$
$$ a_{i+1} = \frac{b_{i+1} a_0 + d_{i+1}}{c_{i+1}} $$
Then for i=0 to i=Maximum_terms, a continued fraction is produced beginning with $$ [a_0;a_1,a_2 ... ,2a_0] $$
I terminate the fraction when the $$a_i = 2 a_0$$ This is the point at which the sequence repeats.
The mathematics was done by Electro World and a very nice video on the mathematics can be found here https://youtu.be/GFJsU9QsytM
A boolean true is returned if a repeat series is found and false if a repeat sequence is not found for the desired precision.
Sample Outputs:
$$ \sqrt{139}=[11;1,3,1,3,7,1,1,2,11,2,1,1,7,3,1,3,1,22] $$ repeat length 18
$$ \sqrt{15}= [3;1,6]$$ repeat length 2
$$ \sqrt{2501}=[50;100]$$ repeat length 1
$$ \sqrt{10807}=[103;1,22,9,2,2,5,4,1,1,1,6,15,1,5,2,1,3,6,34,2,34,6,3,1,2,5,1,15,6,1,1,1,4,5,2,2,9,22,1,206]$$ repeat length 40
A possible two fold speed up would be to look at the palindromic nature of series. In this case 34,2,34. Only half the sequence would need to be determined.
Source Code in Java for BigInteger. Variable precision set by Maximum_terms.
public static Boolean SquareRootConFrac(BigInteger N) {
BigInteger A,B=BigInteger.ONE,C=B,D=BigInteger.ZERO;
BigInteger A0=N.sqrt(),Bi=B,Ci=C,Di=D,G;
BigInteger TwoA0 = BigInteger.TWO.multiply(A0);
int Frac_Length=0, Maximum_terms=10000; //Precision 10000 terms
String str="";
Boolean Repeat=false, Success=false, Initial_BCD=true;
while(!Repeat) {
Frac_Length++; Success=!(Frac_Length==Maximum_terms);
A=((B.multiply(A0)).add(D)).divide(C); Repeat=A.equals(TwoA0)||!Success;`
Bi=B.multiply(C);
Ci=(B.multiply(B).multiply(N)).subtract(D.multiply(D)).subtract(A.multiply(A).multiply(C).multiply(C)).add(BigInteger.TWO.multiply(A).multiply(C).multiply(D));
Di=(A.multiply(C).multiply(C)).subtract(C.multiply(D));
G=Bi.gcd(Ci).gcd(Di);
B=Bi.divide(G);C=Ci.divide(G);D=Di.divide(G);
if(Initial_BCD) {str="["+A+";";System.out.print(str);Initial_BCD=false;}
else {str=""+A;System.out.print(str);if(!Repeat){str=",";System.out.print(str);}}
}
str="]";System.out.println(str);
str="repeat length ";System.out.print(str);
if(Success) {str=""+(Frac_Length-1);System.out.println(str);}
else {str="not found";System.out.println(str);}
return Success;
}