Skip to content

Commit 09a44fd

Browse files
committed
Seperate slow_solution and solution
1 parent f16d38f commit 09a44fd

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

project_euler/problem_073/sol1.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@
1818

1919
from math import gcd
2020

21+
def slow_solution(max_d: int = 12_000) -> int:
22+
"""
23+
Returns number of fractions lie between 1/3 and 1/2 in the sorted set
24+
of reduced proper fractions for d ≤ max_d
25+
26+
>>> slow_solution(4)
27+
0
28+
29+
>>> slow_solution(5)
30+
1
31+
32+
>>> slow_solution(8)
33+
3
34+
"""
35+
36+
fractions_number = 0
37+
for d in range(max_d + 1):
38+
for n in range(d // 3 + 1, (d + 1) // 2):
39+
if gcd(n, d) == 1:
40+
fractions_number += 1
41+
return fractions_number
2142

2243
def solution(max_d: int = 12_000) -> int:
2344
"""
@@ -36,9 +57,18 @@ def solution(max_d: int = 12_000) -> int:
3657

3758
fractions_number = 0
3859
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
60+
if d % 2 == 0:
61+
if (d // 3 + 1) % 2 == 0:
62+
n_start = d // 3 + 2
63+
else:
64+
n_start = d // 3 + 1
65+
for n in range(n_start, (d + 1) // 2, 2):
66+
if gcd(n, d) == 1:
67+
fractions_number += 1
68+
else:
69+
for n in range(d // 3 + 1, (d + 1) // 2):
70+
if gcd(n, d) == 1:
71+
fractions_number += 1
4272
return fractions_number
4373

4474

0 commit comments

Comments
 (0)