File tree 1 file changed +5
-4
lines changed
project_euler/problem_073
1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change 18
18
19
19
from math import gcd
20
20
21
+
21
22
def slow_solution (max_d : int = 12_000 ) -> int :
22
23
"""
23
24
Returns number of fractions lie between 1/3 and 1/2 in the sorted set
@@ -40,6 +41,7 @@ def slow_solution(max_d: int = 12_000) -> int:
40
41
fractions_number += 1
41
42
return fractions_number
42
43
44
+
43
45
def solution (max_d : int = 12_000 ) -> int :
44
46
"""
45
47
Returns number of fractions lie between 1/3 and 1/2 in the sorted set
@@ -58,10 +60,7 @@ def solution(max_d: int = 12_000) -> int:
58
60
fractions_number = 0
59
61
for d in range (max_d + 1 ):
60
62
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
63
+ n_start = d // 3 + 2 if (d // 3 + 1 ) % 2 == 0 else d // 3 + 1
65
64
for n in range (n_start , (d + 1 ) // 2 , 2 ):
66
65
if gcd (n , d ) == 1 :
67
66
fractions_number += 1
@@ -71,6 +70,7 @@ def solution(max_d: int = 12_000) -> int:
71
70
fractions_number += 1
72
71
return fractions_number
73
72
73
+
74
74
def benchmark () -> None :
75
75
"""
76
76
Benchmarks
@@ -86,6 +86,7 @@ def benchmark() -> None:
86
86
print (f"slow_solution : { timeit ('slow_solution()' , globals = globals (), number = 10 )} " )
87
87
print (f"solution : { timeit ('solution()' , globals = globals (), number = 10 )} " )
88
88
89
+
89
90
if __name__ == "__main__" :
90
91
print (f"{ solution () = } " )
91
92
benchmark ()
You can’t perform that action at this time.
0 commit comments