-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy path10023.java
61 lines (52 loc) · 1.47 KB
/
10023.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;
import java.math.*;
class Main {
public static void main(String[] args) {
(new UVa10023()).solve();
}
}
class UVa10023 {
final private Scanner sc = new Scanner(System.in);
public void solve() {
int n;
String input;
n = sc.nextInt();
while (n > 0) {
n--;
BigInteger num = sc.nextBigInteger();
System.out.println(SqRtN(num));
if (n > 0)
System.out.println();
}
}
/*
* Newton-Raphson
*
* stolen from the internetz
*/
BigInteger SqRtN(BigInteger N)
{
BigInteger G = new BigInteger((N.shiftRight((N.bitLength() + 1) / 2)).toString());
BigInteger LastG = null;
BigInteger One = new BigInteger("1");
if (N.compareTo(One) == 0)
return One;
while (true)
{
LastG = G;
G = N.divide(G).add(G).shiftRight(1);
int i = G.compareTo(LastG);
if (i == 0) return G;
if (i < 0)
{
if (LastG.subtract(G).compareTo(One) == 0)
if (G.multiply(G).compareTo(N) < 0 && LastG.multiply(LastG).compareTo(N) > 0) return G;
}
else
{
if (G.subtract(LastG).compareTo(One) == 0)
if (LastG.multiply(LastG).compareTo(N) < 0 && G.multiply(G).compareTo(N) > 0) return LastG;
}
}
}
}