Skip to content

Commit a041b64

Browse files
MaximSmolskiygithub-actionspre-commit-ci[bot]
authored
feat: add Project Euler problem 073 solution 1 (#6273)
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d8ab8a0 commit a041b64

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,8 @@
839839
* Problem 072
840840
* [Sol1](project_euler/problem_072/sol1.py)
841841
* [Sol2](project_euler/problem_072/sol2.py)
842+
* Problem 073
843+
* [Sol1](project_euler/problem_073/sol1.py)
842844
* Problem 074
843845
* [Sol1](project_euler/problem_074/sol1.py)
844846
* [Sol2](project_euler/problem_074/sol2.py)

Diff for: project_euler/problem_073/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_073/sol1.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Project Euler Problem 73: https://projecteuler.net/problem=73
3+
4+
Consider the fraction, n/d, where n and d are positive integers.
5+
If n<d and HCF(n,d)=1, it is called a reduced proper fraction.
6+
7+
If we list the set of reduced proper fractions for d ≤ 8 in ascending order of size,
8+
we get:
9+
10+
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,
11+
5/7, 3/4, 4/5, 5/6, 6/7, 7/8
12+
13+
It can be seen that there are 3 fractions between 1/3 and 1/2.
14+
15+
How many fractions lie between 1/3 and 1/2 in the sorted set
16+
of reduced proper fractions for d ≤ 12,000?
17+
"""
18+
19+
from math import gcd
20+
21+
22+
def solution(max_d: int = 12_000) -> int:
23+
"""
24+
Returns number of fractions lie between 1/3 and 1/2 in the sorted set
25+
of reduced proper fractions for d ≤ max_d
26+
27+
>>> solution(4)
28+
0
29+
30+
>>> solution(5)
31+
1
32+
33+
>>> solution(8)
34+
3
35+
"""
36+
37+
fractions_number = 0
38+
for d in range(max_d + 1):
39+
for n in range(d // 3 + 1, (d + 1) // 2):
40+
if gcd(n, d) == 1:
41+
fractions_number += 1
42+
return fractions_number
43+
44+
45+
if __name__ == "__main__":
46+
print(f"{solution() = }")

0 commit comments

Comments
 (0)