-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added solution for Project Euler problem 75. #3129
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
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
11ab30b
Added solution for Project Euler problem 75. Fixes: #2695
fpringle 0343b15
Added doctest for solution() in project_euler/problem_75/sol1.py
fpringle bc9f738
Update docstring and 0-padding of directory name. Reference: #3256
fpringle 908914e
More descriptive variable names
fpringle d64377f
Moved solution explanation to module-level docstring
fpringle 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
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,53 @@ | ||
""" | ||
Project Euler Problem 75: https://projecteuler.net/problem=75 | ||
|
||
It turns out that 12 cm is the smallest length of wire that can be bent to form an | ||
integer sided right angle triangle in exactly one way, but there are many more examples. | ||
|
||
12 cm: (3,4,5) | ||
24 cm: (6,8,10) | ||
30 cm: (5,12,13) | ||
36 cm: (9,12,15) | ||
40 cm: (8,15,17) | ||
48 cm: (12,16,20) | ||
|
||
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided | ||
right angle triangle, and other lengths allow more than one solution to be found; for | ||
example, using 120 cm it is possible to form exactly three different integer sided | ||
right angle triangles. | ||
|
||
120 cm: (30,40,50), (20,48,52), (24,45,51) | ||
|
||
Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can | ||
exactly one integer sided right angle triangle be formed? | ||
""" | ||
|
||
from collections import defaultdict | ||
from math import gcd | ||
from typing import DefaultDict | ||
|
||
|
||
def solution(limit: int = 1500000) -> int: | ||
""" | ||
Return the number of values of L <= limit such that a wire of length L can be | ||
formmed into an integer sided right angle triangle in exactly one way. | ||
>>> solution(50) | ||
6 | ||
>>> solution(1000) | ||
112 | ||
""" | ||
freqs: DefaultDict = defaultdict(int) | ||
m = 2 | ||
while 2 * m * (m + 1) <= limit: | ||
for n in range((m % 2) + 1, m, 2): | ||
if gcd(m, n) > 1: | ||
continue | ||
perim = 2 * m * (m + n) | ||
for p in range(perim, limit + 1, perim): | ||
freqs[p] += 1 | ||
m += 1 | ||
return sum(1 for v in freqs.values() if v == 1) | ||
|
||
|
||
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.