From 55a9a415cfcccad595054bf6a9d4a279abc0a3b2 Mon Sep 17 00:00:00 2001 From: mindaugl Date: Tue, 8 Apr 2025 15:11:56 +0800 Subject: [PATCH 1/2] 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/2] 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() = }")