-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSquareRootConvergents.java
44 lines (39 loc) · 1.3 KB
/
SquareRootConvergents.java
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
44
import java.math.BigInteger;
import java.util.Scanner;
public class Problem57 {
public static void main(String[] args) {
System.out.println("Enter");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
getAnswer(n);
}
private static void getAnswer(int n) {
BigInteger numerator = BigInteger.valueOf(3);
BigInteger denominator = BigInteger.valueOf(2);
for (int i = 0; i < n; i++) {
// if (numerator.toString().length() > denominator.toString().length()) {
if (log10(numerator) > log10(denominator)) {
System.out.println((i + 1));
}
BigInteger newDenominator = numerator.add(denominator);
BigInteger newNumerator = newDenominator.add(denominator);
numerator = newNumerator;
denominator = newDenominator;
}
}
private static int log10(BigInteger huge) {
int digits = 0;
int bits = huge.bitLength();
while (bits > 4) {
int reduce = bits / 4;
huge = huge.divide(BigInteger.TEN.pow(reduce));
digits += reduce;
bits = huge.bitLength();
}
if (huge.intValue() > 9) {
digits += 1;
}
return digits;
}
}