From 48f57bd443d4758721558207e7c4b6055888e300 Mon Sep 17 00:00:00 2001 From: Sidney Zhang Date: Wed, 17 Dec 2025 16:02:06 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(eular=5F12.py)=EF=BC=9A?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B4=A8=E6=95=B0=E6=B5=8B=E8=AF=95=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=B9=B6=E4=BF=AE=E5=A4=8D=E7=BB=84=E5=90=88=E6=95=B0?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91=20=E2=9C=A8=20feat(eular?= =?UTF-8?q?=5F13.py)=EF=BC=9A=E6=96=B0=E5=A2=9E=E8=B6=85=E5=A4=A7=E6=95=B4?= =?UTF-8?q?=E6=95=B0=E5=8A=A0=E6=B3=95=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= =?UTF-8?q?=20=F0=9F=93=9D=20docs(eular=5F13.md)=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=AE=97=E6=B3=95=E8=AF=B4=E6=98=8E=E6=96=87=E6=A1=A3?= =?UTF-8?q?=20=E2=9C=A8=20feat(eular=5F14.py)=EF=BC=9A=E6=96=B0=E5=A2=9ECo?= =?UTF-8?q?llatz=E5=BA=8F=E5=88=97=E6=9C=80=E9=95=BF=E9=93=BE=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=20=F0=9F=93=9D=20docs(eular=5F14.md)=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20=E2=9C=A8=20feat(eular=5F15.py)=EF=BC=9A=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E7=BD=91=E6=A0=BC=E8=B7=AF=E5=BE=84=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=95=B0=E5=AD=A6=E8=A7=A3=E6=B3=95=20=F0=9F=93=9D=20docs(eula?= =?UTF-8?q?r=5F15.md)=EF=BC=9A=E6=B7=BB=E5=8A=A0=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=95=B0=E5=AD=A6=E8=AF=A6=E7=BB=86=E8=AF=B4=E6=98=8E=20?= =?UTF-8?q?=E2=9C=A8=20feat(eular=5F16.py)=EF=BC=9A=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=B9=82=E6=95=B0=E5=AD=97=E5=92=8C=E8=AE=A1=E7=AE=97=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20=E2=9C=A8=20feat(eular=5F16.hs)=EF=BC=9A=E6=96=B0?= =?UTF-8?q?=E5=A2=9EHaskell=E7=89=88=E6=9C=AC=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solutions/0012.TriangularNumber/eular_12.py | 10 +- solutions/0013/eular_13.py | 152 ++++++++++++++++++++ solutions/0013/readme.md | 8 ++ solutions/0014.CollatzSeq/eular_14.py | 77 ++++++++++ solutions/0014.CollatzSeq/readme.md | 3 + solutions/0015.gridpath/eular_15.py | 21 +++ solutions/0015.gridpath/readme.md | 88 ++++++++++++ solutions/0016.PowerDigitSum/eular_16.hs | 14 ++ solutions/0016.PowerDigitSum/eular_16.py | 17 +++ 9 files changed, 386 insertions(+), 4 deletions(-) create mode 100644 solutions/0013/eular_13.py create mode 100644 solutions/0013/readme.md create mode 100644 solutions/0014.CollatzSeq/eular_14.py create mode 100644 solutions/0014.CollatzSeq/readme.md create mode 100644 solutions/0015.gridpath/eular_15.py create mode 100644 solutions/0015.gridpath/readme.md create mode 100644 solutions/0016.PowerDigitSum/eular_16.hs create mode 100644 solutions/0016.PowerDigitSum/eular_16.py diff --git a/solutions/0012.TriangularNumber/eular_12.py b/solutions/0012.TriangularNumber/eular_12.py index 458ed49..d12598f 100644 --- a/solutions/0012.TriangularNumber/eular_12.py +++ b/solutions/0012.TriangularNumber/eular_12.py @@ -27,7 +27,6 @@ NOTE: import math import random -import re import time from collections import Counter from functools import reduce @@ -74,7 +73,7 @@ def main_coding() -> None: n += 1 -def is_probable_prime(n: int, trials: int = 10) -> bool: +def is_probable_prime(n: int, trials: int = 20) -> bool: """Miller-Rabin素性测试(快速判断是否为质数)""" if n < 2: return False @@ -181,7 +180,8 @@ def get_prime_factors(n: int) -> dict[int | None, int]: def zuheshu(tl: list[int]) -> int: - return reduce(lambda x, y: x * y, tl) + xt = [x + 1 for x in tl] + return reduce(lambda x, y: x * y, xt) @timer @@ -200,5 +200,7 @@ def main_math() -> None: if __name__ == "__main__": + print("暴力试算:") main_coding() - # main_math() + print("质因数分解:") + main_math() diff --git a/solutions/0013/eular_13.py b/solutions/0013/eular_13.py new file mode 100644 index 0000000..68122c3 --- /dev/null +++ b/solutions/0013/eular_13.py @@ -0,0 +1,152 @@ +"""Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.""" + +import time + +txt = """37107287533902102798797998220837590246510135740250 +46376937677490009712648124896970078050417018260538 +74324986199524741059474233309513058123726617309629 +91942213363574161572522430563301811072406154908250 +23067588207539346171171980310421047513778063246676 +89261670696623633820136378418383684178734361726757 +28112879812849979408065481931592621691275889832738 +44274228917432520321923589422876796487670272189318 +47451445736001306439091167216856844588711603153276 +70386486105843025439939619828917593665686757934951 +62176457141856560629502157223196586755079324193331 +64906352462741904929101432445813822663347944758178 +92575867718337217661963751590579239728245598838407 +58203565325359399008402633568948830189458628227828 +80181199384826282014278194139940567587151170094390 +35398664372827112653829987240784473053190104293586 +86515506006295864861532075273371959191420517255829 +71693888707715466499115593487603532921714970056938 +54370070576826684624621495650076471787294438377604 +53282654108756828443191190634694037855217779295145 +36123272525000296071075082563815656710885258350721 +45876576172410976447339110607218265236877223636045 +17423706905851860660448207621209813287860733969412 +81142660418086830619328460811191061556940512689692 +51934325451728388641918047049293215058642563049483 +62467221648435076201727918039944693004732956340691 +15732444386908125794514089057706229429197107928209 +55037687525678773091862540744969844508330393682126 +18336384825330154686196124348767681297534375946515 +80386287592878490201521685554828717201219257766954 +78182833757993103614740356856449095527097864797581 +16726320100436897842553539920931837441497806860984 +48403098129077791799088218795327364475675590848030 +87086987551392711854517078544161852424320693150332 +59959406895756536782107074926966537676326235447210 +69793950679652694742597709739166693763042633987085 +41052684708299085211399427365734116182760315001271 +65378607361501080857009149939512557028198746004375 +35829035317434717326932123578154982629742552737307 +94953759765105305946966067683156574377167401875275 +88902802571733229619176668713819931811048770190271 +25267680276078003013678680992525463401061632866526 +36270218540497705585629946580636237993140746255962 +24074486908231174977792365466257246923322810917141 +91430288197103288597806669760892938638285025333403 +34413065578016127815921815005561868836468420090470 +23053081172816430487623791969842487255036638784583 +11487696932154902810424020138335124462181441773470 +63783299490636259666498587618221225225512486764533 +67720186971698544312419572409913959008952310058822 +95548255300263520781532296796249481641953868218774 +76085327132285723110424803456124867697064507995236 +37774242535411291684276865538926205024910326572967 +23701913275725675285653248258265463092207058596522 +29798860272258331913126375147341994889534765745501 +18495701454879288984856827726077713721403798879715 +38298203783031473527721580348144513491373226651381 +34829543829199918180278916522431027392251122869539 +40957953066405232632538044100059654939159879593635 +29746152185502371307642255121183693803580388584903 +41698116222072977186158236678424689157993532961922 +62467957194401269043877107275048102390895523597457 +23189706772547915061505504953922979530901129967519 +86188088225875314529584099251203829009407770775672 +11306739708304724483816533873502340845647058077308 +82959174767140363198008187129011875491310547126581 +97623331044818386269515456334926366572897563400500 +42846280183517070527831839425882145521227251250327 +55121603546981200581762165212827652751691296897789 +32238195734329339946437501907836945765883352399886 +75506164965184775180738168837861091527357929701337 +62177842752192623401942399639168044983993173312731 +32924185707147349566916674687634660915035914677504 +99518671430235219628894890102423325116913619626622 +73267460800591547471830798392868535206946944540724 +76841822524674417161514036427982273348055556214818 +97142617910342598647204516893989422179826088076852 +87783646182799346313767754307809363333018982642090 +10848802521674670883215120185883543223812876952786 +71329612474782464538636993009049310363619763878039 +62184073572399794223406235393808339651327408011116 +66627891981488087797941876876144230030984490851411 +60661826293682836764744779239180335110989069790714 +85786944089552990653640447425576083659976645795096 +66024396409905389607120198219976047599490197230297 +64913982680032973156037120041377903785566085089252 +16730939319872750275468906903707539413042652315011 +94809377245048795150954100921645863754710598436791 +78639167021187492431995700641917969777599028300699 +15368713711936614952811305876380278410754449733078 +40789923115535562561142322423255033685442488917353 +44889911501440648020369068063960672322193204149535 +41503128880339536053299340368006977710650566631954 +81234880673210146739058568557934581403627822703280 +82616570773948327592232845941706525094512325230608 +22918802058777319719839450180888072429661980811197 +77158542502016545090413245809786882778948721859617 +72107838435069186155435662884062257473692284509516 +20849603980134001723930671666823555245252804609722 +53503534226472524250874054075591789781264330331690""" + + +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 by_str_sum(data: list[str], left: int | None = None) -> str: + res: list[str] = [] + carry = 0 + max_len = max([len(i) for i in data]) + data = [i[::-1] for i in data] + for n in range(max_len): + carry = sum(int(i[n]) for i in data if len(i) > n) + carry + res.insert(0, str(carry % 10)) + carry //= 10 + while carry > 0: + res.insert(0, str(carry % 10)) + carry //= 10 + return "".join(res) if left is None else "".join(res[:left]) + + +@timer +def main() -> None: + data = txt.split("\n") + print(by_str_sum(data, left=10)) + + +def bigint_sum_py(data: list[str], left: int | None = None) -> str: + datax = [int(i) for i in data] + return str(sum(datax)) if left is None else str(sum(datax))[:left] + + +@timer +def main_py(): + data = txt.split("\n") + print(bigint_sum_py(data, left=10)) + + +if __name__ == "__main__": + main() + main_py() diff --git a/solutions/0013/readme.md b/solutions/0013/readme.md new file mode 100644 index 0000000..b2b9b27 --- /dev/null +++ b/solutions/0013/readme.md @@ -0,0 +1,8 @@ +# 超大整数加法 + +简单来说,就是按位运算,原始数据就按照string进行读取,每次计算一位数值, +就和小学时学加法一样计算,把加的数的最右位放入结果,左边的数存为当前的记录中, +计算完所有数之后,在把寄存结果处理一下,就可以了。 + +这也是一种暴力解决的方案。在实际运行中,和python自身实现的运算大整数运算大差不差吧, +但这个暴力方案时间稳定,这个倒是没错的。 diff --git a/solutions/0014.CollatzSeq/eular_14.py b/solutions/0014.CollatzSeq/eular_14.py new file mode 100644 index 0000000..637d6cf --- /dev/null +++ b/solutions/0014.CollatzSeq/eular_14.py @@ -0,0 +1,77 @@ +""" +The following iterative sequence is defined for the set of positive integers: + +n -> n/2 (is even) +n -> 3n + 1 (is odd) + +Using the rule above and starting with 13, we generate the following sequence: + 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 + +It can be seen that this sequence (starting at 13 and finishing at 1 ) contains 10 terms. +Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. + +Which starting number, under one million, produces the longest chain? +""" + +import time +from functools import cache + + +def timer(func): + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + print(f"{func.__name__} took {end_time - start_time:.6f} seconds") + return result + + return wrapper + + +def is_even(n: int) -> bool: + return n & 1 == 0 + + +def collatz_sequence(n: int) -> list[int]: + sequence = [n] + while sequence[-1] != 1: + if is_even(n): + n = n // 2 + else: + n = 3 * n + 1 + sequence.append(n) + return sequence + + +def longest_collatz_sequence(limit: int) -> int: + longest_length = 0 + longest_start = 0 + for i in range(1, limit + 1): + length = len(collatz_sequence(i)) + if length > longest_length: + longest_length = length + longest_start = i + return longest_start + + +@cache +def chain_length(n): + if n == 1: + return 0 + next_term = 3 * n + 1 if n % 2 else n // 2 + return chain_length(next_term) + 1 + + +@timer +def main_do() -> None: + print(longest_collatz_sequence(1000000)) + + +@timer +def main_cache() -> None: + print(max(range(1, 1000001), key=chain_length)) + + +if __name__ == "__main__": + main_do() + main_cache() diff --git a/solutions/0014.CollatzSeq/readme.md b/solutions/0014.CollatzSeq/readme.md new file mode 100644 index 0000000..2e2d425 --- /dev/null +++ b/solutions/0014.CollatzSeq/readme.md @@ -0,0 +1,3 @@ +暴力试算,虽然总能解决问题,但是要损耗时间。有的时候没什么好想法的时候,的确可以用来作为解法。 +但是从hash列表的角度来说,已经计算过的数值,可以用来避免重复计算,提高效率。 +这一点还是很值得记录的。 diff --git a/solutions/0015.gridpath/eular_15.py b/solutions/0015.gridpath/eular_15.py new file mode 100644 index 0000000..c1690a5 --- /dev/null +++ b/solutions/0015.gridpath/eular_15.py @@ -0,0 +1,21 @@ +""" +Starting in the top left corner of a 2 X 2 grid, +and only being able to move to the right and down, +there are exactly 6 routes to the bottom right corner. + +How many such routes are there through a 20 X 20 grid? +""" + +import math + + +def grid_paths(m: int, n: int) -> int: + return math.comb(m + n, m) + + +def main() -> None: + print(grid_paths(20, 20)) + + +if __name__ == "__main__": + main() diff --git a/solutions/0015.gridpath/readme.md b/solutions/0015.gridpath/readme.md new file mode 100644 index 0000000..7002153 --- /dev/null +++ b/solutions/0015.gridpath/readme.md @@ -0,0 +1,88 @@ +# 组合数学 + +### 我的错误 + +我搞了很久在想自己在这个问题上放的错误。原始错误是,m+n长度路径,应该选择(n-1)个情况进行选择,因为最右的点只有一个选择。 +真的是错得离谱了。正确理解应该是程度为m+n的长度上,选择在m或者n的次序上改变方向。 + +那我就整体回顾一下组合数学吧。 + +### 另一条路 + +[整数序列 A000984](https://oeis.org/A000984) 。这里也有这个的详细说明和讨论。 + +### 组合数学概念速览 + +组合数学(Combinatorics)是研究离散对象的计数、排列、组合及结构关系的数学分支,广泛应用于计算机科学、统计学、密码学等领域。其核心思想是**系统化地处理有限结构的排列与选择问题**,以下是其核心原理和关键定理的概述: + +#### **一、基本原理** +1. **加法原理** + 若完成一项任务有 \(m\) 种互斥的方法,另一种任务有 \(n\) 种方法,则选择其中一种任务的方法数为 \(m+n\)。 + **示例**:从3本数学书和4本历史书中选一本,有 \(3+4=7\) 种选法。 + +2. **乘法原理** + 若完成一项任务需连续执行多个步骤,第一步有 \(m\) 种方法,第二步有 \(n\) 种方法,则总方法数为 \(m \times n\)。 + **示例**:从3件上衣和2条裤子搭配,有 \(3 \times 2=6\) 种搭配。 + +#### **二、关键定理与公式** +1. **排列与组合** + - **排列**(顺序相关): + \(n\) 个不同元素取 \(r\) 个排列: + \[ + P(n,r) = \frac{n!}{(n-r)!} + \] + - **组合**(顺序无关): + \(n\) 个不同元素取 \(r\) 个组合: + \[ + C(n,r) = \binom{n}{r} = \frac{n!}{r!(n-r)!} + \] + +2. **二项式定理** + 展开二项式幂的关键公式: + \[ + (x+y)^n = \sum_{k=0}^{n} \binom{n}{k} x^{k} y^{n-k} + \] + 组合数 \(\binom{n}{k}\) 称为**二项式系数**,体现在杨辉三角(帕斯卡三角)中。 + +3. **鸽巢原理(抽屉原理)** + 将 \(n\) 个物品放入 \(m\) 个容器,若 \(n>m\),则至少有一个容器包含至少两个物品。 + **应用**:证明重复性存在(如生日悖论)。 + +4. **容斥原理** + 计算多个集合的并集大小,避免重复计数: + \[ + |A_1 \cup A_2 \cup \cdots \cup A_n| = \sum |A_i| - \sum |A_i \cap A_j| + \cdots + (-1)^{n+1} |A_1 \cap \cdots \cap A_n| + \] + **应用**:错排问题、素数计数。 + +5. **生成函数** + 将序列转化为形式幂级数,化组合问题为代数问题。例如,求组合数时: + \[ + (1+x)^n = \sum_{k=0}^{n} \binom{n}{k} x^k + \] + 普通生成函数用于计数,指数生成函数用于排列问题。 + +6. **卡特兰数** + 定义: + \[ + C_n = \frac{1}{n+1} \binom{2n}{n} + \] + 应用于括号匹配、二叉树计数、网格路径问题。 + +7. **斯特林数** + - **第二类斯特林数** \(S(n,k)\):将 \(n\) 个不同元素划分为 \(k\) 个非空集合的方法数。 + - **第一类斯特林数**:与轮换排列相关。 + +8. **波利亚计数定理** + 考虑对称性下的计数问题,用于染色、图形枚举等场景。 + +#### **三、核心思想** +- **一一对应**:将复杂计数映射到已知模型(如将组合问题转化为方程整数解个数)。 +- **递归与递推**:通过子问题构造关系式(如斐波那契数列、错排公式)。 +- **对称性与分类讨论**:利用对称性简化问题,对不重不漏的分类要求严格。 + +#### **四、应用领域** +- **算法设计**:分析算法复杂度(如动态规划状态数)。 +- **图论**:树、图的计数与结构分析。 +- **编码理论**:纠错码的构造与组合设计。 +- **概率论**:古典概型中的样本空间计数。 diff --git a/solutions/0016.PowerDigitSum/eular_16.hs b/solutions/0016.PowerDigitSum/eular_16.hs new file mode 100644 index 0000000..d4340ae --- /dev/null +++ b/solutions/0016.PowerDigitSum/eular_16.hs @@ -0,0 +1,14 @@ +import Data.Char (digitToInt) + +-- 计算 b^n 的各位数字之和 +powerDigitSum :: Integer -> Integer -> Integer +powerDigitSum b n = sum . map digitToInt . show $ b ^ n + +-- 示例使用 +main :: IO () +main = do + -- 测试用例 + print $ powerDigitSum 2 15 -- 32768 → 3+2+7+6+8 = 26 + print $ powerDigitSum 10 100 -- 1后跟100个0 → 1 + print $ powerDigitSum 3 3 -- 27 → 2+7 = 9 + print $ powerDigitSum 2 1000 diff --git a/solutions/0016.PowerDigitSum/eular_16.py b/solutions/0016.PowerDigitSum/eular_16.py new file mode 100644 index 0000000..8701a38 --- /dev/null +++ b/solutions/0016.PowerDigitSum/eular_16.py @@ -0,0 +1,17 @@ +""" +2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + +What is the sum of the digits of the number 2^1000 ? +""" + + +def power_digit_sum(n): + return sum(int(digit) for digit in str(2**n)) + + +def main(): + print(power_digit_sum(1000)) + + +if __name__ == "__main__": + main()