Problem Statement
Any irrational number greater than 1 can be written as
Q[0] + 1 ------------------------ Q[1] + 1 ----------------- Q[2] + 1 ---------- Q[3] + 1 --- ...for some infinite sequence Q[0],Q[1],Q[2],Q[3],... where each Q[i] is a positive integer. This method of writing a number is called a continued fraction, and each Q[i] is called a partial quotient. A remarkable fact from number theory is that, whenever the square root of a non-square positive integer is written in this form, the sequence of partial quotients is periodic. In other words, there exists some number K such that Q[J+K] = Q[J] for all J > 0. Notice that the repeating pattern begins at Q[1]; the initial partial quotient, Q[0], is not involved in the repetition.
We will represent such a periodic continued fraction as the finite sequence {Q[0],Q[1],...,Q[K]}, where K is the size of the shortest repeating pattern. For example, the square root of 2 has partial quotients 1,2,2,2,... and would be written as {1,2}. Similarly, the square root of 8 has partial quotients 2,1,4,1,4,... and would be written as {2,1,4}. Your task is to take a non-square positive integer n, and calculate its representation as a periodic continued fraction. Several sample calculations will be presented from which you should be able to generalize an algorithm. Although you are not required to follow the same algorithm as the sample calculations, it is highly suggested that you do so. Note that, for the purposes of this problem, the periodic continued fraction will always contain fewer than 100 elements.
Here is a sample calculation showing that the square root of 2 is {1,2}:
Step 0: Calculate Q[0] from sqrt(2). sqrt(2) = 1 + sqrt(2)-1 --------- 1 Q[0] is 1. The remainder is (sqrt(2)-1)/1. Step 1: Calculate Q[1] from (sqrt(2)-1)/1. sqrt(2)-1 1 1 1 1 --------- = ------------- = --------------------- = ------------- = ------------- 1 1 1 sqrt(2)+1 sqrt(2)+1 2 + sqrt(2)-1 --------- --------- * --------- --------- --------- sqrt(2)-1 sqrt(2)-1 sqrt(2)+1 1 1 Q[1] is 2. The remainder is (sqrt(2)-1)/1. Step 2: Calculate Q[2] from (sqrt(2)-1)/1. (Same as Step 1!)
Substituting the result of Step 1 into the result of Step 0, we get
sqrt(2) = 1 + 1 ------- 2 + ...and at this point the pattern repeats, so the square root of 2 is {1,2}.
Notice that, at every step, the partial quotient is chosen to force the remainder between 0 and 1 (exclusive), and that there is only one way to do this. Also notice that, in Step 1, we made essential use of the identity (A-B)*(A+B) = A*A - B*B.
Here is a more elaborate example, showing that the square root of 41 is {6,2,2,12}:
Step 0: Calculate Q[0] from sqrt(41). sqrt(41) = 6 + sqrt(41)-6 ---------- 1 Q[0] is 6. The remainder is (sqrt(41)-6)/1. Step 1: Calculate Q[1] from (sqrt(41)-6)/1. sqrt(41)-6 1 1 1 1 ---------- = -------------- = ----------------------- = -------------- = -------------- 1 1 1 sqrt(41)+6 sqrt(41)+6 2 + sqrt(41)-4 ---------- ---------- * ---------- ---------- ---------- sqrt(41)-6 sqrt(41)-6 sqrt(41)+6 5 5 Q[1] is 2. The remainder is (sqrt(41)-4)/5. Step 2: Calculate Q[2] from (sqrt(41)-4)/5. sqrt(41)-4 1 1 1 1 ---------- = -------------- = ----------------------- = -------------- = -------------- 5 5 5 sqrt(41)+4 sqrt(41)+4 2 + sqrt(41)-6 ---------- ---------- * ---------- ---------- ---------- sqrt(41)-4 sqrt(41)-4 sqrt(41)+4 5 5 Q[2] is 2. The remainder is (sqrt(41)-6)/5. Step 3: Calculate Q[3] from (sqrt(41)-6)/5. sqrt(41)-6 1 1 1 1 ---------- = -------------- = ----------------------- = -------------- = --------------- 5 5 5 sqrt(41)+6 sqrt(41)+6 12 + sqrt(41)-6 ---------- ---------- * ---------- ---------- ---------- sqrt(41)-6 sqrt(41)-6 sqrt(41)+6 1 1 Q[3] is 12. The remainder is (sqrt(41)-6)/1. Step 4: Calculate Q[4] from (sqrt(41)-6)/1. (Same as Step 1!)
Substituting the result of each step into the result of the previous step, we get
sqrt(41) = 6 + 1 ---------------- 2 + 1 ------------ 2 + 1 -------- 12 + ...and at this point the pattern repeats, so the square root of 41 is {6,2,2,12}.
Notice that the remainders are always of the form (sqrt(n)-d)/m, for some integers d and m. It might sometimes appear that the remainder has the form c*(sqrt(n)-d)/m, but it will always be possible to eliminate the c.
Definition
- Class:
- ContinuedFractions
- Method:
- squareRoot
- Parameters:
- int
- Returns:
- int[]
- Method signature:
- int[] squareRoot(int n)
- (be sure your method is public)
Notes
- Because we have restricted each partial quotient to be a positive integer, each square root has a unique representation as a periodic continued fraction.
Constraints
- n is between 2 and 1000, inclusive.
- n is not a square number. For example, n is not 4, 9, or 16.
Examples
2
Returns: { 1, 2 }
The first example above.
41
Returns: { 6, 2, 2, 12 }
The second example above.
63
Returns: { 7, 1, 14 }
158
Returns: { 12, 1, 1, 3, 12, 3, 1, 1, 24 }
919
Returns: { 30, 3, 5, 1, 2, 1, 2, 1, 1, 1, 2, 3, 1, 19, 2, 3, 1, 1, 4, 9, 1, 7, 1, 3, 6, 2, 11, 1, 1, 1, 29, 1, 1, 1, 11, 2, 6, 3, 1, 7, 1, 9, 4, 1, 1, 3, 2, 19, 1, 3, 2, 1, 1, 1, 2, 1, 2, 1, 5, 3, 60 }
3
Returns: { 1, 1, 2 }
5
Returns: { 2, 4 }
6
Returns: { 2, 2, 4 }
7
Returns: { 2, 1, 1, 1, 4 }
8
Returns: { 2, 1, 4 }
10
Returns: { 3, 6 }
11
Returns: { 3, 3, 6 }
12
Returns: { 3, 2, 6 }
13
Returns: { 3, 1, 1, 1, 1, 6 }
14
Returns: { 3, 1, 2, 1, 6 }
15
Returns: { 3, 1, 6 }
17
Returns: { 4, 8 }
18
Returns: { 4, 4, 8 }
19
Returns: { 4, 2, 1, 3, 1, 2, 8 }
20
Returns: { 4, 2, 8 }
1000
Returns: { 31, 1, 1, 1, 1, 1, 6, 2, 2, 15, 2, 2, 6, 1, 1, 1, 1, 1, 62 }
512
Returns: { 22, 1, 1, 1, 2, 6, 11, 6, 2, 1, 1, 1, 44 }
343
Returns: { 18, 1, 1, 11, 1, 5, 3, 1, 17, 1, 3, 5, 1, 11, 1, 1, 36 }
519
Returns: { 22, 1, 3, 1, 1, 2, 1, 2, 3, 7, 3, 2, 1, 2, 1, 1, 3, 1, 44 }
513
Returns: { 22, 1, 1, 1, 5, 1, 4, 5, 2, 5, 4, 1, 5, 1, 1, 1, 44 }
511
Returns: { 22, 1, 1, 1, 1, 6, 1, 14, 4, 1, 21, 1, 4, 14, 1, 6, 1, 1, 1, 1, 44 }
997
Returns: { 31, 1, 1, 2, 1, 4, 1, 1, 4, 1, 2, 1, 1, 62 }
716
Returns: { 26, 1, 3, 7, 2, 1, 1, 6, 10, 1, 1, 4, 2, 1, 12, 1, 2, 4, 1, 1, 10, 6, 1, 1, 2, 7, 3, 1, 52 }
434
Returns: { 20, 1, 4, 1, 40 }
396
Returns: { 19, 1, 8, 1, 38 }
212
Returns: { 14, 1, 1, 3, 1, 1, 1, 6, 1, 1, 1, 3, 1, 1, 28 }
108
Returns: { 10, 2, 1, 1, 4, 1, 1, 2, 20 }
76
Returns: { 8, 1, 2, 1, 1, 5, 4, 5, 1, 1, 2, 1, 16 }
499
Returns: { 22, 2, 1, 21, 1, 2, 44 }
777
Returns: { 27, 1, 6, 1, 54 }
248
Returns: { 15, 1, 2, 1, 30 }
998
Returns: { 31, 1, 1, 2, 4, 8, 1, 3, 1, 30, 1, 3, 1, 8, 4, 2, 1, 1, 62 }
937
Returns: { 30, 1, 1, 1, 1, 3, 4, 2, 3, 6, 1, 1, 19, 1, 6, 1, 2, 2, 1, 6, 1, 19, 1, 1, 6, 3, 2, 4, 3, 1, 1, 1, 1, 60 }
991
Returns: { 31, 2, 12, 10, 2, 2, 2, 1, 1, 2, 6, 1, 1, 1, 1, 3, 1, 8, 4, 1, 2, 1, 2, 3, 1, 4, 1, 20, 6, 4, 31, 4, 6, 20, 1, 4, 1, 3, 2, 1, 2, 1, 4, 8, 1, 3, 1, 1, 1, 1, 6, 2, 1, 1, 2, 2, 2, 10, 12, 2, 62 }