{"id":1105138,"date":"2025-01-08T16:28:02","date_gmt":"2025-01-08T08:28:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105138.html"},"modified":"2025-01-08T16:28:04","modified_gmt":"2025-01-08T08:28:04","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%88%97%e8%a1%a8%e5%ad%98%e6%94%be%e5%ae%8c%e6%95%b0%e7%9a%84%e5%9b%a0%e5%ad%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105138.html","title":{"rendered":"python\u5982\u4f55\u7528\u5217\u8868\u5b58\u653e\u5b8c\u6570\u7684\u56e0\u5b50"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070201\/86a6ff23-df54-4799-9dee-8f8fb21ae553.webp\" alt=\"python\u5982\u4f55\u7528\u5217\u8868\u5b58\u653e\u5b8c\u6570\u7684\u56e0\u5b50\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u7528\u5217\u8868\u5b58\u653e\u5b8c\u6570\u7684\u56e0\u5b50<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b58\u653e\u5b8c\u6570\u7684\u56e0\u5b50\u3002<strong>\u5b8c\u6570\u662f\u6307\u4e00\u4e2a\u6570\uff0c\u5176\u6240\u6709\u771f\u56e0\u5b50\uff08\u5373\u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u7684\u6240\u6709\u56e0\u5b50\uff09\u4e4b\u548c\u7b49\u4e8e\u5176\u672c\u8eab<\/strong>\u3002\u4e3a\u4e86\u627e\u5230\u5b8c\u6570\u7684\u56e0\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a1. \u904d\u5386\u4ece1\u5230n-1\u7684\u6240\u6709\u6570\uff0c\u627e\u5230\u8fd9\u4e9b\u6570\u4e2d\u80fd\u591f\u6574\u9664n\u7684\u56e0\u5b50\uff1b2. \u5c06\u8fd9\u4e9b\u56e0\u5b50\u5b58\u653e\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff1b3. \u8fd4\u56de\u8be5\u5217\u8868\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b8c\u6570\u7684\u5b9a\u4e49\u548c\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u5b8c\u6570\uff08Perfect Number\uff09\u662f\u6307\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u5176\u6240\u6709\u771f\u56e0\u5b50\uff08\u5373\u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u7684\u6240\u6709\u56e0\u5b50\uff09\u7684\u548c\u7b49\u4e8e\u8be5\u6570\u672c\u8eab\u3002\u4f8b\u5982\uff0c6\u662f\u4e00\u4e2a\u5b8c\u6570\uff0c\u56e0\u4e3a\u5176\u771f\u56e0\u5b501\u30012\u30013\u76f8\u52a0\u7b49\u4e8e6\u3002<\/p>\n<\/p>\n<p><h4>\u5b8c\u6570\u7684\u4f8b\u5b50<\/h4>\n<\/p>\n<ul>\n<li>6\u7684\u56e0\u5b50\u662f1, 2, 3\uff0c\u4e141 + 2 + 3 = 6<\/li>\n<li>28\u7684\u56e0\u5b50\u662f1, 2, 4, 7, 14\uff0c\u4e141 + 2 + 4 + 7 + 14 = 28<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001Python\u4e2d\u67e5\u627e\u5b8c\u6570\u7684\u56e0\u5b50<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u67e5\u627e\u5e76\u5b58\u653e\u5b8c\u6570\u7684\u56e0\u5b50\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868\u7528\u4e8e\u5b58\u653e\u56e0\u5b50<\/h4>\n<\/p>\n<p><h4>2. \u904d\u5386\u4ece1\u5230n-1\u7684\u6240\u6709\u6570\uff0c\u68c0\u67e5\u662f\u5426\u4e3an\u7684\u56e0\u5b50<\/h4>\n<\/p>\n<p><h4>3. \u5c06\u627e\u5230\u7684\u56e0\u5b50\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/h4>\n<\/p>\n<p><h4>4. \u8fd4\u56de\u8be5\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5177\u4f53\u7684Python\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_factors(num):<\/p>\n<p>    factors = []<\/p>\n<p>    for i in range(1, num):<\/p>\n<p>        if num % i == 0:<\/p>\n<p>            factors.append(i)<\/p>\n<p>    return factors<\/p>\n<p>def is_perfect_number(num):<\/p>\n<p>    factors = find_factors(num)<\/p>\n<p>    return sum(factors) == num<\/p>\n<p>num = 28<\/p>\n<p>if is_perfect_number(num):<\/p>\n<p>    print(f&quot;{num} is a perfect number with factors: {find_factors(num)}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num} is not a perfect number.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4ee3\u7801\u8be6\u89e3<\/h3>\n<\/p>\n<p><h4>1. \u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868\u7528\u4e8e\u5b58\u653e\u56e0\u5b50<\/h4>\n<\/p>\n<p><p>\u5728\u51fd\u6570<code>find_factors<\/code>\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868<code>factors<\/code>\uff0c\u7528\u4e8e\u5b58\u653e\u627e\u5230\u7684\u56e0\u5b50\u3002<\/p>\n<\/p>\n<p><h4>2. \u904d\u5386\u4ece1\u5230n-1\u7684\u6240\u6709\u6570\uff0c\u68c0\u67e5\u662f\u5426\u4e3an\u7684\u56e0\u5b50<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>for<\/code>\u5faa\u73af\uff0c\u6211\u4eec\u904d\u5386\u4ece1\u5230<code>num-1<\/code>\u7684\u6240\u6709\u6570\u3002\u5728\u5faa\u73af\u4e2d\uff0c\u901a\u8fc7<code>if num % i == 0<\/code>\u68c0\u67e5<code>i<\/code>\u662f\u5426\u4e3a<code>num<\/code>\u7684\u56e0\u5b50\u3002\u5982\u679c\u662f\uff0c\u5219\u5c06<code>i<\/code>\u6dfb\u52a0\u5230<code>factors<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>3. \u5c06\u627e\u5230\u7684\u56e0\u5b50\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/h4>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u4e2d\uff0c\u901a\u8fc7<code>factors.append(i)<\/code>\u5c06\u627e\u5230\u7684\u56e0\u5b50\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>4. \u8fd4\u56de\u8be5\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u7ed3\u675f\u540e\uff0c\u901a\u8fc7<code>return factors<\/code>\u8fd4\u56de\u56e0\u5b50\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9a8c\u8bc1\u5b8c\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570<code>is_perfect_number<\/code>\u4e2d\uff0c\u6211\u4eec\u8c03\u7528<code>find_factors<\/code>\u51fd\u6570\u83b7\u53d6\u56e0\u5b50\u5217\u8868\uff0c\u5e76\u901a\u8fc7<code>sum(factors) == num<\/code>\u68c0\u67e5\u8fd9\u4e9b\u56e0\u5b50\u7684\u548c\u662f\u5426\u7b49\u4e8e<code>num<\/code>\u3002\u5982\u679c\u7b49\u4e8e\uff0c\u5219<code>num<\/code>\u662f\u5b8c\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6269\u5c55\uff1a\u67e5\u627e\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u5b8c\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u6269\u5c55\u4e0a\u8ff0\u4ee3\u7801\uff0c\u67e5\u627e\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u5b8c\u6570\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_perfect_numbers(limit):<\/p>\n<p>    perfect_numbers = []<\/p>\n<p>    for num in range(2, limit + 1):<\/p>\n<p>        if is_perfect_number(num):<\/p>\n<p>            perfect_numbers.append(num)<\/p>\n<p>    return perfect_numbers<\/p>\n<p>limit = 10000<\/p>\n<p>perfect_numbers = find_perfect_numbers(limit)<\/p>\n<p>print(f&quot;Perfect numbers up to {limit}: {perfect_numbers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1. \u67e5\u627e\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u5b8c\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u51fd\u6570<code>find_perfect_numbers<\/code>\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u904d\u5386\u4ece2\u5230<code>limit<\/code>\u7684\u6240\u6709\u6570\uff0c\u8c03\u7528<code>is_perfect_number<\/code>\u51fd\u6570\u68c0\u67e5\u6bcf\u4e2a\u6570\u662f\u5426\u4e3a\u5b8c\u6570\u3002\u5982\u679c\u662f\uff0c\u5219\u5c06\u8be5\u6570\u6dfb\u52a0\u5230<code>perfect_numbers<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u8fd4\u56de\u5b8c\u6570\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u7ed3\u675f\u540e\uff0c\u901a\u8fc7<code>return perfect_numbers<\/code>\u8fd4\u56de\u627e\u5230\u7684\u6240\u6709\u5b8c\u6570\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u548c\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u5927\u7684\u6570\uff0c\u4ee5\u4e0a\u65b9\u6cd5\u53ef\u80fd\u4f1a\u6709\u6027\u80fd\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\uff0c\u4f8b\u5982\u6b27\u51e0\u91cc\u5f97-\u6b27\u62c9\u5b9a\u7406\uff0c\u8be5\u5b9a\u7406\u6307\u51fa\u5982\u679c<code>2^(p-1) * (2^p - 1)<\/code>\u662f\u5b8c\u6570\uff0c\u5176\u4e2d<code>2^p - 1<\/code>\u662f\u4e00\u4e2a\u6885\u68ee\u7d20\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def euclid_euler_theorem(limit):<\/p>\n<p>    perfect_numbers = []<\/p>\n<p>    p = 2<\/p>\n<p>    while True:<\/p>\n<p>        mersenne_prime = 2p - 1<\/p>\n<p>        if is_prime(mersenne_prime):<\/p>\n<p>            perfect_number = 2(p-1) * mersenne_prime<\/p>\n<p>            if perfect_number &gt; limit:<\/p>\n<p>                break<\/p>\n<p>            perfect_numbers.append(perfect_number)<\/p>\n<p>        p += 1<\/p>\n<p>    return perfect_numbers<\/p>\n<p>def is_prime(n):<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return False<\/p>\n<p>    for i in range(2, int(n0.5) + 1):<\/p>\n<p>        if n % i != 0:<\/p>\n<p>            return False<\/p>\n<p>    return True<\/p>\n<p>limit = 10000<\/p>\n<p>perfect_numbers = euclid_euler_theorem(limit)<\/p>\n<p>print(f&quot;Perfect numbers up to {limit}: {perfect_numbers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5217\u8868\u5b58\u653e\u5b8c\u6570\u7684\u56e0\u5b50\uff0c\u5e76\u8be6\u7ec6\u63cf\u8ff0\u4e86\u67e5\u627e\u5b8c\u6570\u56e0\u5b50\u7684\u8fc7\u7a0b\u3002\u901a\u8fc7\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0c\u5e76\u8fdb\u4e00\u6b65\u6269\u5c55\u4e86\u67e5\u627e\u4e00\u5b9a\u8303\u56f4\u5185\u6240\u6709\u5b8c\u6570\u7684\u529f\u80fd\u3002\u6700\u540e\uff0c\u4ecb\u7ecd\u4e86\u57fa\u4e8e\u6b27\u51e0\u91cc\u5f97-\u6b27\u62c9\u5b9a\u7406\u7684\u4f18\u5316\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u7406\u89e3\u548c\u5b9e\u73b0\u5b8c\u6570\u7684\u56e0\u5b50\u67e5\u627e\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u5b8c\u6570\u7684\u56e0\u5b50\u5e76\u5b58\u50a8\u5b83\u4eec\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u6765\u8bc6\u522b\u5b8c\u6570\u7684\u56e0\u5b50\u3002\u5b8c\u6570\u662f\u6307\u4e00\u4e2a\u6570\u7b49\u4e8e\u5b83\u6240\u6709\u56e0\u5b50\u7684\u548c\uff08\u4e0d\u5305\u62ec\u5b83\u81ea\u8eab\uff09\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u4ece1\u5230\u8be5\u6570\u7684\u4e00\u534a\u7684\u6240\u6709\u6570\uff0c\u68c0\u67e5\u662f\u5426\u80fd\u6574\u9664\u8be5\u6570\uff0c\u6765\u627e\u5230\u56e0\u5b50\u3002\u5c06\u8fd9\u4e9b\u56e0\u5b50\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u5b8c\u6570\uff1f<\/strong><\/p>\n<p>\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u5b8c\u6570\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u8ba1\u7b97\u5176\u56e0\u5b50\u7684\u548c\uff0c\u5e76\u5c06\u5176\u4e0e\u8be5\u6570\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u56e0\u5b50\u7684\u548c\u7b49\u4e8e\u8be5\u6570\u672c\u8eab\uff0c\u5219\u5b83\u662f\u5b8c\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5229\u7528\u5faa\u73af\u6765\u7d2f\u52a0\u56e0\u5b50\uff0c\u5e76\u5728\u7d2f\u52a0\u540e\u5224\u65ad\u548c\u662f\u5426\u7b49\u4e8e\u539f\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u5b8c\u6570\u56e0\u5b50\u67e5\u627e\u7684\u6548\u7387\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u63d0\u9ad8\u67e5\u627e\u56e0\u5b50\u7684\u6548\u7387\uff0c\u53ef\u4ee5\u53ea\u904d\u5386\u5230\u8be5\u6570\u7684\u5e73\u65b9\u6839\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u56e0\u5b50i\uff0c\u5982\u679ci\u80fd\u6574\u9664\u8be5\u6570\uff0c\u90a3\u4e48\u540c\u65f6\u4e5f\u80fd\u5f97\u5230\u53e6\u4e00\u4e2a\u56e0\u5b50\uff08\u5373\u8be5\u6570\u9664\u4ee5i\uff09\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u5faa\u73af\u6b21\u6570\uff0c\u4ece\u800c\u52a0\u5feb\u7a0b\u5e8f\u7684\u6267\u884c\u901f\u5ea6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u5b8c\u6570\u7684\u56e0\u5b50\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u5c06\u5b58\u50a8\u5b8c\u6570\u56e0\u5b50\u7684\u5217\u8868\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u4f8b\u5982\uff0c\u5728\u627e\u5230\u5b8c\u6570\u7684\u56e0\u5b50\u540e\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u6253\u5370\u5217\u8868\uff0c\u6216\u8005\u901a\u8fc7\u5faa\u73af\u9010\u4e2a\u8f93\u51fa\u56e0\u5b50\uff0c\u65b9\u4fbf\u7528\u6237\u67e5\u770b\u5177\u4f53\u7684\u56e0\u5b50\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u7528\u5217\u8868\u5b58\u653e\u5b8c\u6570\u7684\u56e0\u5b50 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b58\u653e\u5b8c\u6570\u7684\u56e0\u5b50\u3002\u5b8c\u6570\u662f\u6307\u4e00\u4e2a\u6570\uff0c\u5176\u6240\u6709\u771f [&hellip;]","protected":false},"author":3,"featured_media":1105146,"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\/1105138"}],"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=1105138"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105138\/revisions"}],"predecessor-version":[{"id":1105148,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105138\/revisions\/1105148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105146"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}