From 320f8f2d8c482f8a8084c0e162c69c1af90298e4 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:33:46 +0530 Subject: [PATCH 01/12] Project euler 95 --- project_euler/problem_095/__init__.py | 0 project_euler/problem_095/sol1.py | 38 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 project_euler/problem_095/__init__.py create mode 100644 project_euler/problem_095/sol1.py diff --git a/project_euler/problem_095/__init__.py b/project_euler/problem_095/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py new file mode 100644 index 000000000000..54f2ba687def --- /dev/null +++ b/project_euler/problem_095/sol1.py @@ -0,0 +1,38 @@ +""" +Problem 95 +Url: https://projecteuler.net/problem=95 +""" +def find_smallest_member(n: int) -> int: + """ + Returns the smallest member of the longest amicable chain with no element exceeding one million + >> 14316 + """ + + sum_of_div = [0] * (n + 1) + for i in range(1, n // 2 + 1): + for j in range(i * 2, n + 1, i): + sum_of_div[j] += i + + checked = [False] * (n + 1) + max_len_of_chain = 0 + result = 0 + for i in range(2, n + 1): + possible_chain = [] + j = i + while not checked[j]: + checked[j] = True + possible_chain.append(j) + j = sum_of_div[j] + if j > n: + break + if j in possible_chain: + len_of_chain = len(possible_chain) - possible_chain.index(j) + if len_of_chain > max_len_of_chain: + max_len_of_chain = len_of_chain + result = min(possible_chain[-len_of_chain:]) + break + return result + + +if __name__ == "__main__": + print(f"Solution : {find_smallest_member(10**6)}") From 9ab8052b9c4b5e570ba674846aa0e69fc2c3ad89 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:39:10 +0530 Subject: [PATCH 02/12] adding doctests --- project_euler/problem_095/sol1.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 54f2ba687def..85e85e9ed5a3 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -2,7 +2,7 @@ Problem 95 Url: https://projecteuler.net/problem=95 """ -def find_smallest_member(n: int) -> int: +def solution(n: int = 10**6) -> int: """ Returns the smallest member of the longest amicable chain with no element exceeding one million >> 14316 @@ -35,4 +35,7 @@ def find_smallest_member(n: int) -> int: if __name__ == "__main__": - print(f"Solution : {find_smallest_member(10**6)}") + import doctest + + doctest.testmod() + print(f"{solution() = }") From a3638197c7f5e48e6d933ec8d250fda240d0f20a Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:40:40 +0530 Subject: [PATCH 03/12] adding doctests --- project_euler/problem_095/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 85e85e9ed5a3..f2a290331304 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -4,7 +4,7 @@ """ def solution(n: int = 10**6) -> int: """ - Returns the smallest member of the longest amicable chain with no element exceeding one million + Returns the smallest member when n = 1000000 >> 14316 """ From 6d688122d3b7094ce464061435313168238cdb41 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 04:11:39 +0000 Subject: [PATCH 04/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_095/sol1.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index f2a290331304..a76f249d139d 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -2,6 +2,8 @@ Problem 95 Url: https://projecteuler.net/problem=95 """ + + def solution(n: int = 10**6) -> int: """ Returns the smallest member when n = 1000000 From 51a626b7b3ba19aadb5c37310f61490d6e58f828 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:44:17 +0530 Subject: [PATCH 05/12] descriptive names --- project_euler/problem_095/sol1.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index a76f249d139d..1f7dc5489ade 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -4,28 +4,28 @@ """ -def solution(n: int = 10**6) -> int: +def solution(number : int = 10**6) -> int: """ Returns the smallest member when n = 1000000 >> 14316 """ - sum_of_div = [0] * (n + 1) - for i in range(1, n // 2 + 1): - for j in range(i * 2, n + 1, i): + sum_of_div = [0] * (number + 1) + for i in range(1, number // 2 + 1): + for j in range(i * 2, number + 1, i): sum_of_div[j] += i - checked = [False] * (n + 1) + checked = [False] * (number + 1) max_len_of_chain = 0 result = 0 - for i in range(2, n + 1): + for i in range(2, number + 1): possible_chain = [] j = i while not checked[j]: checked[j] = True possible_chain.append(j) j = sum_of_div[j] - if j > n: + if j > number: break if j in possible_chain: len_of_chain = len(possible_chain) - possible_chain.index(j) From 977446f234af3dcb700fb891475a7ef0871fe13e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 04:14:55 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_095/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 1f7dc5489ade..18b97bf9be48 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -4,7 +4,7 @@ """ -def solution(number : int = 10**6) -> int: +def solution(number: int = 10**6) -> int: """ Returns the smallest member when n = 1000000 >> 14316 From 3ddfcdc25163bae76af33e4afb67f5a92fca29a4 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:47:53 +0530 Subject: [PATCH 07/12] examples --- project_euler/problem_095/sol1.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 1f7dc5489ade..82bbc0d3d68a 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -1,13 +1,30 @@ """ Problem 95 Url: https://projecteuler.net/problem=95 -""" +Statement: +The proper divisors of a number are all the divisors excluding the number itself. +For example, the proper divisors of 28 is 1,2,4,7 and 14 and . +As the sum of these divisors is equal to 28, we call it a perfect number. + +Interestingly the sum of the proper divisors of 220 is 284 +The sum of the proper divisors of 284 is 220 forming a chain of two numbers. +For this reason, 220 and 284 are called an amicable pair. +Perhaps less well known are longer chains. +For example, starting with 12496, we form a chain of five numbers: +12496 -> 14288 -> 15472 -> 14536 -> 14264(->12496 -> ....) +Since this chain returns to its starting point, it is called an amicable chain. + +Find the smallest member of the longest amicable chain with no element exceeding one million. +""" def solution(number : int = 10**6) -> int: """ Returns the smallest member when n = 1000000 - >> 14316 + >>> solution(1000000) + 14316 + >>> solution(5000) + 220 """ sum_of_div = [0] * (number + 1) From 6f0f56305ed42e9492afdbf18c1cbf5a60704595 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 04:18:47 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_095/sol1.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 9c01113a2434..1b6a70f553c7 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -6,11 +6,11 @@ For example, the proper divisors of 28 is 1,2,4,7 and 14 and . As the sum of these divisors is equal to 28, we call it a perfect number. -Interestingly the sum of the proper divisors of 220 is 284 +Interestingly the sum of the proper divisors of 220 is 284 The sum of the proper divisors of 284 is 220 forming a chain of two numbers. For this reason, 220 and 284 are called an amicable pair. -Perhaps less well known are longer chains. +Perhaps less well known are longer chains. For example, starting with 12496, we form a chain of five numbers: 12496 -> 14288 -> 15472 -> 14536 -> 14264(->12496 -> ....) Since this chain returns to its starting point, it is called an amicable chain. @@ -18,6 +18,7 @@ Find the smallest member of the longest amicable chain with no element exceeding one million. """ + def solution(number: int = 10**6) -> int: """ Returns the smallest member when n = 1000000 From c19bd99827ee9224e20201f3756bd1e300448c3c Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:49:53 +0530 Subject: [PATCH 09/12] ruff errors sorted --- project_euler/problem_095/sol1.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 1b6a70f553c7..2433191a98ff 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -3,13 +3,12 @@ Url: https://projecteuler.net/problem=95 Statement: The proper divisors of a number are all the divisors excluding the number itself. -For example, the proper divisors of 28 is 1,2,4,7 and 14 and . +For example, the proper divisors of 28 is 1,2,4,7 and 14 As the sum of these divisors is equal to 28, we call it a perfect number. Interestingly the sum of the proper divisors of 220 is 284 The sum of the proper divisors of 284 is 220 forming a chain of two numbers. For this reason, 220 and 284 are called an amicable pair. - Perhaps less well known are longer chains. For example, starting with 12496, we form a chain of five numbers: 12496 -> 14288 -> 15472 -> 14536 -> 14264(->12496 -> ....) From 9d6cb8e4ded8e3c423fb54f89dc4c7ccd347170a Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:50:36 +0530 Subject: [PATCH 10/12] ruff errors sorted --- project_euler/problem_095/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 2433191a98ff..4bffbcee7920 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -14,7 +14,8 @@ 12496 -> 14288 -> 15472 -> 14536 -> 14264(->12496 -> ....) Since this chain returns to its starting point, it is called an amicable chain. -Find the smallest member of the longest amicable chain with no element exceeding one million. +Find the smallest member of the longest amicable chain with .. +no element exceeding one million. """ From e652c2806970471e16be61612952197a14d31dbd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 04:20:43 +0000 Subject: [PATCH 11/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_095/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 4bffbcee7920..36c550d0dc14 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -3,7 +3,7 @@ Url: https://projecteuler.net/problem=95 Statement: The proper divisors of a number are all the divisors excluding the number itself. -For example, the proper divisors of 28 is 1,2,4,7 and 14 +For example, the proper divisors of 28 is 1,2,4,7 and 14 As the sum of these divisors is equal to 28, we call it a perfect number. Interestingly the sum of the proper divisors of 220 is 284 From 18e2c7cf70550b26bf95d6c088eeff65012f02f1 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:51:15 +0530 Subject: [PATCH 12/12] ruff errors sorted --- project_euler/problem_095/sol1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_095/sol1.py b/project_euler/problem_095/sol1.py index 4bffbcee7920..c89866cd162b 100644 --- a/project_euler/problem_095/sol1.py +++ b/project_euler/problem_095/sol1.py @@ -3,10 +3,10 @@ Url: https://projecteuler.net/problem=95 Statement: The proper divisors of a number are all the divisors excluding the number itself. -For example, the proper divisors of 28 is 1,2,4,7 and 14 +For example, the proper divisors of 28 is 1,2,4,7 and 14. As the sum of these divisors is equal to 28, we call it a perfect number. -Interestingly the sum of the proper divisors of 220 is 284 +Interestingly the sum of the proper divisors of 220 is 284. The sum of the proper divisors of 284 is 220 forming a chain of two numbers. For this reason, 220 and 284 are called an amicable pair. Perhaps less well known are longer chains.