-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add Project Euler problem 587 solution 1 #6269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dhruvmanila
merged 6 commits into
TheAlgorithms:master
from
MaximSmolskiy:add-project-euler-problem-587-solution-1
Jul 26, 2022
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9651b1f
Add solution
MaximSmolskiy cd67c86
updating DIRECTORY.md
a12acec
Add return statement
MaximSmolskiy fe102ee
Merge branch 'add-project-euler-problem-587-solution-1' of https://gi…
MaximSmolskiy 847c141
Update variable name L_section_area accordingly
MaximSmolskiy ea72d4c
Provide descriptive names
MaximSmolskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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: | ||
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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 | ||
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for n in count(1): | ||
if concave_triangle_area(n) / L_section_area < fraction: | ||
return n | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{solution() = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.