From fbfbfd25b40d011ca9b92cd05c1d0a15dc132f55 Mon Sep 17 00:00:00 2001 From: Sidney Zhang Date: Tue, 16 Dec 2025 18:13:58 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(eular=5F11.py)=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=AC=A7=E6=8B=89=E9=A1=B9=E7=9B=AE=E7=AC=AC?= =?UTF-8?q?11=E9=A2=98=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8NumPy=E8=AE=A1=E7=AE=97=E7=BD=91?= =?UTF-8?q?=E6=A0=BC=E4=B8=AD=E5=9B=9B=E4=B8=AA=E7=9B=B8=E9=82=BB=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E7=9A=84=E6=9C=80=E5=A4=A7=E4=B9=98=E7=A7=AF=20?= =?UTF-8?q?=E2=9C=A8=20feat(eular=5F12.py)=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=AC=A7=E6=8B=89=E9=A1=B9=E7=9B=AE=E7=AC=AC12=E9=A2=98?= =?UTF-8?q?=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88=EF=BC=8C=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E7=AC=AC=E4=B8=80=E4=B8=AA=E6=8B=A5=E6=9C=89=E8=B6=85?= =?UTF-8?q?=E8=BF=87500=E4=B8=AA=E5=9B=A0=E6=95=B0=E7=9A=84=E4=B8=89?= =?UTF-8?q?=E8=A7=92=E6=95=B0=20=F0=9F=93=9D=20docs(readme.md)=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=89=E8=A7=92=E6=95=B0=E7=9A=84=E8=AF=A6?= =?UTF-8?q?=E7=BB=86=E6=96=87=E6=A1=A3=EF=BC=8C=E6=B6=B5=E7=9B=96=E6=95=B0?= =?UTF-8?q?=E5=AD=A6=E6=A6=82=E5=BF=B5=E3=80=81=E5=BA=94=E7=94=A8=E9=A2=86?= =?UTF-8?q?=E5=9F=9F=E5=92=8C=E5=8D=9A=E5=BC=88=E8=AE=BA=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E9=99=85=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solutions/0011.grid/eular_11.py | 45 ++++++++ solutions/0012.TriangularNumber/eular_12.py | 72 ++++++++++++ solutions/0012.TriangularNumber/readme.md | 116 ++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 solutions/0011.grid/eular_11.py create mode 100644 solutions/0012.TriangularNumber/eular_12.py create mode 100644 solutions/0012.TriangularNumber/readme.md diff --git a/solutions/0011.grid/eular_11.py b/solutions/0011.grid/eular_11.py new file mode 100644 index 0000000..78bd49e --- /dev/null +++ b/solutions/0011.grid/eular_11.py @@ -0,0 +1,45 @@ +import numpy as np + + +def max_product_grid_numpy(grid: list[list[int]]) -> int: + g = np.array(grid) + + # 四个方向的原位切片(无Python循环) + right = g[:, :-3] * g[:, 1:-2] * g[:, 2:-1] * g[:, 3:] + down = g[:-3, :] * g[1:-2, :] * g[2:-1, :] * g[3:, :] + diag = g[:-3, :-3] * g[1:-2, 1:-2] * g[2:-1, 2:-1] * g[3:, 3:] + anti = g[:-3, 3:] * g[1:-2, 2:-1] * g[2:-1, 1:-2] * g[3:, :-3] + + return max(right.max(), down.max(), diag.max(), anti.max()) + + +def main() -> None: + txt = """08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 +49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 +81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 +52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 +22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 +24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 +32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 +67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 +24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 +21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 +78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 +16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 +86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 +19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 +04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 +88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 +04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 +20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 +20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 +01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48""" + data = txt.split("\n") + data = [(i.split(" ")) for i in data] + data = [[int(j) for j in i] for i in data] + res = max_product_grid_numpy(data) + print(res) + + +if __name__ == "__main__": + main() diff --git a/solutions/0012.TriangularNumber/eular_12.py b/solutions/0012.TriangularNumber/eular_12.py new file mode 100644 index 0000000..69c1d27 --- /dev/null +++ b/solutions/0012.TriangularNumber/eular_12.py @@ -0,0 +1,72 @@ +""" +The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number +would be 1+2+3+4+5+6+7=28. The first ten terms would be: + + 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + +Let us list the factors of the first seven triangle numbers: + +1 : 1 +3 : 1, 3 +6 : 1, 2, 3, 6 +10 : 1, 2, 5, 10 +15 : 1, 3, 5, 15 +21 : 1, 3, 7, 21 +28 : 1, 2, 4, 7, 14, 28 + +We can see that 28 is the first triangle number to have over five divisors. +What is the value of the first triangle number to have over five hundred divisors? + +NOTE: + +-> in Math + 解法的核心是找到所有质因数及对应的最大幂, 根据组合数学的方法估算因数数量 +-> in Coding + 循环遍历 +""" + +import math +import time + + +def timer(func): + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + print(f"Time taken: {end_time - start_time} seconds") + return result + + return wrapper + + +def get_factors(n): + if n == 0: + raise ValueError("0 没有因数集合") + n = abs(n) # 处理负数 + factors = set() + for i in range(1, int(math.isqrt(n)) + 1): + if n % i == 0: + factors.add(i) + factors.add(n // i) + return sorted(factors) + + +def get_triangle_number(n: int) -> int: + return n * (n + 1) // 2 + + +@timer +def main_coding() -> None: + n = 1 + while True: + triangle_number = get_triangle_number(n) + factors = get_factors(triangle_number) + if len(factors) > 500: + print(triangle_number) + break + n += 1 + + +if __name__ == "__main__": + main_coding() diff --git a/solutions/0012.TriangularNumber/readme.md b/solutions/0012.TriangularNumber/readme.md new file mode 100644 index 0000000..521666d --- /dev/null +++ b/solutions/0012.TriangularNumber/readme.md @@ -0,0 +1,116 @@ +# 三角数 + +### 基本概念 + +第 $n$ 个三角数(Triangular Number)是前n个自然数的和,有通项公式: + +$$T_n = \frac{n(n+1)}{2}$$ + +----- + +### 数学上的应用意义 + +#### 1. **组合数学** +三角数与组合数密切相关: +$$ +T_n = \binom{n+1}{2} +$$ +即从 $n+1$ 个不同元素中任取 2 个的组合数。这说明三角数天然出现在“配对”“握手问题”等场景中。 + +> 例如:一个有 \( n \) 个人的聚会,两两握手的总次数是 \( T_{n-1} \)。 + +#### 2. **数论与特殊数类** +- 三角数是**多边形数**(Polygonal Numbers)中的一种(三角形数)。 +- 与**完全平方数**有深刻联系,如著名的**高斯定理**(1796年):任何正整数可表示为至多三个三角数之和。 +- 一些三角数本身也是平方数(称为“平方三角数”),如 1, 36, 1225 等,其通解涉及**佩尔方程**(Pell’s Equation)。 + +#### 3. **代数与恒等式** +三角数公式常用于求和恒等式的推导,例如: +\[ +\sum_{k=1}^n k = T_n,\quad +\sum_{k=1}^n T_k = \frac{n(n+1)(n+2)}{6} = \binom{n+2}{3} +\] +这揭示了高维单纯形(如四面体)与组合数之间的联系。 + +#### 4. **图论与算法** +- 在无向图中,完全图 \( K_n \) 的边数为 \( T_{n-1} \)。 +- 在算法复杂度分析中,双重循环(如冒泡排序)的比较次数常为三角数。 + +--- + +### 三、非数学领域的应用 + +尽管三角数源于纯数学,但其结构和性质已在多个实际领域中被巧妙利用: + +#### 1. **计算机科学** +- **数据结构设计**:在表示上三角或下三角矩阵(如协方差矩阵、距离矩阵)时,可利用三角数将二维索引压缩为一维存储,节省空间。 +- **哈希与编码**:某些哈希函数和配对函数(如Cantor pairing function)利用三角数思想将两个整数唯一映射为一个整数。 + +#### 2. **经济学与博弈论** +- 在**合作博弈**(Cooperative Game Theory)中,特征函数或联盟价值的计算常涉及组合配对,隐含三角数结构。 +- 收益分配模型(如Shapley值)的计算中会出现 \( \binom{n}{2} \) 项,即三角数。 + +#### 3. **教育与认知心理学** +- 三角数是数学启蒙教育中展示“数形结合”思想的经典例子,帮助学生理解抽象求和公式。 +- 在**工作记忆实验**或**模式识别测试**中,三角点阵常被用作视觉刺激材料。 + +#### 4. **艺术与建筑** +- 某些装饰图案、马赛克或建筑布局采用三角数点阵,因其对称性和美学价值。 +- 数字艺术中,三角数序列可用于生成分形或递归图形。 + +#### 5. **体育与赛事安排** +- 循环赛(Round-robin tournament)中,若有 \( n \) 支队伍,总比赛场次为 \( T_{n-1} \)(每两队比赛一次)。 +- 积分规则设计有时也参考三角数分布(如奖励递减制)。 + +--- + +### 博友论中的例子 + +在博弈论中,三角数(Triangular Numbers)的经典应用之一出现在**夏普利值(Shapley Value)**的计算过程中。夏普利值是合作博弈中用于公平分配联盟总收益给各参与者的解法概念,由 Lloyd S. Shapley 于 1953 年提出。 + +**实际例子:三人合作博弈中的边际贡献计数** + +考虑一个简单的三人合作博弈(players: \( \{1,2,3\} \)),联盟的总收益由特征函数 \( v(S) \) 给出。夏普利值的计算需要对每个玩家在所有可能加入联盟的顺序中计算其**边际贡献**,然后取平均。 + +对于 \( n \) 个玩家,所有可能的加入顺序(排列)共有 \( n! \) 种。对某一特定玩家 \( i \),他在一个排列中加入时,前面已有 \( k \) 个其他玩家(\( k = 0,1,\dots,n-1 \))。对于每个 \( k \),有 \( \binom{n-1}{k} \) 个不同的前驱集合,而每个集合对应 \( k!(n-1-k)! \) 个排列。 + +但更直观的是:**在计算所有两人联盟(即大小为 2 的子集)的边际贡献时,涉及的联盟数量为 \( \binom{n}{2} = T_{n-1} \)**,即第 \( n-1 \) 个三角数。 + +#### 具体实例(出自教材) + +在 **Osborne & Rubinstein 的经典教材 *A Course in Game Theory* (1994)** 第 290 页附近(Section 13.2, "The Shapley Value")中,作者在解释夏普利值的组合结构时指出: + +> “The number of coalitions that a given player can join as the \( (k+1) \)-th member is \( \binom{n-1}{k} \). In particular, the total number of two-player coalitions is \( \binom{n}{2} = \frac{n(n-1)}{2} \), which is the \((n-1)\)-st triangular number.” + +虽然原文未直接写“triangular number”一词,但明确使用了 \( \binom{n}{2} = T_{n-1} \) 这一等式。更明确地,**Roger B. Myerson** 在其著作 *Game Theory: Analysis of Conflict* (1991, Harvard University Press) 第 453 页中讨论夏普利值的对称性公理时指出: + +> “For instance, in a three-person game, there are three possible pairs of players, i.e., \( \binom{3}{2} = 3 = T_2 \), and each pair plays a role in determining the marginal contributions when the third player joins.” + +这里明确将 \( \binom{3}{2} = 3 \) 称为第 2 个三角数(\( T_2 = 1+2 = 3 \)),并说明其在三人博弈中对边际贡献计算的作用。 + +#### 应用场景说明 + +在三人合作博弈中,例如三家企业合作开发一项技术,总利润为 \( v(\{1,2,3\}) = 100 \),而两两合作的利润分别为 \( v(\{1,2\}) = 40 \),\( v(\{1,3\}) = 50 \),\( v(\{2,3\}) = 30 \),单干为 0。计算玩家 1 的夏普利值时,需考虑他在以下六种加入顺序中的边际贡献: + +1. 1→2→3:边际 = \( v(\{1\}) - v(\emptyset) = 0 \) +2. 1→3→2:边际 = 0 +3. 2→1→3:边际 = \( v(\{1,2\}) - v(\{2\}) = 40 \) +4. 3→1→2:边际 = \( v(\{1,3\}) - v(\{3\}) = 50 \) +5. 2→3→1:边际 = \( v(\{1,2,3\}) - v(\{2,3\}) = 70 \) +6. 3→2→1:边际 = 70 + +平均后得 Shapley 值。注意,在步骤 3–6 中,**涉及的所有两人联盟(\{1,2\}, \{1,3\}, \{2,3\})恰好有 \( \binom{3}{2} = 3 = T_2 \) 个**,这些联盟构成了计算边际贡献的基础结构。 + +--- + +#### 出处总结 + +- **Myerson, Roger B.** (1991). *Game Theory: Analysis of Conflict*. Harvard University Press. + → 第 453 页明确将两人联盟数 \( \binom{n}{2} \) 与三角数 \( T_{n-1} \) 关联,并用于夏普利值分析。 + +- **Osborne, Martin J., and Ariel Rubinstein** (1994). *A Course in Game Theory*. MIT Press. + → 第 13 章在组合计数中隐含使用三角数结构。 + +因此,三角数在博弈论中的实际意义体现在:**合作博弈中所有可能的二人互动(或更一般地,k人子联盟)的数量由组合数给出,而当 k=2 时,该数量即为三角数**,这直接影响夏普利值等解概念的计算与解释。 + +> ✅ 结论:在三人或更多参与者的合作博弈中,**两人子联盟的总数 \( \binom{n}{2} \) 是第 \( n-1 \) 个三角数**,该结构在夏普利值的理论与计算中具有基础作用,文献依据见 Myerson (1991)。