|
| 1 | +""" |
| 2 | +Project Euler Problem 587: https://projecteuler.net/problem=587 |
| 3 | +
|
| 4 | +A square is drawn around a circle as shown in the diagram below on the left. |
| 5 | +We shall call the blue shaded region the L-section. |
| 6 | +A line is drawn from the bottom left of the square to the top right |
| 7 | +as shown in the diagram on the right. |
| 8 | +We shall call the orange shaded region a concave triangle. |
| 9 | +
|
| 10 | +It should be clear that the concave triangle occupies exactly half of the L-section. |
| 11 | +
|
| 12 | +Two circles are placed next to each other horizontally, |
| 13 | +a rectangle is drawn around both circles, and |
| 14 | +a line is drawn from the bottom left to the top right as shown in the diagram below. |
| 15 | +
|
| 16 | +This time the concave triangle occupies approximately 36.46% of the L-section. |
| 17 | +
|
| 18 | +If n circles are placed next to each other horizontally, |
| 19 | +a rectangle is drawn around the n circles, and |
| 20 | +a line is drawn from the bottom left to the top right, |
| 21 | +then it can be shown that the least value of n |
| 22 | +for which the concave triangle occupies less than 10% of the L-section is n = 15. |
| 23 | +
|
| 24 | +What is the least value of n |
| 25 | +for which the concave triangle occupies less than 0.1% of the L-section? |
| 26 | +""" |
| 27 | + |
| 28 | +from itertools import count |
| 29 | +from math import asin, pi, sqrt |
| 30 | + |
| 31 | + |
| 32 | +def circle_bottom_arc_integral(point: float) -> float: |
| 33 | + """ |
| 34 | + Returns integral of circle bottom arc y = 1 / 2 - sqrt(1 / 4 - (x - 1 / 2) ^ 2) |
| 35 | +
|
| 36 | + >>> circle_bottom_arc_integral(0) |
| 37 | + 0.39269908169872414 |
| 38 | +
|
| 39 | + >>> circle_bottom_arc_integral(1 / 2) |
| 40 | + 0.44634954084936207 |
| 41 | +
|
| 42 | + >>> circle_bottom_arc_integral(1) |
| 43 | + 0.5 |
| 44 | + """ |
| 45 | + |
| 46 | + return ( |
| 47 | + (1 - 2 * point) * sqrt(point - point**2) + 2 * point + asin(sqrt(1 - point)) |
| 48 | + ) / 4 |
| 49 | + |
| 50 | + |
| 51 | +def concave_triangle_area(circles_number: int) -> float: |
| 52 | + """ |
| 53 | + Returns area of concave triangle |
| 54 | +
|
| 55 | + >>> concave_triangle_area(1) |
| 56 | + 0.026825229575318944 |
| 57 | +
|
| 58 | + >>> concave_triangle_area(2) |
| 59 | + 0.01956236140083944 |
| 60 | + """ |
| 61 | + |
| 62 | + intersection_y = (circles_number + 1 - sqrt(2 * circles_number)) / ( |
| 63 | + 2 * (circles_number**2 + 1) |
| 64 | + ) |
| 65 | + intersection_x = circles_number * intersection_y |
| 66 | + |
| 67 | + triangle_area = intersection_x * intersection_y / 2 |
| 68 | + concave_region_area = circle_bottom_arc_integral( |
| 69 | + 1 / 2 |
| 70 | + ) - circle_bottom_arc_integral(intersection_x) |
| 71 | + |
| 72 | + return triangle_area + concave_region_area |
| 73 | + |
| 74 | + |
| 75 | +def solution(fraction: float = 1 / 1000) -> int: |
| 76 | + """ |
| 77 | + Returns least value of n |
| 78 | + for which the concave triangle occupies less than fraction of the L-section |
| 79 | +
|
| 80 | + >>> solution(1 / 10) |
| 81 | + 15 |
| 82 | + """ |
| 83 | + |
| 84 | + l_section_area = (1 - pi / 4) / 4 |
| 85 | + |
| 86 | + for n in count(1): |
| 87 | + if concave_triangle_area(n) / l_section_area < fraction: |
| 88 | + return n |
| 89 | + |
| 90 | + return -1 |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + print(f"{solution() = }") |
0 commit comments