Skip to content

Commit f8dafad

Browse files
committed
P587: Added explanation and slightly simplified code in all language solutions.
1 parent 02e6b50 commit f8dafad

File tree

3 files changed

+206
-8
lines changed

3 files changed

+206
-8
lines changed

java/p587.java

+69-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,73 @@ public static void main(String[] args) {
1414
}
1515

1616

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+
*/
1784
public String run() {
1885
double lSectionArea = 1 - Math.PI / 4;
1986
for (int i = 1; ; i++) {
@@ -22,8 +89,7 @@ public String run() {
2289
double b = -2 * (slope + 1);
2390
double c = 1;
2491
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));
2793
if (concaveTriangleArea / lSectionArea < 0.001)
2894
return Integer.toString(i);
2995
if (i == Integer.MAX_VALUE)
@@ -35,7 +101,7 @@ public String run() {
35101
// The indefinite integral of (1 - sqrt(2x - x^2)) dx.
36102
private static double integral(double x) {
37103
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;
39105
}
40106

41107
}

mathematica/p587.mathematica

+70-2
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,87 @@
77
*)
88

99

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

1280
(* The indefinite integral of (1 - sqrt(2x - x^2)) dx. *)
1381
TheIntegral[x_] := Block[{t = x - 1},
14-
t - (Sqrt[1 - t^2] * t + ArcSin[t]) / 2]
82+
t - (Sqrt[x * (2 - x)] * t + ArcSin[t]) / 2]
1583

1684
ConcaveTriangleArea[i_] := Block[{
1785
slope = 1 / i,
1886
a = slope^2 + 1,
1987
b = -2 * (slope + 1),
2088
c = 1,
2189
x = (2 * c) / (-b + Sqrt[b^2 - 4 * a * c])},
22-
x * (1 - Sqrt[(-x + 2) * x]) / 2 + TheIntegral[1] - TheIntegral[x]]
90+
(x^2 * slope / 2) + (TheIntegral[1] - TheIntegral[x])]
2391

2492
i = 1;
2593
While[ConcaveTriangleArea[i] / LSectionArea >= 1 / 1000,

python/p587.py

+67-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,76 @@
99
import itertools, math
1010

1111

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

1883
lsectionarea = 1.0 - math.pi / 4.0
1984
for i in itertools.count(1):
@@ -22,8 +87,7 @@ def integral(x):
2287
b = -2.0 * (slope + 1.0)
2388
c = 1.0
2489
x = (2.0 * c) / (-b + math.sqrt(b * b - 4 * a * c))
25-
concavetrianglearea = x * (1.0 - math.sqrt((-x + 2.0) * x)) / 2.0
26-
concavetrianglearea += integral(1.0) - integral(x)
90+
concavetrianglearea = (x**2 * slope / 2) + (integral(1.0) - integral(x))
2791
if concavetrianglearea / lsectionarea < 0.001:
2892
return str(i)
2993

0 commit comments

Comments
 (0)