-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem198.ceylon
39 lines (32 loc) · 1.21 KB
/
problem198.ceylon
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
class Fraction(shared Integer numer, shared Integer denom) {}
class SearchPoint(shared Fraction lower, shared Fraction upper) {}
Integer denominatorBound = 100000000;
Integer rangeBound = 100;
shared void run() {
variable Integer ambiguousNumbers = 0;
Array<SearchPoint?> frontier = Array<SearchPoint?>.ofSize(20001, null);
variable Integer frontierIndex = 0;
frontier[0] = SearchPoint(Fraction(0, 1), Fraction(1, 50));
while (frontierIndex >= 0) {
SearchPoint? next = frontier[frontierIndex];
assert (exists next);
frontierIndex--;
Fraction avg = Fraction(
next.lower.numer * next.upper.denom + next.upper.numer * next.lower.denom,
next.lower.denom * next.upper.denom * 2
);
if (avg.denom > denominatorBound) {
continue;
}
if (rangeBound * avg.numer < avg.denom) {
ambiguousNumbers++;
}
if (next.lower.denom + next.upper.denom <= denominatorBound) {
Fraction mid = Fraction(next.lower.numer + next.upper.numer, next.lower.denom + next.upper.denom);
frontier[frontierIndex + 1] = SearchPoint(next.lower, mid);
frontier[frontierIndex + 2] = SearchPoint(mid, next.upper);
frontierIndex += 2;
}
}
print(ambiguousNumbers);
}