{"id":962168,"date":"2024-12-27T04:05:46","date_gmt":"2024-12-26T20:05:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962168.html"},"modified":"2024-12-27T04:05:49","modified_gmt":"2024-12-26T20:05:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e8%b4%a8%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/962168.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8f93\u51fa\u8d28\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104214\/ab8df2fd-e125-4acc-807c-76580b4e2ee8.webp\" alt=\"python\u4e2d\u5982\u4f55\u8f93\u51fa\u8d28\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8f93\u51fa\u8d28\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u6700\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528\u57fa\u672c\u5faa\u73af\u3001\u4f18\u5316\u7684\u8bd5\u9664\u6cd5\u548c\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u57fa\u672c\u5faa\u73af\u9002\u7528\u4e8e\u5c0f\u8303\u56f4\u5185\u7684\u8d28\u6570\u68c0\u6d4b\uff0c\u800c\u4f18\u5316\u7684\u8bd5\u9664\u6cd5\u548c\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5\u5219\u66f4\u9002\u5408\u5927\u8303\u56f4\u548c\u9ad8\u6548\u7684\u8d28\u6570\u751f\u6210\u3002\u6211\u4eec\u63a5\u4e0b\u6765\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u57fa\u672c\u5faa\u73af\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u57fa\u672c\u5faa\u73af\u65b9\u6cd5\u662f\u68c0\u67e5\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u53ef\u4ee5\u88ab\u6bd4\u81ea\u8eab\u5c0f\u7684\u6570\u5b57\u6574\u9664\u6765\u5224\u65ad\u662f\u5426\u4e3a\u8d28\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u6027\u80fd\u4e0a\u6709\u663e\u8457\u7684\u4e0d\u8db3\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8f83\u5927\u6570\u5b57\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_prime_basic(n):<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return False<\/p>\n<p>    for i in range(2, n):<\/p>\n<p>        if n % i == 0:<\/p>\n<p>            return False<\/p>\n<p>    return True<\/p>\n<p>def print_primes_basic(limit):<\/p>\n<p>    for num in range(2, limit + 1):<\/p>\n<p>        if is_prime_basic(num):<\/p>\n<p>            print(num)<\/p>\n<p>print_primes_basic(100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684<code>is_prime_basic<\/code>\u51fd\u6570\uff0c\u901a\u8fc7\u4ece2\u5230n-1\u7684\u5faa\u73af\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u8d28\u6570\u3002\u7136\u540e\uff0c\u901a\u8fc7<code>print_primes_basic<\/code>\u51fd\u6570\u8f93\u51fa\u7ed9\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u8d28\u6570\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u76f4\u63a5\u3001\u6613\u4e8e\u7406\u89e3\uff0c\u4f46\u5728\u5904\u7406\u8f83\u5927\u8303\u56f4\u7684\u8d28\u6570\u65f6\u6548\u7387\u8f83\u4f4e<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f18\u5316\u7684\u8bd5\u9664\u6cd5<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bd5\u9664\u6cd5\u7684\u4f18\u5316\u7248\u672c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u51cf\u5c11\u9700\u8981\u6d4b\u8bd5\u7684\u56e0\u5b50\u6570\u91cf\u6765\u63d0\u9ad8\u6548\u7387<\/strong>\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ea\u9700\u8981\u6d4b\u8bd5\u76f4\u5230\u6570\u5b57\u7684\u5e73\u65b9\u6839\u5373\u53ef\uff0c\u56e0\u4e3a\u5982\u679cn\u53ef\u4ee5\u5206\u89e3\u4e3a\u4e24\u4e2a\u56e0\u5b50a\u548cb\uff0c\u5176\u4e2da &lt;= b\uff0c\u90a3\u4e48a * a &lt;= n\uff0c\u5373a &lt;= sqrt(n)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def is_prime_optimized(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>def print_primes_optimized(limit):<\/p>\n<p>    for num in range(2, limit + 1):<\/p>\n<p>        if is_prime_optimized(num):<\/p>\n<p>            print(num)<\/p>\n<p>print_primes_optimized(100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5f15\u5165<code>math<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u6570\u5b57\u7684\u5e73\u65b9\u6839\uff0c\u5e76\u51cf\u5c11\u5faa\u73af\u7684\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002<strong>\u4f18\u5316\u7684\u8bd5\u9664\u6cd5\u663e\u8457\u51cf\u5c11\u4e86\u9700\u8981\u6d4b\u8bd5\u7684\u56e0\u5b50\u6570\u91cf\uff0c\u4ece\u800c\u5728\u5904\u7406\u8f83\u5927\u8303\u56f4\u7684\u8d28\u6570\u65f6\u66f4\u52a0\u9ad8\u6548<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5<\/p>\n<\/p>\n<p><p>\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5\u662f\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u7b97\u6cd5\uff0c\u9002\u5408\u7528\u4e8e\u751f\u6210\u8f83\u5927\u8303\u56f4\u7684\u8d28\u6570\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u6807\u8bb0\u975e\u8d28\u6570\u6765\u7b5b\u9009\u51fa\u8d28\u6570<\/strong>\u3002<\/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>    for p in range(2, limit + 1):<\/p>\n<p>        if primes[p]:<\/p>\n<p>            print(p)<\/p>\n<p>sieve_of_eratosthenes(100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\u6765\u6807\u8bb0\u8d28\u6570\u548c\u975e\u8d28\u6570\uff0c\u4ece\u800c\u6709\u6548\u5730\u7b5b\u9009\u51fa\u6240\u6709\u8d28\u6570\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u8303\u56f4\u8d28\u6570\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u6279\u91cf\u5904\u7406\u591a\u4e2a\u6570\u5b57<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u4e0e\u4f18\u5316\u5efa\u8bae<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong>\uff1a\u5bf9\u4e8e\u8f83\u5c0f\u7684\u6570\u5b57\u8303\u56f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u57fa\u672c\u5faa\u73af\u65b9\u6cd5\uff0c\u56e0\u4e3a\u7b80\u5355\u6613\u61c2\u4e14\u5b9e\u73b0\u5bb9\u6613\u3002\u7136\u800c\uff0c\u5f53\u5904\u7406\u66f4\u5927\u7684\u6570\u5b57\u8303\u56f4\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u4f18\u5316\u7684\u8bd5\u9664\u6cd5\u6216\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5185\u5b58\u4e0e\u6027\u80fd\u6743\u8861<\/strong>\uff1a\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5\u867d\u7136\u9ad8\u6548\uff0c\u4f46\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u3002\u5982\u679c\u5185\u5b58\u4f7f\u7528\u662f\u4e00\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4f18\u5316\u7684\u8bd5\u9664\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u884c\u5316\u5904\u7406<\/strong>\uff1a\u5728\u9700\u8981\u5904\u7406\u975e\u5e38\u5927\u8303\u56f4\u7684\u8d28\u6570\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5e76\u884c\u5316\u5904\u7406\u3002Python\u7684\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5206\u62c5\u8ba1\u7b97\u8d1f\u62c5\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5e93\u51fd\u6570<\/strong>\uff1a\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u73b0\u6210\u7684\u5e93\u51fd\u6570\u53ef\u80fd\u66f4\u4e3a\u9ad8\u6548\u3002\u4f8b\u5982\uff0cSymPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\uff0c\u5305\u62ec\u8d28\u6570\u68c0\u6d4b\u548c\u751f\u6210\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from sympy import isprime, primerange<\/p>\n<p>print(list(primerange(1, 100)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>SymPy\u5e93\u4e2d\u7684<code>isprime<\/code>\u548c<code>primerange<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u7528\u4e8e\u8d28\u6570\u7684\u68c0\u6d4b\u548c\u751f\u6210\uff0c<strong>\u8fd9\u662f\u5728\u9700\u8981\u5feb\u901f\u5b9e\u73b0\u6216\u5904\u7406\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u65f6\u7684\u4e00\u4e2a\u9ad8\u6548\u9009\u62e9<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c\u5728Python\u4e2d\u8f93\u51fa\u8d28\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u9002\u5408\u7684\u7b97\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u548c\u6027\u80fd\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u57fa\u672c\u5faa\u73af\u8fd8\u662f\u9ad8\u6548\u7684\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5\uff0c\u7406\u89e3\u5176\u80cc\u540e\u7684\u539f\u7406\u548c\u9002\u7528\u573a\u666f\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u7684\u9700\u6c42\u4e0b\u9009\u62e9\u6700\u5408\u9002\u7684\u8d28\u6570\u8f93\u51fa\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u7f16\u5199\u7a0b\u5e8f\u6765\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d28\u6570\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u8d28\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5224\u65ad\u8be5\u6570\u662f\u5426\u80fd\u88ab\u5c0f\u4e8e\u5b83\u7684\u4efb\u4f55\u6570\u6574\u9664\u3002\u82e5\u80fd\u6574\u9664\uff0c\u5219\u4e0d\u662f\u8d28\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def is_prime(n):\n    if n &lt;= 1:\n        return False\n    for i in range(2, int(n**0.5) + 1):\n        if n % i == 0:\n            return False\n    return True\n\n# \u68c0\u67e5\u8d28\u6570\nnumber = 29\nprint(f&quot;{number} \u662f\u8d28\u6570\u5417\uff1f {is_prime(number)}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u7ed9\u5b9a\u7684\u6570\u662f\u5426\u4e3a\u8d28\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u51fa\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u8d28\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u5408\u4e4b\u524d\u7684\u8d28\u6570\u68c0\u67e5\u51fd\u6570\uff0c\u904d\u5386\u7279\u5b9a\u8303\u56f4\u5185\u7684\u6bcf\u4e2a\u6570\uff0c\u5e76\u8f93\u51fa\u6240\u6709\u8d28\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def list_primes_in_range(start, end):\n    primes = []\n    for num in range(start, end + 1):\n        if is_prime(num):\n            primes.append(num)\n    return primes\n\n# \u8f93\u51fa\u8303\u56f4\u5185\u7684\u8d28\u6570\nprint(list_primes_in_range(10, 50))\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5217\u51fa\u6307\u5b9a\u8303\u56f4\uff08\u4f8b\u598210\u523050\uff09\u5185\u7684\u6240\u6709\u8d28\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f18\u5316\u8d28\u6570\u8f93\u51fa\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\u6765\u4f18\u5316\u8d28\u6570\u7684\u8f93\u51fa\uff0c\u4f8b\u5982\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u6807\u8bb0\u5408\u6570\u6765\u627e\u5230\u8d28\u6570\uff0c\u6548\u7387\u66f4\u9ad8\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">def sieve_of_eratosthenes(limit):\n    is_prime = [True] * (limit + 1)\n    p = 2\n    while p**2 &lt;= limit:\n        if is_prime[p]:\n            for i in range(p**2, limit + 1, p):\n                is_prime[i] = False\n        p += 1\n    return [p for p in range(2, limit + 1) if is_prime[p]]\n\n# \u8f93\u51fa\u8303\u56f4\u5185\u7684\u8d28\u6570\nprint(sieve_of_eratosthenes(50))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5728\u8f83\u5927\u8303\u56f4\u5185\u9ad8\u6548\u5730\u627e\u5230\u6240\u6709\u8d28\u6570\uff0c\u9002\u5408\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u51fa\u8d28\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528\u57fa\u672c\u5faa\u73af\u3001\u4f18\u5316\u7684\u8bd5\u9664\u6cd5\u548c\u57c3\u62c9\u6258\u8272\u5c3c\u7b5b\u6cd5\u3002\u5176\u4e2d\uff0c\u57fa\u672c\u5faa\u73af\u9002 [&hellip;]","protected":false},"author":3,"featured_media":962175,"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\/962168"}],"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=962168"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962168\/revisions"}],"predecessor-version":[{"id":962176,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962168\/revisions\/962176"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/962175"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=962168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=962168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=962168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}