@@ -14,6 +14,73 @@ public static void main(String[] args) {
14
14
}
15
15
16
16
17
+ /*
18
+ * Start by defining the coordinate system in a convenient way. The position and scale of the diagram don't
19
+ * matter because we only care about the ratio of areas, not the absolute areas. So, let the bottom left
20
+ * of the diagram be the origin (x = 0, y = 0), and let each circle to have a radius of 1.
21
+ *
22
+ * The leftmost circle is centered at (1, 1), and its equation is (x - 1)^2 + (y - 1)^2 = 1.
23
+ * The diagonal line has slope = s = 1 / n (for any positive n), and the line's equation is y = s * x.
24
+ * From basic geometry, the area of the blue L-section is 1 - pi / 4.
25
+ *
26
+ * Let's find the x-coordinate where the diagonal line intersects the first circle.
27
+ * Take the equation of the circle and substitute y = s * x for the line:
28
+ *
29
+ * (x - 1)^2 + (s*x - 1)^2 = 1.
30
+ * (x^2 - 2x + 1) + (s^2 x^2 - 2s*x + 1) = 1.
31
+ * (1 + s^2)x^2 + (-2 - 2s)x + 1 = 0.
32
+ *
33
+ * We can apply the quadratic formula with a = 1 + s^2, b = -2 - 2s, c = 1. There are two solutions for x,
34
+ * and we only want the smaller value. Thus, let X = (-b - sqrt(b^2 - 4ac)) / (2a). Or equivalently
35
+ * with more numerical stability (using the Citardauq formula), X = (2c) / (-b + sqrt(b^2 - 4ac)).
36
+ *
37
+ * The orange concave triangle can be divided into two parts by a vertical line:
38
+ *
39
+ * - The left part is a proper triangle, whose area is easily seen as x * y / 2 = X^2 * s / 2.
40
+ *
41
+ * - The right part is the region between the circle and the baseline. Let's re-express
42
+ * the circle's equation in terms of y, and only keep the lower semicircle:
43
+ *
44
+ * (x - 1)^2 + (y - 1)^2 = 1.
45
+ * (y - 1)^2 = 1 - (x - 1)^2.
46
+ * y - 1 = -sqrt(1 - (x - 1)^2).
47
+ * y = 1 - sqrt(1 - (x - 1)^2).
48
+ * y = 1 - sqrt(1 - (x^2 - 2x + 1)).
49
+ * y = 1 - sqrt(2x - x^2).
50
+ *
51
+ * Now, the indefinite integral of f(x) = 1 - sqrt(2x - x^2) with respect to x
52
+ * is F(x) = (x - 1) - [sqrt(2x - x^2) * (x - 1) + asin(x - 1)] / 2.
53
+ * Finding this integral is not obvious, but verifying it is a fairly straightforward
54
+ * mechanical procedure involving differentiation and simplification.
55
+ *
56
+ * The area of the right part is the integral of f(x) for x from X to 1, because the start is
57
+ * the x-coordinate where line meets the circle, and the end is where the circle meets the baseline.
58
+ * Hence the area is equal to F(1) - F(X).
59
+ *
60
+ * All in all, for any given n, the area of the orange concave triangle is X^2 * s / 2 + F(1) - F(X).
61
+ * The rest of the algorithm is a brute-force search with n = 1, 2, 3, ... until the ratio condition is met.
62
+ *
63
+ * Additional notes:
64
+ * - Intuitively, as n increases and the slope gets smaller, the area of the orange concave triangle should strictly
65
+ * decrease. This statement is in fact true, but proving it involves a big pile of differentiation and algebra.
66
+ * 0. We need to show that X (which is the x-coordinate of the line-circle intersection) increases with n.
67
+ * We'd differentiate X with respect to n, and get an expression that is always positive for any positive n.
68
+ * 1. Because X increases with n, the area of the right part, with its always-positive integrand, must decrease.
69
+ * 2. As for the left part, we'd differentiate X^2 * s / 2 with respect to n, and get a huge messy formula.
70
+ * It turns out this formula is negative for all n > 1. Hence the area of this triangle also decreases with n.
71
+ * After we prove that increasing n leads to decreasing orange area, we could use
72
+ * binary search to find the minimum value of n needed to meet the ratio requirement.
73
+ * - The use of floating-point arithmetic, for basic arithmetic operations (+ - * /) and irrational functions (sqrt,
74
+ * asin) alike, is inherently difficult or impossible to prove the correctness of. Furthermore, the algorithms
75
+ * for irrational functions are hard to understand and beyond the scope of this problem, and the error bounds for
76
+ * all operations are difficult to reason about.
77
+ * It should be possible to solve this particular problem using only integer arithmetic in a provably correct way.
78
+ * The basic idea would be to round the result of each operation both down and up to an integer fraction,
79
+ * keep track of pessimistic intervals that are guaranteed to contain the true value, accept a comparison only
80
+ * if the intervals don't overlap, and recompute everything at a higher precision if a comparison is inconclusive.
81
+ * Note: Because it doesn't seem easy to compute pi and asin(), it might be better to
82
+ * approximate integrals directly using the Darboux definition of lower and upper sums.
83
+ */
17
84
public String run () {
18
85
double lSectionArea = 1 - Math .PI / 4 ;
19
86
for (int i = 1 ; ; i ++) {
@@ -22,8 +89,7 @@ public String run() {
22
89
double b = -2 * (slope + 1 );
23
90
double c = 1 ;
24
91
double x = (2 * c ) / (-b + Math .sqrt (b * b - 4 * a * c ));
25
- double concaveTriangleArea = x * (1 - Math .sqrt ((-x + 2 ) * x )) / 2 ;
26
- concaveTriangleArea += integral (1 ) - integral (x );
92
+ double concaveTriangleArea = (x * x * slope / 2 ) + (integral (1 ) - integral (x ));
27
93
if (concaveTriangleArea / lSectionArea < 0.001 )
28
94
return Integer .toString (i );
29
95
if (i == Integer .MAX_VALUE )
@@ -35,7 +101,7 @@ public String run() {
35
101
// The indefinite integral of (1 - sqrt(2x - x^2)) dx.
36
102
private static double integral (double x ) {
37
103
double t = x - 1 ;
38
- return t - (Math .sqrt (1 - t * t ) * t + Math .asin (t )) / 2 ;
104
+ return t - (Math .sqrt (x * ( 2 - x ) ) * t + Math .asin (t )) / 2 ;
39
105
}
40
106
41
107
}
0 commit comments