From 36f4fb1b812a7fe9bc7b2415d8d5f6de67176677 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:01:11 +0530 Subject: [PATCH 01/10] Project Euler 95 solution --- project_euler/problem_95 | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 project_euler/problem_95 diff --git a/project_euler/problem_95 b/project_euler/problem_95 new file mode 100644 index 000000000000..314c7fa885c3 --- /dev/null +++ b/project_euler/problem_95 @@ -0,0 +1,47 @@ +""" +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 and 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 find_smallest_member(N): + + # calculate sum of proper divisors for every number up to N + sum_div = [0] * (N+1) + for i in range(1, N//2+1): + for j in range(i*2, N+1, i): + sum_div[j] += i + + # find amicable chains + visited = [False] * (N+1) + max_chain_len = 0 + result = 0 + for i in range(2, N+1): + chain = [] + j = i + while not visited[j]: + visited[j] = True + chain.append(j) + j = sum_div[j] + if j > N: + break + if j in chain: + chain_len = len(chain) - chain.index(j) + if chain_len > max_chain_len: + max_chain_len = chain_len + result = min(chain[-chain_len:]) + break + return result + +if __name__ == "__main__": + print(find_smallest_member(10**6)) + From 4f58d3a55c8570bcd0502f7a80b74e0bb05b5e65 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:07:54 +0530 Subject: [PATCH 02/10] change in 95 --- project_euler/problem_95 | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/project_euler/problem_95 b/project_euler/problem_95 index 314c7fa885c3..aeb917e36eb0 100644 --- a/project_euler/problem_95 +++ b/project_euler/problem_95 @@ -13,32 +13,30 @@ 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 find_smallest_member(N): +def find_smallest_member(n): - # calculate sum of proper divisors for every number up to N - sum_div = [0] * (N+1) - for i in range(1, N//2+1): - for j in range(i*2, N+1, i): - sum_div[j] += i + 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 - # find amicable chains - visited = [False] * (N+1) + checked = [False] * (n+1) max_chain_len = 0 result = 0 - for i in range(2, N+1): - chain = [] + for i in range(2, n+1): + possible_chain = [] j = i - while not visited[j]: - visited[j] = True - chain.append(j) - j = sum_div[j] - if j > N: + while not checked[j]: + checked[j] = True + possible_chain.append(j) + j = sum_of_div[j] + if j > n: break - if j in chain: - chain_len = len(chain) - chain.index(j) - if chain_len > max_chain_len: - max_chain_len = chain_len - result = min(chain[-chain_len:]) + 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 From cec24b95a683c5628e2429b687fe6c340d26911c Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:11:42 +0530 Subject: [PATCH 03/10] change in 95 --- project_euler/problem_95 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/project_euler/problem_95 b/project_euler/problem_95 index aeb917e36eb0..68c4f4c83a07 100644 --- a/project_euler/problem_95 +++ b/project_euler/problem_95 @@ -14,6 +14,10 @@ Find the smallest member of the longest amicable chain with no element exceeding """ def find_smallest_member(n): + """ + 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): From a0bc2069b52af1099efb90a6a5ec711ce0830f57 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:13:06 +0530 Subject: [PATCH 04/10] change in 95 --- project_euler/problem_95 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_95 b/project_euler/problem_95 index 68c4f4c83a07..49c2732cdd47 100644 --- a/project_euler/problem_95 +++ b/project_euler/problem_95 @@ -45,5 +45,5 @@ def find_smallest_member(n): return result if __name__ == "__main__": - print(find_smallest_member(10**6)) + print(f"Solution : {find_smallest_member(10**6)}") From 11af4cec08b2c984ecc15277232c1e74919480b0 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:14:26 +0530 Subject: [PATCH 05/10] change in 95 --- project_euler/{problem_95 => sol1.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename project_euler/{problem_95 => sol1.py} (100%) diff --git a/project_euler/problem_95 b/project_euler/sol1.py similarity index 100% rename from project_euler/problem_95 rename to project_euler/sol1.py From ea35a714ed225a604a738c8f6fa2904b36082c2a Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:15:02 +0530 Subject: [PATCH 06/10] change in 95 --- project_euler/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/sol1.py b/project_euler/sol1.py index 49c2732cdd47..f637bf24d888 100644 --- a/project_euler/sol1.py +++ b/project_euler/sol1.py @@ -13,7 +13,7 @@ Find the smallest member of the longest amicable chain with no element exceeding one million. """ -def find_smallest_member(n): +def find_smallest_member(n : int) -> int: """ Returns the smallest member of the longest amicable chain with no element exceeding one million >> 14316 From 6b19990e841bd75745a371b4df5d8b94135b55b3 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 03:46:59 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/sol1.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/project_euler/sol1.py b/project_euler/sol1.py index f637bf24d888..b47806c9708d 100644 --- a/project_euler/sol1.py +++ b/project_euler/sol1.py @@ -13,21 +13,22 @@ Find the smallest member of the longest amicable chain with no element exceeding one million. """ -def find_smallest_member(n : int) -> int: + +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 = [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) + checked = [False] * (n + 1) max_chain_len = 0 result = 0 - for i in range(2, n+1): + for i in range(2, n + 1): possible_chain = [] j = i while not checked[j]: @@ -44,6 +45,6 @@ def find_smallest_member(n : int) -> int: break return result + if __name__ == "__main__": print(f"Solution : {find_smallest_member(10**6)}") - From 3427fbaccfeffc249589e4c9659ff74a5a1be22f Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:21:14 +0530 Subject: [PATCH 08/10] change in 95 --- project_euler/sol1.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/project_euler/sol1.py b/project_euler/sol1.py index f637bf24d888..c9b5222c8f3b 100644 --- a/project_euler/sol1.py +++ b/project_euler/sol1.py @@ -2,9 +2,12 @@ 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. +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 and 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. +Interestingly the sum of the proper divisors of 220 is 284 and 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 1642e3d79f36b7cb9b27cd91f6ff972e6ed2fe54 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 1 Oct 2023 09:24:15 +0530 Subject: [PATCH 09/10] change in 95 --- project_euler/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/sol1.py b/project_euler/sol1.py index e0cd5af7775d..627cb062b63c 100644 --- a/project_euler/sol1.py +++ b/project_euler/sol1.py @@ -29,7 +29,7 @@ def find_smallest_member(n: int) -> int: sum_of_div[j] += i checked = [False] * (n + 1) - max_chain_len = 0 + max_len_of_chain = 0 result = 0 for i in range(2, n + 1): possible_chain = [] From 3d21ced50be109850a448adf81b259c2eac6ccf8 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 03:54:18 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/sol1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/sol1.py b/project_euler/sol1.py index e0cd5af7775d..eda391859ac0 100644 --- a/project_euler/sol1.py +++ b/project_euler/sol1.py @@ -2,11 +2,11 @@ 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 . +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 and the sum of the proper divisors of 284 is 220 forming a chain of two numbers. +Interestingly the sum of the proper divisors of 220 is 284 and 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: