13
2
The continued fraction of a number n
is a fraction of the following form:
which converges to n
.
The sequence a
in a continued fraction is typically written as: [a0; a1, a2, a3, ... an].
We will write ours in the same fashion, but with the repeating part between semicolons.
Your goal is to return the continued fraction of the square root of n
.
Input: An integer, n
. n
will never be a perfect square.
Output: The continued fraction of sqrt(n)
.
Test Cases:
2 -> [1; 2;]
3 -> [1; 1, 2;]
19 -> [4; 2, 1, 3, 1, 2, 8;]
Shortest code wins. Good luck!
1Does the output have to be in the same format as the test cases? – grc – 2012-06-18T07:36:34.727
No. As long as you have the semicolons, it's fine. – beary605 – 2012-06-18T20:30:07.513
Hm, getting the right answers, having trouble knowing when the fraction is rational to stop. Is it really as simple as when a<sub>0</sub> is double the sqrt of the original input? – JoeFish – 2012-06-21T18:28:50.460
Yep, that's the limit. – beary605 – 2012-06-22T05:27:50.977
@beary605 thanks. Been doing a lot more reading, and now I see that the continued fraction of a square root is a bit of a special case. Fascinating stuff! Still working on a non-floating point version. – JoeFish – 2012-06-22T12:35:17.750