-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddPeriodSquareRoots.java
More file actions
43 lines (37 loc) · 1.04 KB
/
OddPeriodSquareRoots.java
File metadata and controls
43 lines (37 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package dev;
public class OddPeriodSquareRoots {
public static void main(String[] args) {
int limit = 10000;
int result = countOddPeriods(limit);
System.out.println("The number of continued fractions for N <= " + limit + " with an odd period is: " + result);
}
public static int countOddPeriods(int limit) {
int count = 0;
for (int n = 2; n <= limit; n++) {
if (!isSquare(n)) {
if (getPeriod(n) % 2 == 1) {
count++;
}
}
}
return count;
}
private static boolean isSquare(int n) {
int sqrt = (int) Math.sqrt(n);
return sqrt * sqrt == n;
}
private static int getPeriod(int n) {
int period = 0;
int a0 = (int) Math.sqrt(n);
int m = 0;
int d = 1;
int a = a0;
do {
m = d * a - m;
d = (n - m * m) / d;
a = (a0 + m) / d;
period++;
} while (a != 2 * a0);
return period;
}
}