-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Implemented improved algorithm using Numba for Project Euler Problem 73 #11204
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
Closed
Closed
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e541045
Implemented improved algorithm using Numba for Project Euler Problem 73
venkat1924 3f00dbd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8d6ab30
Added code to install Numba automatically when code is run.
venkat1924 d3f96f4
Merge remote-tracking branch 'origin/fix-euler-problem-73' into fix-e…
venkat1924 52acb90
Changed the code to not use Numba, since that created issues during p…
venkat1924 1278112
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8f11b8c
Corrected mistakes with naming.
venkat1924 93dc2c4
Merge remote-tracking branch 'origin/fix-euler-problem-73' into fix-e…
venkat1924 e965f3b
Removed unused math module, replaced a list comprehension with list().
venkat1924 a4aca01
Updated doctring explaining solution function.
venkat1924 4d99dab
Fixed indentation.
venkat1924 95a7331
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 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,48 @@ | ||
""" | ||
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? | ||
""" | ||
|
||
|
||
def solution(limit: 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 | ||
""" | ||
phi = list(range(limit + 1)) | ||
count = 0 | ||
|
||
for d in range(2, limit + 1): | ||
if phi[d] == d: | ||
for j in range(d, limit + 1, d): | ||
phi[j] -= phi[j] // d | ||
|
||
count += phi[d] // 2 - phi[(d + 2) // 3] | ||
|
||
return count | ||
|
||
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occurred while parsing the file:
project_euler/problem_073/sol2.py