-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem66.bean
50 lines (38 loc) · 916 Bytes
/
problem66.bean
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
// -*- Java -*- (Just makes things look nicer)
import java.math.BigInteger;
dio(s) {
a0 = Math.floor(Math.sqrt(s));
if (a0 * a0 == s)
return BigInteger.ZERO;
s = BigInteger.valueOf((long)s);
m = BigInteger.ZERO;
d = BigInteger.ONE;
a = BigInteger.valueOf((long)a0);
n1 = BigInteger.ONE;
n0 = a;
d1 = BigInteger.ZERO;
d0 = BigInteger.ONE;
while (true) {
if (n0.multiply(n0).subtract( s.multiply(d0).multiply(d0) ).equals(BigInteger.ONE))
return n0;
m = d.multiply(a).subtract(m);
d = s.subtract( m.multiply(m) ).divide(d);
a = BigInteger.valueOf((long)a0).add(m).divide(d);
n2 = n1;
n1 = n0;
d2 = d1;
d1 = d0;
n0 = a.multiply(n1).add(n2);
d0 = a.multiply(d1).add(d2);
}
}
maxd = null;
maxx = BigInteger.ZERO;
for (i = 1; i <= 1000; i++) {
x = dio(i);
if (x.compareTo(maxx) > 0) {
maxx = x;
maxd = i;
}
}
print(maxd);