From cde6389000ca480954d0a961e6a6e4b1a0052f5b Mon Sep 17 00:00:00 2001 From: mindaugl Date: Mon, 7 Apr 2025 10:41:32 +0800 Subject: [PATCH 01/13] Add initial version of file for the Euler project problem 136 solution. --- project_euler/problem_136/__init__.py | 0 project_euler/problem_136/sol1.py | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 project_euler/problem_136/__init__.py create mode 100644 project_euler/problem_136/sol1.py diff --git a/project_euler/problem_136/__init__.py b/project_euler/problem_136/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py new file mode 100644 index 000000000000..15bea7c77824 --- /dev/null +++ b/project_euler/problem_136/sol1.py @@ -0,0 +1,23 @@ +N = 50000000 + + +def solution(n_limit: int = N) -> int: + n_sol = [0] * n_limit + + for delta in range(1, (n_limit + 1) // 4): + for y in range(4 * delta - 1, delta, -1): + n = y * (4 * delta - y) + if n >= n_limit: + break + n_sol[n] += 1 + + ans = 0 + for i in range(n_limit): + if n_sol[i] == 1: + ans += 1 + + return ans + + +if __name__ == "__main__": + print(f"{solution() = }") From 028055e9a25f6547375affa1bced97da8b3f5bf7 Mon Sep 17 00:00:00 2001 From: mindaugl Date: Mon, 7 Apr 2025 10:54:46 +0800 Subject: [PATCH 02/13] Add documentation and tests for the Euler project problem 136 solution. --- project_euler/problem_136/sol1.py | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index 15bea7c77824..53330357b62c 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -1,7 +1,41 @@ +""" +Project Euler Problem 136: https://projecteuler.net/problem=136 + +Singleton Difference + +By change of variables + +x = y + delta +z = y - delta + +The expression can be rewritten: + +x^2 - y^2 - z^2 = y*(4*delta - y) = n + +The algorithm loops over delta and y, which is restricted in upper and lower limits, +to count how many solutions each n has. +In the end it is counted how many n's have one solution. + + +>>> solution(100) +25 +""" + N = 50000000 def solution(n_limit: int = N) -> int: + """ + Define n count list and loop over delta, y to get the counts, then check + which n has count == 1. + + >>> solution(3) + 0 + >>> solution(10) + 2 + >>> solution(110) + 26 + """ n_sol = [0] * n_limit for delta in range(1, (n_limit + 1) // 4): From 58485a113b79409a0ff944df38d1e82fad408899 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 01:48:55 +0300 Subject: [PATCH 03/13] Update sol1.py --- project_euler/problem_136/sol1.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index 53330357b62c..9f119efe5726 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -3,6 +3,14 @@ Singleton Difference +The positive integers, x, y, and z, are consecutive terms of an arithmetic progression. +Given that n is a positive integer, the equation, x^2 - y^2 - z^2 = n, has exactly one solution when n = 20: + 13^2 - 10^2 - 7^2 = 20. + +In fact there are twenty-five values of n below one hundred for which the equation has a unique solution. + +How many values of n less than fifty million have exactly one solution? + By change of variables x = y + delta @@ -10,21 +18,14 @@ The expression can be rewritten: -x^2 - y^2 - z^2 = y*(4*delta - y) = n +x^2 - y^2 - z^2 = y * (4 * delta - y) = n The algorithm loops over delta and y, which is restricted in upper and lower limits, to count how many solutions each n has. In the end it is counted how many n's have one solution. - - ->>> solution(100) -25 """ -N = 50000000 - - -def solution(n_limit: int = N) -> int: +def solution(n_limit: int = 50 * 10**6) -> int: """ Define n count list and loop over delta, y to get the counts, then check which n has count == 1. @@ -33,6 +34,8 @@ def solution(n_limit: int = N) -> int: 0 >>> solution(10) 2 + >>> solution(100) + 25 >>> solution(110) 26 """ From d14f6b85e360ae595802e40ec1cc0b833c150696 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 17 Apr 2025 22:49:19 +0000 Subject: [PATCH 04/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_136/sol1.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index 9f119efe5726..b8f7e6cc2139 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -25,6 +25,7 @@ In the end it is counted how many n's have one solution. """ + def solution(n_limit: int = 50 * 10**6) -> int: """ Define n count list and loop over delta, y to get the counts, then check From 28c86013debe775f5d77286b6f147c6c42765ee7 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 01:50:38 +0300 Subject: [PATCH 05/13] Update sol1.py --- project_euler/problem_136/sol1.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index b8f7e6cc2139..d32c466aa078 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -4,10 +4,12 @@ Singleton Difference The positive integers, x, y, and z, are consecutive terms of an arithmetic progression. -Given that n is a positive integer, the equation, x^2 - y^2 - z^2 = n, has exactly one solution when n = 20: - 13^2 - 10^2 - 7^2 = 20. +Given that n is a positive integer, the equation, x^2 - y^2 - z^2 = n, +has exactly one solution when n = 20: + 13^2 - 10^2 - 7^2 = 20. -In fact there are twenty-five values of n below one hundred for which the equation has a unique solution. +In fact there are twenty-five values of n below one hundred for which +the equation has a unique solution. How many values of n less than fifty million have exactly one solution? From 1fd3f75cce58251c2a580044bdfb01e461fa4ef5 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 01:51:53 +0300 Subject: [PATCH 06/13] Update sol1.py --- project_euler/problem_136/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index d32c466aa078..ff7036c0ee60 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -27,7 +27,6 @@ In the end it is counted how many n's have one solution. """ - def solution(n_limit: int = 50 * 10**6) -> int: """ Define n count list and loop over delta, y to get the counts, then check From 89887e60dfd2569e5ebfbf559718b23f0c98dc25 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 17 Apr 2025 22:52:15 +0000 Subject: [PATCH 07/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_136/sol1.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index ff7036c0ee60..d32c466aa078 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -27,6 +27,7 @@ In the end it is counted how many n's have one solution. """ + def solution(n_limit: int = 50 * 10**6) -> int: """ Define n count list and loop over delta, y to get the counts, then check From 6639f180d4b1a204864285eb873233bc8ece59a1 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 01:58:02 +0300 Subject: [PATCH 08/13] Update sol1.py --- project_euler/problem_136/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index d32c466aa078..a3bedde555bb 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -44,7 +44,7 @@ def solution(n_limit: int = 50 * 10**6) -> int: """ n_sol = [0] * n_limit - for delta in range(1, (n_limit + 1) // 4): + for delta in range(1, n_limit // 4 + 1): for y in range(4 * delta - 1, delta, -1): n = y * (4 * delta - y) if n >= n_limit: From 8f23ddc977f92beb933ada99f0ee69de5b120748 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 02:02:28 +0300 Subject: [PATCH 09/13] Update sol1.py --- project_euler/problem_136/sol1.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index a3bedde555bb..69c7d5ba3a6d 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -33,10 +33,8 @@ def solution(n_limit: int = 50 * 10**6) -> int: Define n count list and loop over delta, y to get the counts, then check which n has count == 1. - >>> solution(3) - 0 - >>> solution(10) - 2 + >>> solution(21) + 1 >>> solution(100) 25 >>> solution(110) From 1bb57c21bd355b260d7a947cb685c4b15cdf7a2d Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 02:04:47 +0300 Subject: [PATCH 10/13] Update sol1.py --- project_euler/problem_136/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index 69c7d5ba3a6d..23595c975858 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -42,7 +42,7 @@ def solution(n_limit: int = 50 * 10**6) -> int: """ n_sol = [0] * n_limit - for delta in range(1, n_limit // 4 + 1): + for delta in range(1, (n_limit + 1) // 4 + 1): for y in range(4 * delta - 1, delta, -1): n = y * (4 * delta - y) if n >= n_limit: From f156f4546cf70c517a12d19bfc855f63f468dfb5 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 02:05:18 +0300 Subject: [PATCH 11/13] Update sol1.py --- project_euler/problem_136/sol1.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index 23595c975858..e1137cf2207b 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -33,6 +33,10 @@ def solution(n_limit: int = 50 * 10**6) -> int: Define n count list and loop over delta, y to get the counts, then check which n has count == 1. + >>> solution(3) + 0 + >>> solution(10) + 2 >>> solution(21) 1 >>> solution(100) From 36f7d2767f902dca11bc59e417edd8f7b096485f Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 02:10:09 +0300 Subject: [PATCH 12/13] Update sol1.py --- project_euler/problem_136/sol1.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index e1137cf2207b..90ec033d1ae2 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -36,9 +36,7 @@ def solution(n_limit: int = 50 * 10**6) -> int: >>> solution(3) 0 >>> solution(10) - 2 - >>> solution(21) - 1 + 3 >>> solution(100) 25 >>> solution(110) From aa4618fb56dc812ecbb502fc91df55147e4ef036 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 18 Apr 2025 02:13:34 +0300 Subject: [PATCH 13/13] Update sol1.py --- project_euler/problem_136/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_136/sol1.py b/project_euler/problem_136/sol1.py index 90ec033d1ae2..688a9a5d7f24 100644 --- a/project_euler/problem_136/sol1.py +++ b/project_euler/problem_136/sol1.py @@ -40,7 +40,7 @@ def solution(n_limit: int = 50 * 10**6) -> int: >>> solution(100) 25 >>> solution(110) - 26 + 27 """ n_sol = [0] * n_limit