{"id":1029529,"date":"2024-12-31T11:09:04","date_gmt":"2024-12-31T03:09:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1029529.html"},"modified":"2024-12-31T11:09:07","modified_gmt":"2024-12-31T03:09:07","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e6%95%b0%e5%88%86%e8%a7%a3%e8%b4%a8%e5%9b%a0%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1029529.html","title":{"rendered":"\u5982\u4f55\u5c06python\u6570\u5206\u89e3\u8d28\u56e0\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/89f4ae08-3a1b-46b0-bc1e-25e379df14a4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5c06python\u6570\u5206\u89e3\u8d28\u56e0\u6570\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5c06Python\u6570\u5206\u89e3\u8d28\u56e0\u6570<\/strong><\/p>\n<\/p>\n<p><p><strong>Python\u6570\u5206\u89e3\u8d28\u56e0\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u8bd5\u9664\u6cd5\u3001\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u3001Pollard&#39;s rho\u7b97\u6cd5\u3001SymPy\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u8bd5\u9664\u6cd5\u662f\u6700\u57fa\u7840\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8bd5\u9664\u6cd5\uff0c\u5e76\u5728\u540e\u6587\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bd5\u9664\u6cd5<\/h3>\n<\/p>\n<p><p>\u8bd5\u9664\u6cd5\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u63a5\u7684\u8d28\u56e0\u6570\u5206\u89e3\u65b9\u6cd5\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\u4ece\u5c0f\u5230\u5927\u4f9d\u6b21\u5c1d\u8bd5\u9664\u4ee5\u53ef\u80fd\u7684\u8d28\u6570\uff0c\u76f4\u5230\u5269\u4e0b\u7684\u6570\u4e3a1\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\u4ece2\u5f00\u59cb\uff0c\u9010\u4e00\u5c1d\u8bd5\u9664\u4ee5\u5f53\u524d\u6570\u5b57\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u6570\u5b57\u80fd\u88ab\u6574\u9664\uff0c\u5219\u8bb0\u5f55\u8fd9\u4e2a\u8d28\u6570\uff0c\u5e76\u5c06\u5f53\u524d\u6570\u5b57\u9664\u4ee5\u8fd9\u4e2a\u8d28\u6570\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa42\uff0c\u76f4\u5230\u5f53\u524d\u6570\u5b57\u4e0d\u80fd\u88ab\u6574\u9664\uff0c\u7136\u540e\u5c1d\u8bd5\u4e0b\u4e00\u4e2a\u8d28\u6570\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa41-3\uff0c\u76f4\u5230\u5f53\u524d\u6570\u5b57\u7b49\u4e8e1\u3002<\/li>\n<\/ol>\n<p><h4>1.2 Python\u5b9e\u73b0<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def prime_factors(n):<\/p>\n<p>    i = 2<\/p>\n<p>    factors = []<\/p>\n<p>    while i * i &lt;= n:<\/p>\n<p>        if n % i:<\/p>\n<p>            i += 1<\/p>\n<p>        else:<\/p>\n<p>            n \/\/= i<\/p>\n<p>            factors.append(i)<\/p>\n<p>    if n &gt; 1:<\/p>\n<p>        factors.append(n)<\/p>\n<p>    return factors<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>number = 100<\/p>\n<p>print(f&quot;{number} \u7684\u8d28\u56e0\u6570\u4e3a: {prime_factors(number)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5<\/h3>\n<\/p>\n<p><p>\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u662f\u4e00\u79cd\u7528\u4e8e\u627e\u51fa\u6240\u6709\u5c0f\u4e8e\u7b49\u4e8e\u7ed9\u5b9a\u6570\u7684\u8d28\u6570\u7684\u9ad8\u6548\u7b97\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u4f18\u5316\u8d28\u56e0\u6570\u5206\u89e3\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u57fa\u672c\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u8868\u793a\u4ece2\u5230\u7ed9\u5b9a\u6570\u7684\u6240\u6709\u6570\u5b57\u3002<\/li>\n<li>\u4ece\u7b2c\u4e00\u4e2a\u8d28\u65702\u5f00\u59cb\uff0c\u6807\u8bb0\u5176\u6240\u6709\u500d\u6570\u4e3a\u975e\u8d28\u6570\u3002<\/li>\n<li>\u627e\u5230\u4e0b\u4e00\u4e2a\u672a\u6807\u8bb0\u7684\u6570\u5b57\uff0c\u5c06\u5176\u6240\u6709\u500d\u6570\u6807\u8bb0\u4e3a\u975e\u8d28\u6570\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa42-3\uff0c\u76f4\u5230\u6570\u7ec4\u4e2d\u6240\u6709\u6570\u5b57\u90fd\u88ab\u5904\u7406\u3002<\/li>\n<\/ol>\n<p><h4>2.2 Python\u5b9e\u73b0<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def sieve_of_eratosthenes(limit):<\/p>\n<p>    primes = []<\/p>\n<p>    sieve = [True] * (limit + 1)<\/p>\n<p>    for num in range(2, limit + 1):<\/p>\n<p>        if sieve[num]:<\/p>\n<p>            primes.append(num)<\/p>\n<p>            for multiple in range(num * num, limit + 1, num):<\/p>\n<p>                sieve[multiple] = False<\/p>\n<p>    return primes<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>limit = 100<\/p>\n<p>print(f&quot;\u5c0f\u4e8e\u7b49\u4e8e {limit} \u7684\u8d28\u6570\u4e3a: {sieve_of_eratosthenes(limit)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pollard&#39;s rho\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>Pollard&#39;s rho\u7b97\u6cd5\u662f\u4e00\u79cd\u7528\u4e8e\u5927\u6574\u6570\u56e0\u6570\u5206\u89e3\u7684\u6982\u7387\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u5904\u7406\u8f83\u5927\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u57fa\u672c\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\u9009\u62e9\u4e00\u4e2a\u968f\u673a\u6570\u4f5c\u4e3a\u521d\u59cb\u503c\u3002<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u591a\u9879\u5f0f\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u8be5\u51fd\u6570\u751f\u6210\u4e00\u7cfb\u5217\u6570\u3002<\/li>\n<li>\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u627e\u51fa\u751f\u6210\u6570\u5217\u4e2d\u7684\u516c\u56e0\u5b50\u3002<\/li>\n<li>\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u76f4\u5230\u627e\u5230\u975e\u5e73\u51e1\u56e0\u5b50\u3002<\/li>\n<\/ol>\n<p><h4>3.2 Python\u5b9e\u73b0<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import math<\/p>\n<p>def pollards_rho(n):<\/p>\n<p>    if n % 2 == 0:<\/p>\n<p>        return 2<\/p>\n<p>    x = random.randint(2, n - 1)<\/p>\n<p>    y = x<\/p>\n<p>    c = random.randint(1, n - 1)<\/p>\n<p>    d = 1<\/p>\n<p>    while d == 1:<\/p>\n<p>        x = (x * x + c) % n<\/p>\n<p>        y = (y * y + c) % n<\/p>\n<p>        y = (y * y + c) % n<\/p>\n<p>        d = math.gcd(abs(x - y), n)<\/p>\n<p>    return d<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>number = 10403<\/p>\n<p>factor = pollards_rho(number)<\/p>\n<p>print(f&quot;{number} \u7684\u4e00\u4e2a\u975e\u5e73\u51e1\u56e0\u5b50\u4e3a: {factor}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001SymPy\u5e93<\/h3>\n<\/p>\n<p><p>SymPy\u662fPython\u7684\u4e00\u4e2a\u7b26\u53f7\u6570\u5b66\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u6570\u5b66\u5de5\u5177\uff0c\u5305\u62ec\u8d28\u56e0\u6570\u5206\u89e3\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b89\u88c5SymPy<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sympy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528SymPy\u8fdb\u884c\u8d28\u56e0\u6570\u5206\u89e3<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import factorint<\/p>\n<p>def sympy_prime_factors(n):<\/p>\n<p>    return factorint(n)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>number = 100<\/p>\n<p>print(f&quot;{number} \u7684\u8d28\u56e0\u6570\u4e3a: {sympy_prime_factors(number)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8d28\u56e0\u6570\u5206\u89e3\u5728\u8bb8\u591a\u6570\u5b66\u548c\u8ba1\u7b97\u95ee\u9898\u4e2d\u90fd\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u51e0\u79cd\u5e38\u7528\u7684\u8d28\u56e0\u6570\u5206\u89e3\u65b9\u6cd5\uff0c\u5305\u62ec\u8bd5\u9664\u6cd5\u3001\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u3001Pollard&#39;s rho\u7b97\u6cd5\u548cSymPy\u5e93\u3002<strong>\u8bd5\u9664\u6cd5<\/strong>\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u6570\u5b57\uff0c<strong>\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5<\/strong>\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u8d28\u6570\u5217\u8868\u4ee5\u4f9b\u5176\u4ed6\u7b97\u6cd5\u4f7f\u7528\uff0c<strong>Pollard&#39;s rho\u7b97\u6cd5<\/strong>\u9002\u7528\u4e8e\u5904\u7406\u8f83\u5927\u7684\u6570\u5b57\uff0c\u800c<strong>SymPy\u5e93<\/strong>\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u73b0\u6210\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u5b57\u7684\u89c4\u6a21\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u4ee5\u83b7\u5f97\u6700\u4f73\u6027\u80fd\u548c\u7ed3\u679c\u3002\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u7684\u7406\u89e3\u548c\u638c\u63e1\uff0c\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u8fdb\u884c\u8d28\u56e0\u6570\u5206\u89e3\uff0c\u5e76\u5e94\u7528\u4e8e\u5404\u79cd\u5b9e\u9645\u95ee\u9898\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u8d28\u56e0\u6570\u5206\u89e3\u7684\u57fa\u672c\u65b9\u6cd5\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u8d28\u56e0\u6570\u5206\u89e3\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u91c7\u7528\u8bd5\u9664\u6cd5\u3002\u53ef\u4ee5\u4ece2\u5f00\u59cb\uff0c\u9010\u6b65\u68c0\u67e5\u6bcf\u4e2a\u6574\u6570\u662f\u5426\u80fd\u6574\u9664\u76ee\u6807\u6570\u3002\u5982\u679c\u80fd\u6574\u9664\uff0c\u5c31\u5c06\u8fd9\u4e2a\u6574\u6570\u8bb0\u5f55\u4e3a\u4e00\u4e2a\u8d28\u56e0\u6570\uff0c\u5e76\u7ee7\u7eed\u5bf9\u5546\u8fdb\u884c\u76f8\u540c\u7684\u68c0\u67e5\uff0c\u76f4\u5230\u5546\u4e3a1\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u6240\u6709\u7684\u8d28\u56e0\u6570\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u8d28\u56e0\u6570\u5206\u89e3\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f8b\u5982SymPy\u548cNumPy\uff0c\u6765\u7b80\u5316\u8d28\u56e0\u6570\u5206\u89e3\u7684\u8fc7\u7a0b\u3002SymPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u8d28\u56e0\u6570\u5206\u89e3\u51fd\u6570<code>factorint()<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u663e\u793a\u6bcf\u4e2a\u8d28\u56e0\u6570\u53ca\u5176\u5bf9\u5e94\u7684\u6307\u6570\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u6548\u7387\uff0c\u8fd8\u51cf\u5c11\u4e86\u624b\u52a8\u7f16\u5199\u7b97\u6cd5\u7684\u590d\u6742\u6027\u3002<\/p>\n<p><strong>\u8d28\u56e0\u6570\u5206\u89e3\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u54ea\u4e9b\u91cd\u8981\u6027\uff1f<\/strong><br \/>\u8d28\u56e0\u6570\u5206\u89e3\u5728\u8bb8\u591a\u9886\u57df\u90fd\u5177\u6709\u91cd\u8981\u610f\u4e49\uff0c\u4f8b\u5982\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u8d28\u56e0\u6570\u7684\u96be\u4ee5\u5206\u89e3\u6027\u662f\u5b89\u5168\u6027\u7684\u91cd\u8981\u57fa\u7840\u3002\u6b64\u5916\uff0c\u5728\u6570\u8bba\u3001\u6570\u636e\u52a0\u5bc6\u3001\u7b97\u6cd5\u8bbe\u8ba1\u7b49\u65b9\u9762\uff0c\u8d28\u56e0\u6570\u5206\u89e3\u7684\u7814\u7a76\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6570\u7684\u6027\u8d28\u548c\u89e3\u51b3\u590d\u6742\u95ee\u9898\u3002\u8fd9\u4f7f\u5f97\u638c\u63e1\u8d28\u56e0\u6570\u5206\u89e3\u4e0d\u4ec5\u6709\u52a9\u4e8e\u7f16\u7a0b\u6280\u5de7\u7684\u63d0\u5347\uff0c\u4e5f\u80fd\u6269\u5c55\u6570\u5b66\u548c\u8ba1\u7b97\u673a\u79d1\u5b66\u7684\u5e94\u7528\u89c6\u91ce\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5c06Python\u6570\u5206\u89e3\u8d28\u56e0\u6570 Python\u6570\u5206\u89e3\u8d28\u56e0\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u8bd5\u9664\u6cd5\u3001\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u3001Pollard&#038;# [&hellip;]","protected":false},"author":3,"featured_media":1029536,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029529"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1029529"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029529\/revisions"}],"predecessor-version":[{"id":1029540,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029529\/revisions\/1029540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1029536"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1029529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1029529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1029529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}