{"id":1007974,"date":"2024-12-27T10:58:35","date_gmt":"2024-12-27T02:58:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007974.html"},"modified":"2024-12-27T10:58:39","modified_gmt":"2024-12-27T02:58:39","slug":"python%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e7%b4%a0%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007974.html","title":{"rendered":"python\u8bed\u8a00\u5982\u4f55\u8868\u793a\u7d20\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083458\/855ca8e9-4b91-434f-be64-6c695b3aee53.webp\" alt=\"python\u8bed\u8a00\u5982\u4f55\u8868\u793a\u7d20\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8868\u793a\u7d20\u6570\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u53ea\u80fd\u88ab1\u548c\u81ea\u8eab\u6574\u9664\u6765\u5b9e\u73b0\u3002\u901a\u5e38\u4f7f\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u8bd5\u9664\u6cd5\u3001\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u3001\u4ee5\u53ca\u4e00\u4e9b\u4f18\u5316\u7b97\u6cd5\u3002\u8bd5\u9664\u6cd5\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u9010\u4e00\u6d4b\u8bd5\u4e00\u4e2a\u6570\u662f\u5426\u80fd\u88ab\u8f83\u5c0f\u7684\u6570\u6574\u9664\u6765\u5224\u65ad\u5b83\u662f\u5426\u4e3a\u7d20\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u8bd5\u9664\u6cd5<\/strong>\u662f\u4e00\u79cd\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u9010\u4e2a\u6d4b\u8bd5\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\u3002\u9996\u5148\uff0c2\u662f\u552f\u4e00\u7684\u5076\u6570\u7d20\u6570\uff0c\u56e0\u6b64\u5982\u679c\u4e00\u4e2a\u6570\u5927\u4e8e2\u4e14\u662f\u5076\u6570\uff0c\u5219\u5b83\u4e0d\u662f\u7d20\u6570\u3002\u5bf9\u4e8e\u5947\u6570\uff0c\u53ef\u4ee5\u4ece3\u5f00\u59cb\u68c0\u67e5\u5176\u80fd\u5426\u88ab\u4efb\u4f55\u5c0f\u4e8e\u5176\u5e73\u65b9\u6839\u7684\u6570\u6574\u9664\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u8bd5\u9664\u6cd5<\/h2>\n<\/p>\n<p><p>\u8bd5\u9664\u6cd5\u662f\u5224\u65ad\u7d20\u6570\u7684\u57fa\u672c\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u8fed\u4ee3\u4ece2\u5230\u6570\u5b57\u7684\u5e73\u65b9\u6839\uff0c\u68c0\u67e5\u8be5\u6570\u5b57\u662f\u5426\u80fd\u88ab\u4efb\u4f55\u8fd9\u4e9b\u6570\u5b57\u6574\u9664\u3002\u5982\u679c\u4e0d\u80fd\uff0c\u5219\u8be5\u6570\u5b57\u662f\u7d20\u6570\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b9e\u73b0\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8bd5\u9664\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u7d20\u6570\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_prime(n):<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return False<\/p>\n<p>    if n &lt;= 3:<\/p>\n<p>        return True<\/p>\n<p>    if n % 2 == 0 or n % 3 == 0:<\/p>\n<p>        return False<\/p>\n<p>    i = 5<\/p>\n<p>    while i * i &lt;= n:<\/p>\n<p>        if n % i == 0 or n % (i + 2) == 0:<\/p>\n<p>            return False<\/p>\n<p>        i += 6<\/p>\n<p>    return True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f18\u5316\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f18\u5316\u8bd5\u9664\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8df3\u8fc7\u5076\u6570<\/strong>\uff1a\u56e0\u4e3a\u9664\u4e862\u4ee5\u5916\uff0c\u6240\u6709\u5076\u6570\u90fd\u4e0d\u662f\u7d20\u6570\u3002<\/li>\n<li><strong>\u68c0\u67e5\u5230\u5e73\u65b9\u6839<\/strong>\uff1a\u4e0d\u9700\u8981\u68c0\u67e5\u5927\u4e8en\u7684\u5e73\u65b9\u6839\u7684\u56e0\u5b50\u3002<\/li>\n<li><strong>6k\u00b11\u4f18\u5316<\/strong>\uff1a\u9664\u4e862\u548c3\uff0c\u6240\u6709\u7d20\u6570\u90fd\u57286\u7684\u500d\u6570\u9644\u8fd1\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5<\/h2>\n<\/p>\n<p><p>\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u662f\u4e00\u79cd\u7528\u4e8e\u5bfb\u627e\u7d20\u6570\u7684\u9ad8\u6548\u7b97\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u627e\u51fa\u8f83\u5927\u8303\u56f4\u5185\u6240\u6709\u7d20\u6570\u7684\u60c5\u51b5\u3002\u8be5\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\u4ece2\u5f00\u59cb\uff0c\u5c06\u6bcf\u4e2a\u7d20\u6570\u7684\u500d\u6570\u6807\u8bb0\u4e3a\u5408\u6570\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b9e\u73b0\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u7684Python\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sieve_of_eratosthenes(limit):<\/p>\n<p>    primes = [True for _ in range(limit + 1)]<\/p>\n<p>    p = 2<\/p>\n<p>    while (p * p &lt;= limit):<\/p>\n<p>        if primes[p]:<\/p>\n<p>            for i in range(p * p, limit + 1, p):<\/p>\n<p>                primes[i] = False<\/p>\n<p>        p += 1<\/p>\n<p>    prime_numbers = [p for p in range(2, limit) if primes[p]]<\/p>\n<p>    return prime_numbers<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n<\/p>\n<p><p>\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log log n)\uff0c\u6bd4\u8bd5\u9664\u6cd5\u66f4\u9ad8\u6548\uff0c\u5c24\u5176\u9002\u5408\u5927\u8303\u56f4\u7d20\u6570\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f18\u5316\u7b97\u6cd5<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u8bd5\u9664\u6cd5\u548c\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u4f18\u5316\u7b97\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\u3002\u4f8b\u5982\uff0c<strong>\u8d39\u9a6c\u5c0f\u5b9a\u7406<\/strong>\u548c<strong>\u7c73\u52d2-\u62c9\u5bbe\u6d4b\u8bd5<\/strong>\uff0c\u8fd9\u4e9b\u7b97\u6cd5\u5728\u5904\u7406\u5927\u6570\u65f6\u5177\u6709\u8f83\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8d39\u9a6c\u5c0f\u5b9a\u7406<\/h3>\n<\/p>\n<p><p>\u8d39\u9a6c\u5c0f\u5b9a\u7406\u6307\u51fa\uff0c\u5982\u679cp\u662f\u7d20\u6570\u4e14a\u662f\u6574\u6570\uff0c\u6ee1\u8db3\u6761\u4ef61 &lt; a &lt; p\uff0c\u5219a^(p-1) \u2261 1 (mod p)\u3002\u5229\u7528\u8fd9\u4e2a\u6027\u8d28\uff0c\u53ef\u4ee5\u6784\u5efa\u6982\u7387\u7d20\u6027\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7c73\u52d2-\u62c9\u5bbe\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u7c73\u52d2-\u62c9\u5bbe\u6d4b\u8bd5\u662f\u57fa\u4e8e\u8d39\u9a6c\u5c0f\u5b9a\u7406\u7684\u6982\u7387\u7d20\u6027\u6d4b\u8bd5\u3002\u5b83\u662f\u4e00\u79cd\u5feb\u901f\u5224\u65ad\u5927\u6570\u662f\u5426\u4e3a\u7d20\u6570\u7684\u7b97\u6cd5\uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u5b8c\u5168\u786e\u5b9a\u7684\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001Python\u5e93\u652f\u6301<\/h2>\n<\/p>\n<p><p>Python\u6709\u4e00\u4e9b\u5e93\u548c\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u7d20\u6570\u95ee\u9898\uff0c\u4f8b\u5982SymPy\u5e93\u3002SymPy\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u5904\u7406\u7d20\u6570\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528SymPy\u5e93<\/h3>\n<\/p>\n<p><p>SymPy\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>isprime()<\/code>\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import isprime<\/p>\n<p>print(isprime(17))  # \u8f93\u51fa: True<\/p>\n<p>print(isprime(18))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6027\u80fd\u4f18\u52bf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528SymPy\u5e93\u7684<code>isprime()<\/code>\u51fd\u6570\uff0c\u4e0d\u4ec5\u65b9\u4fbf\u800c\u4e14\u5728\u5904\u7406\u5927\u6570\u65f6\u6027\u80fd\u826f\u597d\uff0c\u56e0\u4e3a\u5176\u5185\u90e8\u5b9e\u73b0\u4e86\u591a\u79cd\u4f18\u5316\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5e94\u7528\u5b9e\u4f8b<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7d20\u6570\u88ab\u5e7f\u6cdb\u7528\u4e8e\u5bc6\u7801\u5b66\u3001\u968f\u673a\u6570\u751f\u6210\u7b49\u9886\u57df\u3002\u4e86\u89e3\u5982\u4f55\u5728Python\u4e2d\u8868\u793a\u548c\u5904\u7406\u7d20\u6570\uff0c\u5bf9\u4e8e\u5f00\u53d1\u5177\u6709\u5b89\u5168\u6027\u548c\u9ad8\u6027\u80fd\u9700\u6c42\u7684\u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5bc6\u7801\u5b66\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728RSA\u52a0\u5bc6\u7b97\u6cd5\u4e2d\uff0c\u7d20\u6570\u7528\u4e8e\u751f\u6210\u516c\u94a5\u548c\u79c1\u94a5\u3002\u9009\u62e9\u4e24\u4e2a\u5927\u7684\u7d20\u6570\uff0c\u901a\u8fc7\u7279\u5b9a\u7684\u6570\u5b66\u8fd0\u7b97\u751f\u6210\u5bc6\u94a5\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u968f\u673a\u6570\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u67d0\u4e9b\u968f\u673a\u6570\u751f\u6210\u7b97\u6cd5\u9700\u8981\u7d20\u6570\u6765\u4fdd\u8bc1\u751f\u6210\u7684\u6570\u5217\u62e5\u6709\u826f\u597d\u7684\u7edf\u8ba1\u6027\u8d28\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u6570\u8bba\u7814\u7a76<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u8bba\u7814\u7a76\u4e2d\uff0c\u7d20\u6570\u662f\u7814\u7a76\u7684\u6838\u5fc3\u5bf9\u8c61\u4e4b\u4e00\u3002\u4f7f\u7528Python\u5904\u7406\u7d20\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u76f8\u5173\u7684\u6570\u5b66\u7814\u7a76\u548c\u5b9e\u9a8c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u8868\u793a\u548c\u5904\u7406\u7d20\u6570\u3002\u8fd9\u5bf9\u4e8e\u4ece\u4e8b\u79d1\u5b66\u8ba1\u7b97\u3001\u5bc6\u7801\u5b66\u6216\u4efb\u4f55\u9700\u8981\u5904\u7406\u5927\u6570\u7684\u5f00\u53d1\u5de5\u4f5c\u8005\u6765\u8bf4\uff0c\u90fd\u662f\u4e00\u79cd\u5fc5\u5907\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662f\u7d20\u6570\uff0c\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u5b83\uff1f<\/strong><br \/>\u7d20\u6570\u662f\u5927\u4e8e1\u7684\u81ea\u7136\u6570\uff0c\u53ea\u6709\u4e24\u4e2a\u6b63\u56e0\u6570\uff1a1\u548c\u5b83\u672c\u8eab\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\u3002\u901a\u5e38\u7684\u505a\u6cd5\u662f\u68c0\u67e5\u8be5\u6570\u662f\u5426\u80fd\u88ab\u4ece2\u5230\u5176\u5e73\u65b9\u6839\u7684\u6240\u6709\u6574\u6570\u6574\u9664\u3002\u5982\u679c\u4e0d\u80fd\u6574\u9664\uff0c\u5219\u8be5\u6570\u4e3a\u7d20\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u7d20\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u201c\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u201d\u6765\u751f\u6210\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u7d20\u6570\u3002\u8fd9\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u8f83\u9ad8\uff0c\u9002\u5408\u5904\u7406\u8f83\u5927\u7684\u6570\u503c\u8303\u56f4\u3002\u901a\u8fc7\u5efa\u7acb\u4e00\u4e2a\u5e03\u5c14\u5217\u8868\u6765\u6807\u8bb0\u6bcf\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\uff0c\u9010\u6b65\u7b5b\u9009\u51fa\u6240\u6709\u7684\u7d20\u6570\uff0c\u6700\u7ec8\u8fd4\u56de\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u7d20\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u7d20\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u901a\u8fc7\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4f7f\u7528\u4e4b\u524d\u5b9a\u4e49\u7684\u7d20\u6570\u68c0\u67e5\u51fd\u6570\uff0c\u5224\u65ad\u6bcf\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\u3002\u5c06\u6240\u6709\u7684\u7d20\u6570\u5b58\u50a8\u5728\u4e00\u4e2a\u65b0\u5217\u8868\u4e2d\uff0c\u6700\u540e\u8fd4\u56de\u8fd9\u4e2a\u65b0\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u660e\u4e86\uff0c\u8fd8\u80fd\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u627e\u5230\u5217\u8868\u4e2d\u7684\u7d20\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8868\u793a\u7d20\u6570\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u53ea\u80fd\u88ab1\u548c\u81ea\u8eab\u6574\u9664\u6765\u5b9e\u73b0\u3002\u901a\u5e38\u4f7f\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u8bd5\u9664\u6cd5\u3001\u57c3\u62c9\u6258\u65af\u7279 [&hellip;]","protected":false},"author":3,"featured_media":1007988,"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\/1007974"}],"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=1007974"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007974\/revisions"}],"predecessor-version":[{"id":1007991,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007974\/revisions\/1007991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007988"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}