From 09a44fd5367cae3a6ee751af6d05807036c8d014 Mon Sep 17 00:00:00 2001 From: Venkata Ramana Menda Date: Fri, 6 Sep 2024 23:15:34 +0530 Subject: [PATCH 1/4] Seperate slow_solution and solution --- project_euler/problem_073/sol1.py | 36 ++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_073/sol1.py b/project_euler/problem_073/sol1.py index 2b66b7d8769b..bd5db31ac358 100644 --- a/project_euler/problem_073/sol1.py +++ b/project_euler/problem_073/sol1.py @@ -18,6 +18,27 @@ from math import gcd +def slow_solution(max_d: 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 + + >>> slow_solution(4) + 0 + + >>> slow_solution(5) + 1 + + >>> slow_solution(8) + 3 + """ + + fractions_number = 0 + for d in range(max_d + 1): + for n in range(d // 3 + 1, (d + 1) // 2): + if gcd(n, d) == 1: + fractions_number += 1 + return fractions_number def solution(max_d: int = 12_000) -> int: """ @@ -36,9 +57,18 @@ def solution(max_d: int = 12_000) -> int: fractions_number = 0 for d in range(max_d + 1): - for n in range(d // 3 + 1, (d + 1) // 2): - if gcd(n, d) == 1: - fractions_number += 1 + if d % 2 == 0: + if (d // 3 + 1) % 2 == 0: + n_start = d // 3 + 2 + else: + n_start = d // 3 + 1 + for n in range(n_start, (d + 1) // 2, 2): + if gcd(n, d) == 1: + fractions_number += 1 + else: + for n in range(d // 3 + 1, (d + 1) // 2): + if gcd(n, d) == 1: + fractions_number += 1 return fractions_number From 30aabe7ccfc342e9db00d63c10788efc2c5d3623 Mon Sep 17 00:00:00 2001 From: Venkata Ramana Menda Date: Fri, 6 Sep 2024 23:18:21 +0530 Subject: [PATCH 2/4] Add performance benchmark --- project_euler/problem_073/sol1.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/project_euler/problem_073/sol1.py b/project_euler/problem_073/sol1.py index bd5db31ac358..15f3c1be05a9 100644 --- a/project_euler/problem_073/sol1.py +++ b/project_euler/problem_073/sol1.py @@ -71,6 +71,21 @@ def solution(max_d: int = 12_000) -> int: fractions_number += 1 return fractions_number +def benchmark() -> None: + """ + Benchmarks + """ + # Running performance benchmarks... + # slow_solution : 21.02750190000006 + # solution : 15.79036830000041 + + from timeit import timeit + + print("Running performance benchmarks...") + + print(f"slow_solution : {timeit('slow_solution()', globals=globals(), number=10)}") + print(f"solution : {timeit('solution()', globals=globals(), number=10)}") if __name__ == "__main__": print(f"{solution() = }") + benchmark() From fdf3614bd4a4daf7b434c0db69cfd7490ebe847b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Sep 2024 18:01:01 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_073/sol1.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/project_euler/problem_073/sol1.py b/project_euler/problem_073/sol1.py index 15f3c1be05a9..02b82f89f120 100644 --- a/project_euler/problem_073/sol1.py +++ b/project_euler/problem_073/sol1.py @@ -18,6 +18,7 @@ from math import gcd + def slow_solution(max_d: int = 12_000) -> int: """ 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: fractions_number += 1 return fractions_number + def solution(max_d: int = 12_000) -> int: """ Returns number of fractions lie between 1/3 and 1/2 in the sorted set @@ -71,6 +73,7 @@ def solution(max_d: int = 12_000) -> int: fractions_number += 1 return fractions_number + def benchmark() -> None: """ Benchmarks @@ -86,6 +89,7 @@ def benchmark() -> None: print(f"slow_solution : {timeit('slow_solution()', globals=globals(), number=10)}") print(f"solution : {timeit('solution()', globals=globals(), number=10)}") + if __name__ == "__main__": print(f"{solution() = }") benchmark() From 8298470273c1cec63252537f2c4acbd986ee66da Mon Sep 17 00:00:00 2001 From: Venkata Ramana Menda Date: Fri, 6 Sep 2024 23:38:19 +0530 Subject: [PATCH 4/4] Fix issues --- project_euler/problem_073/sol1.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_073/sol1.py b/project_euler/problem_073/sol1.py index 15f3c1be05a9..8d51ed5d1877 100644 --- a/project_euler/problem_073/sol1.py +++ b/project_euler/problem_073/sol1.py @@ -18,6 +18,7 @@ from math import gcd + def slow_solution(max_d: int = 12_000) -> int: """ 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: fractions_number += 1 return fractions_number + def solution(max_d: int = 12_000) -> int: """ 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: fractions_number = 0 for d in range(max_d + 1): if d % 2 == 0: - if (d // 3 + 1) % 2 == 0: - n_start = d // 3 + 2 - else: - n_start = d // 3 + 1 + n_start = d // 3 + 2 if (d // 3 + 1) % 2 == 0 else d // 3 + 1 for n in range(n_start, (d + 1) // 2, 2): if gcd(n, d) == 1: fractions_number += 1 @@ -71,6 +70,7 @@ def solution(max_d: int = 12_000) -> int: fractions_number += 1 return fractions_number + def benchmark() -> None: """ Benchmarks @@ -86,6 +86,7 @@ def benchmark() -> None: print(f"slow_solution : {timeit('slow_solution()', globals=globals(), number=10)}") print(f"solution : {timeit('solution()', globals=globals(), number=10)}") + if __name__ == "__main__": print(f"{solution() = }") benchmark()