-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
feat: add Project Euler problem 073 solution 1 #6273
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-073-solution-1
Oct 24, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6a4377
Add solution
MaximSmolskiy 75b15ee
updating DIRECTORY.md
efe41c1
Add tests
MaximSmolskiy f2506d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 11268e3
Change tests
MaximSmolskiy 6eeef80
Retest
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,46 @@ | ||
""" | ||
Project Euler Problem 73: https://projecteuler.net/problem=73 | ||
|
||
Consider the fraction, n/d, where n and d are positive integers. | ||
If n<d and HCF(n,d)=1, it is called a reduced proper fraction. | ||
|
||
If we list the set of reduced proper fractions for d ≤ 8 in ascending order of size, | ||
we get: | ||
|
||
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, | ||
5/7, 3/4, 4/5, 5/6, 6/7, 7/8 | ||
|
||
It can be seen that there are 3 fractions between 1/3 and 1/2. | ||
|
||
How many fractions lie between 1/3 and 1/2 in the sorted set | ||
of reduced proper fractions for d ≤ 12,000? | ||
""" | ||
|
||
from math import gcd | ||
|
||
|
||
def solution(max_d: int = 12_000) -> int: | ||
""" | ||
Returns number of fractions lie between 1/3 and 1/2 in the sorted set | ||
of reduced proper fractions for d ≤ max_d | ||
|
||
>>> solution(4) | ||
0 | ||
|
||
>>> solution(5) | ||
1 | ||
|
||
>>> solution(8) | ||
3 | ||
""" | ||
|
||
fractions_number = 0 | ||
for d in range(max_d + 1): | ||
for n in range(d // 3 + 1, (d + 1) // 2): | ||
if gcd(n, d) == 1: | ||
fractions_number += 1 | ||
return fractions_number | ||
|
||
|
||
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.