From 55a9a415cfcccad595054bf6a9d4a279abc0a3b2 Mon Sep 17 00:00:00 2001 From: mindaugl Date: Tue, 8 Apr 2025 15:11:56 +0800 Subject: [PATCH 1/8] Add solution for the Euler project problem 164. --- project_euler/problem_164/__init__.py | 0 project_euler/problem_164/sol1.py | 58 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 project_euler/problem_164/__init__.py create mode 100644 project_euler/problem_164/sol1.py diff --git a/project_euler/problem_164/__init__.py b/project_euler/problem_164/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_164/sol1.py b/project_euler/problem_164/sol1.py new file mode 100644 index 000000000000..97cb085e3508 --- /dev/null +++ b/project_euler/problem_164/sol1.py @@ -0,0 +1,58 @@ +""" +Project Euler Problem 164: https://projecteuler.net/problem=164 + +Three Consecutive Digital Sum Limit + +How many 20 digit numbers n (without any leading zero) exist such that no three +consecutive digits of n have a sum greater than 9? + +Brute-force recursive solution with caching of intermediate results. + +>>> solution(10) +21838806 +""" + + +def solve( + digit: int, prev: int, prev2: int, sum_max: int, first: bool, cache: dict[str, int] +) -> int: + """ + Solve for remaining 'digit' digits, with previous 'prev' number, and + previous-previous 'prev2' number, total sum of 'sum_max'. + Pass around 'cache' to store/reuse intermediate results. + + >>> solve(1, 0, 0, 9, True, {}) + 9 + >>> solve(1, 0, 0, 9, False, {}) + 10 + """ + if digit == 0: + return 1 + comb = 0 + cache_str = f"{digit},{prev},{prev2}" + if cache_str in cache: + return cache[cache_str] + for v in range(sum_max - prev - prev2 + 1): + if first and v == 0: + continue + comb += solve(digit - 1, v, prev, sum_max, False, cache) + cache[cache_str] = comb + return comb + + +def solution(n_digits: int = 20) -> int: + """ + Solves the problem for n_digits number of digits. + + >>> solution(2) + 45 + """ + sum_max = 9 + cache: dict[str, int] = {} + ans = solve(n_digits, 0, 0, sum_max, True, cache) + + return ans + + +if __name__ == "__main__": + print(f"{solution(10) = }") From 2b883d1be4ad3594a567f51698890a8ff2a2b3a6 Mon Sep 17 00:00:00 2001 From: mindaugl Date: Wed, 9 Apr 2025 16:18:45 +0800 Subject: [PATCH 2/8] Add solution for the Euler project problem 190. --- project_euler/problem_190/__init__.py | 0 project_euler/problem_190/sol1.py | 50 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 project_euler/problem_190/__init__.py create mode 100644 project_euler/problem_190/sol1.py diff --git a/project_euler/problem_190/__init__.py b/project_euler/problem_190/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_190/sol1.py b/project_euler/problem_190/sol1.py new file mode 100644 index 000000000000..1cc0aa667d4a --- /dev/null +++ b/project_euler/problem_190/sol1.py @@ -0,0 +1,50 @@ +""" +Project Euler Problem 190: https://projecteuler.net/problem=190 + +Maximising a Weighted Product + +Let S_m = (x_1, x_2, ..., x_m) be the m-tuple of positive real numbers with +x_1 + x_2 + ... + x_m = m for which P_m = x_1 * x_2^2 * ... * x_m^m is maximised. +For example, it can be verified that |_P_10_| = 4112 +(|__| is the integer part function). +Find Sum_{m=2}^15 = |_P_m_|. + +Solution: +- Fix x_1 = m - x_2 - ... - x_m. +- Calculate partial derivatives of P_m wrt the x_2, ..., x_m. This gives that + x_2 = 2 * x_1, x_3 = 3 * x_1, ..., x_m = m * x_1. +- Calculate partial second order derivatives of P_m wrt the x_2, ..., x_m. + By plugging in the values from the previous step, can verify that solution is maximum. + +>>> solution(5) +10 + +""" + + +def solution(n: int = 15) -> int: + """ + Calculate sum of P_m for m from 2 to n. + + >>> solution(2) + 1 + >>> solution(3) + 2 + >>> solution(4) + 4 + + """ + + ans = 0 + for m in range(2, n + 1): + x1 = 2 / (m + 1) + capital_p = 1.0 + for i in range(1, m + 1): + xi = i * x1 + capital_p *= pow(xi, i) + ans += int(capital_p) + return ans + + +if __name__ == "__main__": + print(f"{solution() = }") From cda3c1dc1f58cef6166bdc55ff6947e9e97c0fc5 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 5 May 2025 10:02:28 +0300 Subject: [PATCH 3/8] Delete project_euler/problem_164/sol1.py --- project_euler/problem_164/sol1.py | 58 ------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 project_euler/problem_164/sol1.py diff --git a/project_euler/problem_164/sol1.py b/project_euler/problem_164/sol1.py deleted file mode 100644 index 97cb085e3508..000000000000 --- a/project_euler/problem_164/sol1.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Project Euler Problem 164: https://projecteuler.net/problem=164 - -Three Consecutive Digital Sum Limit - -How many 20 digit numbers n (without any leading zero) exist such that no three -consecutive digits of n have a sum greater than 9? - -Brute-force recursive solution with caching of intermediate results. - ->>> solution(10) -21838806 -""" - - -def solve( - digit: int, prev: int, prev2: int, sum_max: int, first: bool, cache: dict[str, int] -) -> int: - """ - Solve for remaining 'digit' digits, with previous 'prev' number, and - previous-previous 'prev2' number, total sum of 'sum_max'. - Pass around 'cache' to store/reuse intermediate results. - - >>> solve(1, 0, 0, 9, True, {}) - 9 - >>> solve(1, 0, 0, 9, False, {}) - 10 - """ - if digit == 0: - return 1 - comb = 0 - cache_str = f"{digit},{prev},{prev2}" - if cache_str in cache: - return cache[cache_str] - for v in range(sum_max - prev - prev2 + 1): - if first and v == 0: - continue - comb += solve(digit - 1, v, prev, sum_max, False, cache) - cache[cache_str] = comb - return comb - - -def solution(n_digits: int = 20) -> int: - """ - Solves the problem for n_digits number of digits. - - >>> solution(2) - 45 - """ - sum_max = 9 - cache: dict[str, int] = {} - ans = solve(n_digits, 0, 0, sum_max, True, cache) - - return ans - - -if __name__ == "__main__": - print(f"{solution(10) = }") From d93526fa9dcc6c6b1fbd5ba7d5e86270f00267cc Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 5 May 2025 10:02:42 +0300 Subject: [PATCH 4/8] Delete project_euler/problem_164/__init__.py --- project_euler/problem_164/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 project_euler/problem_164/__init__.py diff --git a/project_euler/problem_164/__init__.py b/project_euler/problem_164/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 From c5c57529cf86505d1a3e89d91c0c2a91518f2367 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 5 May 2025 10:06:40 +0300 Subject: [PATCH 5/8] Update sol1.py --- project_euler/problem_190/sol1.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_190/sol1.py b/project_euler/problem_190/sol1.py index 1cc0aa667d4a..41a21d66941d 100644 --- a/project_euler/problem_190/sol1.py +++ b/project_euler/problem_190/sol1.py @@ -5,9 +5,11 @@ Let S_m = (x_1, x_2, ..., x_m) be the m-tuple of positive real numbers with x_1 + x_2 + ... + x_m = m for which P_m = x_1 * x_2^2 * ... * x_m^m is maximised. -For example, it can be verified that |_P_10_| = 4112 -(|__| is the integer part function). -Find Sum_{m=2}^15 = |_P_m_|. + +For example, it can be verified that |_ P_10 _| = 4112 +(|_ _| is the integer part function). + +Find Sum_{m=2}^15 = |_ P_m _|. Solution: - Fix x_1 = m - x_2 - ... - x_m. @@ -15,10 +17,6 @@ x_2 = 2 * x_1, x_3 = 3 * x_1, ..., x_m = m * x_1. - Calculate partial second order derivatives of P_m wrt the x_2, ..., x_m. By plugging in the values from the previous step, can verify that solution is maximum. - ->>> solution(5) -10 - """ @@ -32,7 +30,8 @@ def solution(n: int = 15) -> int: 2 >>> solution(4) 4 - + >>> solution(5) + 10 """ ans = 0 From 2ee900c34cc45b69c9280a35c13731d45be597ed Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 5 May 2025 10:07:51 +0300 Subject: [PATCH 6/8] Update sol1.py --- project_euler/problem_190/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_190/sol1.py b/project_euler/problem_190/sol1.py index 41a21d66941d..2185ef16b8a2 100644 --- a/project_euler/problem_190/sol1.py +++ b/project_euler/problem_190/sol1.py @@ -22,7 +22,7 @@ def solution(n: int = 15) -> int: """ - Calculate sum of P_m for m from 2 to n. + Calculate sum of |_ P_m _| for m from 2 to n. >>> solution(2) 1 From df249f2c63af94890fba55fbeeddf0ca5fabf120 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 5 May 2025 10:10:23 +0300 Subject: [PATCH 7/8] Update sol1.py --- project_euler/problem_190/sol1.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_190/sol1.py b/project_euler/problem_190/sol1.py index 2185ef16b8a2..324cb0d4c010 100644 --- a/project_euler/problem_190/sol1.py +++ b/project_euler/problem_190/sol1.py @@ -33,16 +33,15 @@ def solution(n: int = 15) -> int: >>> solution(5) 10 """ - - ans = 0 + total = 0 for m in range(2, n + 1): x1 = 2 / (m + 1) - capital_p = 1.0 + p = 1.0 for i in range(1, m + 1): xi = i * x1 - capital_p *= pow(xi, i) - ans += int(capital_p) - return ans + p *= xi ** i + total += int(p) + return total if __name__ == "__main__": From 746272941fc45f9d305da566fc52db4276f96898 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 5 May 2025 07:10:47 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_190/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_190/sol1.py b/project_euler/problem_190/sol1.py index 324cb0d4c010..b18d45be16b4 100644 --- a/project_euler/problem_190/sol1.py +++ b/project_euler/problem_190/sol1.py @@ -39,7 +39,7 @@ def solution(n: int = 15) -> int: p = 1.0 for i in range(1, m + 1): xi = i * x1 - p *= xi ** i + p *= xi**i total += int(p) return total