From c5276b46f3bf33848cbf0ab3fae22fa76803482a Mon Sep 17 00:00:00 2001 From: utkarsh Date: Tue, 6 Oct 2020 22:41:44 +0530 Subject: [PATCH 1/3] Added Problem 72 --- project_euler/problem_72/__init__.py | 0 project_euler/problem_72/sol1.py | 46 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 project_euler/problem_72/__init__.py create mode 100644 project_euler/problem_72/sol1.py diff --git a/project_euler/problem_72/__init__.py b/project_euler/problem_72/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_72/sol1.py b/project_euler/problem_72/sol1.py new file mode 100644 index 000000000000..ce6268a0d1a4 --- /dev/null +++ b/project_euler/problem_72/sol1.py @@ -0,0 +1,46 @@ +""" +Problem 72 Counting fractions: https://projecteuler.net/problem=72 + +Description: + +Consider the fraction, n/d, where n and d are positive integers. If n int: + """ + Returns an integer, the solution to the problem + >>> solution(10) + 31 + >>> solution(100) + 3043 + >>> solution(1_000_000) + 303963552391 + """ + + phi = [i - 1 for i in range(N + 1)] + + for i in range(2, N + 1): + for j in range(2 * i, N + 1, i): + phi[j] -= phi[i] + + return sum(phi[2 : N + 1]) + + +if __name__ == "__main__": + print(solution(100)) From 50059e5614cdf1722b400339a4695fa6e97b2f70 Mon Sep 17 00:00:00 2001 From: utkarsh Date: Tue, 6 Oct 2020 22:47:34 +0530 Subject: [PATCH 2/3] Removed args from solution() --- project_euler/problem_72/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_72/sol1.py b/project_euler/problem_72/sol1.py index ce6268a0d1a4..03ac1813935b 100644 --- a/project_euler/problem_72/sol1.py +++ b/project_euler/problem_72/sol1.py @@ -43,4 +43,4 @@ def solution(N: int = 1_000_000) -> int: if __name__ == "__main__": - print(solution(100)) + print(solution()) From cacd8b082ef3d276130550cbddf5750cf7d80dcb Mon Sep 17 00:00:00 2001 From: utkarsh Date: Mon, 12 Oct 2020 17:30:29 +0530 Subject: [PATCH 3/3] Incorporated the suggested changes --- project_euler/problem_72/sol1.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/project_euler/problem_72/sol1.py b/project_euler/problem_72/sol1.py index 03ac1813935b..846396ab0f9c 100644 --- a/project_euler/problem_72/sol1.py +++ b/project_euler/problem_72/sol1.py @@ -22,24 +22,24 @@ """ -def solution(N: int = 1_000_000) -> int: +def solution(limit: int = 1_000_000) -> int: """ Returns an integer, the solution to the problem >>> solution(10) 31 >>> solution(100) 3043 - >>> solution(1_000_000) - 303963552391 + >>> solution(1_000) + 304191 """ - phi = [i - 1 for i in range(N + 1)] + phi = [i - 1 for i in range(limit + 1)] - for i in range(2, N + 1): - for j in range(2 * i, N + 1, i): + for i in range(2, limit + 1): + for j in range(2 * i, limit + 1, i): phi[j] -= phi[i] - return sum(phi[2 : N + 1]) + return sum(phi[2 : limit + 1]) if __name__ == "__main__":