From 9651b1f766c9d0f77dd78b3088b697dda6e31a2b Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Tue, 26 Jul 2022 02:22:46 +0300 Subject: [PATCH 1/5] Add solution --- project_euler/problem_587/__init__.py | 0 project_euler/problem_587/sol1.py | 88 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 project_euler/problem_587/__init__.py create mode 100644 project_euler/problem_587/sol1.py diff --git a/project_euler/problem_587/__init__.py b/project_euler/problem_587/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_587/sol1.py b/project_euler/problem_587/sol1.py new file mode 100644 index 000000000000..242d02e2450f --- /dev/null +++ b/project_euler/problem_587/sol1.py @@ -0,0 +1,88 @@ +""" +Project Euler Problem 587: https://projecteuler.net/problem=587 + +A square is drawn around a circle as shown in the diagram below on the left. +We shall call the blue shaded region the L-section. +A line is drawn from the bottom left of the square to the top right +as shown in the diagram on the right. +We shall call the orange shaded region a concave triangle. + +It should be clear that the concave triangle occupies exactly half of the L-section. + +Two circles are placed next to each other horizontally, +a rectangle is drawn around both circles, and +a line is drawn from the bottom left to the top right as shown in the diagram below. + +This time the concave triangle occupies approximately 36.46% of the L-section. + +If n circles are placed next to each other horizontally, +a rectangle is drawn around the n circles, and +a line is drawn from the bottom left to the top right, +then it can be shown that the least value of n +for which the concave triangle occupies less than 10% of the L-section is n = 15. + +What is the least value of n +for which the concave triangle occupies less than 0.1% of the L-section? +""" + +from itertools import count +from math import asin, pi, sqrt + + +def circle_bottom_arc_integral(x: float) -> float: + """ + Returns integral of circle bottom arc y = 1 / 2 - sqrt(1 / 4 - (x - 1 / 2) ^ 2) + + >>> circle_bottom_arc_integral(0) + 0.39269908169872414 + + >>> circle_bottom_arc_integral(1 / 2) + 0.44634954084936207 + + >>> circle_bottom_arc_integral(1) + 0.5 + """ + + return ((1 - 2 * x) * sqrt(x - x**2) + 2 * x + asin(sqrt(1 - x))) / 4 + + +def concave_triangle_area(n: int) -> float: + """ + Returns area of concave triangle + + >>> concave_triangle_area(1) + 0.026825229575318944 + + >>> concave_triangle_area(2) + 0.01956236140083944 + """ + + intersection_y = (n + 1 - sqrt(2 * n)) / (2 * (n**2 + 1)) + intersection_x = n * intersection_y + + triangle_area = intersection_x * intersection_y / 2 + concave_region_area = circle_bottom_arc_integral( + 1 / 2 + ) - circle_bottom_arc_integral(intersection_x) + + return triangle_area + concave_region_area + + +def solution(fraction: float = 1 / 1000) -> int: + """ + Returns least value of n + for which the concave triangle occupies less than fraction of the L-section + + >>> solution(1 / 10) + 15 + """ + + L_section_area = (1 - pi / 4) / 4 + + for n in count(1): + if concave_triangle_area(n) / L_section_area < fraction: + return n + + +if __name__ == "__main__": + print(f"{solution() = }") From cd67c86bdf5972bbc91fd205376501662c5e1d56 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 25 Jul 2022 23:23:55 +0000 Subject: [PATCH 2/5] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1ee106252ce2..843ff77bb67b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -898,6 +898,8 @@ * [Sol1](project_euler/problem_493/sol1.py) * Problem 551 * [Sol1](project_euler/problem_551/sol1.py) + * Problem 587 + * [Sol1](project_euler/problem_587/sol1.py) * Problem 686 * [Sol1](project_euler/problem_686/sol1.py) From a12acece8d794e7af45e398b3f1cd2302da3ba62 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Tue, 26 Jul 2022 02:34:00 +0300 Subject: [PATCH 3/5] Add return statement --- project_euler/problem_587/sol1.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project_euler/problem_587/sol1.py b/project_euler/problem_587/sol1.py index 242d02e2450f..7f9f25a6b9f9 100644 --- a/project_euler/problem_587/sol1.py +++ b/project_euler/problem_587/sol1.py @@ -83,6 +83,8 @@ def solution(fraction: float = 1 / 1000) -> int: if concave_triangle_area(n) / L_section_area < fraction: return n + return -1 + if __name__ == "__main__": print(f"{solution() = }") From 847c14183690c63c859e3a4e5fc9c5e9e0012622 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Tue, 26 Jul 2022 02:42:22 +0300 Subject: [PATCH 4/5] Update variable name L_section_area accordingly --- project_euler/problem_587/sol1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_587/sol1.py b/project_euler/problem_587/sol1.py index 7f9f25a6b9f9..08168d696d12 100644 --- a/project_euler/problem_587/sol1.py +++ b/project_euler/problem_587/sol1.py @@ -77,10 +77,10 @@ def solution(fraction: float = 1 / 1000) -> int: 15 """ - L_section_area = (1 - pi / 4) / 4 + l_section_area = (1 - pi / 4) / 4 for n in count(1): - if concave_triangle_area(n) / L_section_area < fraction: + if concave_triangle_area(n) / l_section_area < fraction: return n return -1 From ea72d4c7411dd381fe4b37973ee75569a1f62276 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Tue, 26 Jul 2022 02:45:37 +0300 Subject: [PATCH 5/5] Provide descriptive names --- project_euler/problem_587/sol1.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_587/sol1.py b/project_euler/problem_587/sol1.py index 08168d696d12..dde5c16103ac 100644 --- a/project_euler/problem_587/sol1.py +++ b/project_euler/problem_587/sol1.py @@ -29,7 +29,7 @@ from math import asin, pi, sqrt -def circle_bottom_arc_integral(x: float) -> float: +def circle_bottom_arc_integral(point: float) -> float: """ Returns integral of circle bottom arc y = 1 / 2 - sqrt(1 / 4 - (x - 1 / 2) ^ 2) @@ -43,10 +43,12 @@ def circle_bottom_arc_integral(x: float) -> float: 0.5 """ - return ((1 - 2 * x) * sqrt(x - x**2) + 2 * x + asin(sqrt(1 - x))) / 4 + return ( + (1 - 2 * point) * sqrt(point - point**2) + 2 * point + asin(sqrt(1 - point)) + ) / 4 -def concave_triangle_area(n: int) -> float: +def concave_triangle_area(circles_number: int) -> float: """ Returns area of concave triangle @@ -57,8 +59,10 @@ def concave_triangle_area(n: int) -> float: 0.01956236140083944 """ - intersection_y = (n + 1 - sqrt(2 * n)) / (2 * (n**2 + 1)) - intersection_x = n * intersection_y + intersection_y = (circles_number + 1 - sqrt(2 * circles_number)) / ( + 2 * (circles_number**2 + 1) + ) + intersection_x = circles_number * intersection_y triangle_area = intersection_x * intersection_y / 2 concave_region_area = circle_bottom_arc_integral(