-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem192.du
116 lines (105 loc) · 2.69 KB
/
problem192.du
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import Math;
import BigInt;
const LIMIT = 1000000000000;
class FiniteFrac {
init(var seq) {
this.index = seq.len() - 1;
}
next() {
if (this.index >= 1) {
this.index -= 1;
return this.seq[this.index + 1];
} else {
return nil;
}
}
}
class SqrtConvergentState {
init(var n) {
this.a0 = Math.floor(Math.sqrt(n));
this.a = this.a0;
this.r = 0;
this.s = 1;
}
next() {
var lastA = this.a;
this.r = this.a * this.s - this.r;
this.s = Math.floor((this.n - this.r * this.r) / this.s);
this.a = Math.floor((this.a0 + this.r) / this.s);
return lastA;
}
}
def cmp(a, b) {
var mult = 1;
while {
var aTerm = a.next();
var bTerm = b.next();
if (aTerm == nil) {
return mult;
}
if (bTerm == nil) {
return - mult;
}
if (aTerm != bTerm) {
return mult * (aTerm - bTerm);
}
mult *= -1;
}
}
def realize(convergent) {
var numer = convergent[-1];
var denom = 1;
for (var i = convergent.len() - 2; i >= 0; i -= 1) {
var curr = convergent[i];
var newNumer = curr * numer + denom;
var newDenom = numer;
// Note: No need to divide by gcd here. If numer and
// denom are coprime, then numer and (denom + X * numer)
// are also coprime.
numer = newNumer;
denom = newDenom;
}
return denom;
}
def generateLastConvergent(n) {
var convergent = [];
var state = SqrtConvergentState(n);
convergent.push(state.a);
while (realize(convergent) <= LIMIT) {
state.next();
convergent.push(state.a);
}
return [convergent, state];
}
def findBest(n) {
var tmp = generateLastConvergent(n);
var convergent = tmp[0];
var state = tmp[1];
var originalA = convergent[-1];
while (2 * convergent[-1] >= originalA) {
var value = realize(convergent);
if (2 * convergent[-1] == originalA) {
// Corner case for even convergents.
convergent[-1] = originalA;
var lhs = FiniteFrac(convergent);
if ((cmp(lhs, state) > 0) and (value <= LIMIT)) {
return value;
}
break;
}
if (value <= LIMIT) {
return value;
}
convergent[-1] -= 1;
}
convergent.pop();
return realize(convergent);
}
var sum = BigInt.new().unwrap();
for (var n = 2; n <= 100000; n += 1) {
var a0 = Math.floor(Math.sqrt(n));
if (a0 * a0 != n) {
sum = sum.add(BigInt.new(findBest(n).toString()).unwrap()).unwrap();
}
}
print(sum);