Determining the continued fractions of square roots 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 -&gt; [1; 2;]
3 -&gt; [1; 1, 2;]
19 -&gt; [4; 2, 1, 3, 1, 2, 8;]

Shortest code wins. Good luck!
GolfScript (66 60 chars)

~:^,{.*^&gt;}?(:?';'[1?{^1$.*-@/?@+.2$/@@1$%?\- 1$(}do;;]','*1$


Warning: most of the ? in there are the variable representing floor(sqrt(input)) rather than the builtin. But the first one is the builtin.

Takes input on stdin and outputs to stdout.

Psuedocode of the algorithm (proof of correctness currently left as an exercise for the reader):

n := input()
m := floor(sqrt(n))
output(m)
x := 1
y := m
do
  x := (n - y * y) / x
  output((m + y) / x)
  y := m - (m + y) % x
while (x &gt; 1)


Yet again I find myself wanting a single operator which takes a b on the stack and leaves a/b a%b on the stack.