Skip to content
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

Implemented improved algorithm using Numba for Project Euler Problem 73 #11204

Closed
wants to merge 12 commits into from
Closed
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions project_euler/problem_073/sol2.py
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))

Check failure on line 34 in project_euler/problem_073/sol2.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E999)

project_euler/problem_073/sol2.py:34:1: E999 SyntaxError: Unexpected token Indent
count = 0

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

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 35:5.
parser error: error at 34:4: expected one of (, *, +, -, ..., AWAIT, DEDENT, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~

    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() = }")