{"id":1097905,"date":"2025-01-08T15:16:18","date_gmt":"2025-01-08T07:16:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097905.html"},"modified":"2025-01-08T15:16:20","modified_gmt":"2025-01-08T07:16:20","slug":"%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e6%9c%80%e5%a4%a7%e5%85%ac%e7%ba%a6%e6%95%b0python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097905.html","title":{"rendered":"\u5982\u4f55\u5b9a\u4e49\u6700\u5927\u516c\u7ea6\u6570python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212444\/09c86723-8f5c-434f-a0da-b3d28b75d92d.webp\" alt=\"\u5982\u4f55\u5b9a\u4e49\u6700\u5927\u516c\u7ea6\u6570python\" \/><\/p>\n<p><p> <strong>\u6700\u5927\u516c\u7ea6\u6570\uff08Greatest Common Divisor, GCD\uff09\u662f\u6307\u80fd\u591f\u6574\u9664\u4e24\u4e2a\u6216\u591a\u4e2a\u6574\u6570\u7684\u6700\u5927\u6574\u6570\u3002<\/strong> <strong>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u6700\u5927\u516c\u7ea6\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u8f97\u8f6c\u76f8\u9664\u6cd5\uff08\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\uff09\u3001\u9012\u5f52\u65b9\u6cd5\u4ee5\u53caPython\u5185\u7f6e\u51fd\u6570\u3002<\/strong> <strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\uff0c\u5e2e\u52a9\u60a8\u7406\u89e3\u548c\u5e94\u7528\u3002<\/strong> \u8f83\u4e3a\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u5b9a\u4e49\u6700\u5927\u516c\u7ea6\u6570<\/h3>\n<\/p>\n<p><p>\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u662f\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u6700\u5927\u516c\u7ea6\u6570\u7684\u7ecf\u5178\u65b9\u6cd5\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\u5229\u7528\u4e24\u4e2a\u6570\u4e4b\u95f4\u7684\u5dee\u548c\u4f59\u6570\u53cd\u590d\u8fd0\u7b97\uff0c\u76f4\u5230\u5176\u4e2d\u4e00\u4e2a\u6570\u4e3a\u96f6\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5c06\u8f83\u5927\u6570\u9664\u4ee5\u8f83\u5c0f\u6570\uff0c\u5f97\u5230\u5546\u548c\u4f59\u6570\uff1b<\/li>\n<li>\u7528\u8f83\u5c0f\u6570\u548c\u4f59\u6570\u7ee7\u7eed\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u76f4\u5230\u4f59\u6570\u4e3a\u96f6\uff1b<\/li>\n<li>\u6700\u540e\u4e00\u4e2a\u4e0d\u4e3a\u96f6\u7684\u4f59\u6570\u5373\u4e3a\u6700\u5927\u516c\u7ea6\u6570\u3002<\/li>\n<\/ol>\n<p><h4>1.1 \u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u7684\u5b9e\u73b0<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def gcd(a, b):<\/p>\n<p>    while b != 0:<\/p>\n<p>        a, b = b, a % b<\/p>\n<p>    return a<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(gcd(48, 18))  # \u8f93\u51fa\uff1a6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>gcd<\/code> \u51fd\u6570\u901a\u8fc7\u5faa\u73af\u4e0d\u65ad\u4ea4\u6362 <code>a<\/code> \u548c <code>b<\/code> \u7684\u503c\uff0c\u5e76\u4f7f\u7528 <code>a % b<\/code> \u8ba1\u7b97\u65b0\u7684 <code>b<\/code> \u503c\uff0c\u76f4\u5230 <code>b<\/code> \u4e3a\u96f6\u3002\u6b64\u65f6\uff0c<code>a<\/code> \u5373\u4e3a\u6240\u6c42\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u9012\u5f52\u6cd5\u5b9a\u4e49\u6700\u5927\u516c\u7ea6\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u6cd5\u662f\u53e6\u4e00\u79cd\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u51fd\u6570\u6765\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u9012\u5f52\u6cd5\u7684\u5b9e\u73b0<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def gcd_recursive(a, b):<\/p>\n<p>    if b == 0:<\/p>\n<p>        return a<\/p>\n<p>    else:<\/p>\n<p>        return gcd_recursive(a % b, b)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(gcd_recursive(48, 18))  # \u8f93\u51fa\uff1a6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>gcd_recursive<\/code> \u51fd\u6570\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u81ea\u8eab\u6765\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u3002\u5982\u679c <code>b<\/code> \u4e3a\u96f6\uff0c\u5219\u8fd4\u56de <code>a<\/code>\uff1b\u5426\u5219\uff0c\u9012\u5f52\u8c03\u7528 <code>gcd_recursive(a % b, b)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570<\/h3>\n<\/p>\n<p><p>Python 3.5 \u53ca\u4ee5\u4e0a\u7248\u672c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570 <code>math.gcd<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u5feb\u6377\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7684\u5b9e\u73b0<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(math.gcd(48, 18))  # \u8f93\u51fa\uff1a6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u53ea\u9700\u5bfc\u5165 <code>math<\/code> \u6a21\u5757\uff0c\u5e76\u8c03\u7528 <code>math.gcd<\/code> \u51fd\u6570\u5373\u53ef\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u6700\u4e3a\u7b80\u6d01\uff0c\u63a8\u8350\u5728Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u5982\u5206\u6570\u5316\u7b80\u3001\u52a0\u5bc6\u7b97\u6cd5\u7b49\u3002\u4e0b\u9762\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u53ca\u5176\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5206\u6570\u5316\u7b80<\/h4>\n<\/p>\n<p><p>\u5206\u6570\u5316\u7b80\u662f\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u7684\u5e38\u89c1\u5e94\u7528\u3002\u901a\u8fc7\u6700\u5927\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u5c06\u5206\u6570\u7684\u5206\u5b50\u548c\u5206\u6bcd\u540c\u65f6\u9664\u4ee5\u5176\u6700\u5927\u516c\u7ea6\u6570\uff0c\u4ece\u800c\u5f97\u5230\u6700\u7b80\u5206\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def simplify_fraction(numerator, denominator):<\/p>\n<p>    gcd_value = gcd(numerator, denominator)<\/p>\n<p>    return numerator \/\/ gcd_value, denominator \/\/ gcd_value<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(simplify_fraction(48, 18))  # \u8f93\u51fa\uff1a(8, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>simplify_fraction<\/code> \u51fd\u6570\u901a\u8fc7\u8ba1\u7b97\u5206\u5b50\u548c\u5206\u6bcd\u7684\u6700\u5927\u516c\u7ea6\u6570\uff0c\u5c06\u5206\u6570\u5316\u7b80\u4e3a\u6700\u7b80\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u52a0\u5bc6\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u4e00\u4e9b\u52a0\u5bc6\u7b97\u6cd5\uff08\u5982RSA\u7b97\u6cd5\uff09\u4e2d\uff0c\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u662f\u751f\u6210\u5bc6\u94a5\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u901a\u8fc7\u6700\u5927\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u6570\u7684\u4e92\u8d28\u5173\u7cfb\uff0c\u4ece\u800c\u751f\u6210\u516c\u94a5\u548c\u79c1\u94a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def are_coprime(a, b):<\/p>\n<p>    return gcd(a, b) == 1<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(are_coprime(35, 64))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>are_coprime<\/code> \u51fd\u6570\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\uff0c\u5224\u65ad\u5b83\u4eec\u662f\u5426\u4e92\u8d28\u3002\u5982\u679c\u6700\u5927\u516c\u7ea6\u6570\u4e3a1\uff0c\u5219\u8868\u793a\u5b83\u4eec\u4e92\u8d28\u3002<\/p>\n<\/p>\n<p><h4>4.3 \u591a\u4e2a\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u6709\u65f6\u9700\u8981\u8ba1\u7b97\u591a\u4e2a\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u4e24\u4e24\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\uff0c\u9010\u6b65\u6c42\u5f97\u591a\u4e2a\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>def gcd_multiple(numbers):<\/p>\n<p>    return reduce(gcd, numbers)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>numbers = [48, 72, 120]<\/p>\n<p>print(gcd_multiple(numbers))  # \u8f93\u51fa\uff1a24<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>gcd_multiple<\/code> \u51fd\u6570\u901a\u8fc7 <code>reduce<\/code> \u51fd\u6570\u548c <code>gcd<\/code> \u51fd\u6570\uff0c\u9010\u6b65\u8ba1\u7b97\u591a\u4e2a\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u4e0e\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u548c\u65b9\u6cd5\u5bf9\u6027\u80fd\u4f18\u5316\u975e\u5e38\u91cd\u8981\u3002\u4e0b\u9762\u5c06\u6bd4\u8f83\u4e09\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u5e2e\u52a9\u60a8\u9009\u62e9\u6700\u4f18\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u6027\u80fd\u6bd4\u8f83<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u6d4b\u8bd5\u51fd\u6570<\/strong><\/h2>\n<p>def test_gcd_performance():<\/p>\n<p>    a, b = 123456, 789012<\/p>\n<p>    # \u6d4b\u8bd5\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    for _ in range(100000):<\/p>\n<p>        gcd(a, b)<\/p>\n<p>    print(&quot;\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u8017\u65f6\uff1a&quot;, time.time() - start_time)<\/p>\n<p>    # \u6d4b\u8bd5\u9012\u5f52\u6cd5<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    for _ in range(100000):<\/p>\n<p>        gcd_recursive(a, b)<\/p>\n<p>    print(&quot;\u9012\u5f52\u6cd5\u8017\u65f6\uff1a&quot;, time.time() - start_time)<\/p>\n<p>    # \u6d4b\u8bd5\u5185\u7f6e\u51fd\u6570<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    for _ in range(100000):<\/p>\n<p>        math.gcd(a, b)<\/p>\n<p>    print(&quot;\u5185\u7f6e\u51fd\u6570\u8017\u65f6\uff1a&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong><\/h2>\n<p>test_gcd_performance()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>test_gcd_performance<\/code> \u51fd\u6570\u901a\u8fc7\u591a\u6b21\u8c03\u7528\u4e09\u79cd\u65b9\u6cd5\uff0c\u6bd4\u8f83\u5b83\u4eec\u7684\u8017\u65f6\u3002\u4e00\u822c\u6765\u8bf4\uff0cPython\u5185\u7f6e\u51fd\u6570 <code>math.gcd<\/code> \u7684\u6027\u80fd\u6700\u4f73\uff0c\u5176\u6b21\u662f\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\uff0c\u9012\u5f52\u6cd5\u6027\u80fd\u7a0d\u900a\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u5982\u5904\u7406\u8d1f\u6570\u3001\u96f6\u548c\u5927\u6570\u3002\u4e0b\u9762\u5c06\u4ecb\u7ecd\u8fd9\u4e9b\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u5904\u7406\u8d1f\u6570<\/h4>\n<\/p>\n<p><p>\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u65f6\uff0c\u53ef\u4ee5\u5ffd\u7565\u8d1f\u53f7\uff0c\u56e0\u4e3a\u8d1f\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u7b49\u4e8e\u5176\u7edd\u5bf9\u503c\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def gcd_with_negatives(a, b):<\/p>\n<p>    return gcd(abs(a), abs(b))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(gcd_with_negatives(-48, 18))  # \u8f93\u51fa\uff1a6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>gcd_with_negatives<\/code> \u51fd\u6570\u901a\u8fc7\u8ba1\u7b97\u7edd\u5bf9\u503c\u7684\u6700\u5927\u516c\u7ea6\u6570\uff0c\u89e3\u51b3\u4e86\u8d1f\u6570\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u5904\u7406\u96f6<\/h4>\n<\/p>\n<p><p>\u5f53\u8f93\u5165\u7684\u6570\u4e3a\u96f6\u65f6\uff0c\u6700\u5927\u516c\u7ea6\u6570\u7684\u5b9a\u4e49\u4e3a\u53e6\u4e00\u6570\u7684\u7edd\u5bf9\u503c\u3002\u5982\u679c\u4e24\u4e2a\u6570\u5747\u4e3a\u96f6\uff0c\u5219\u6ca1\u6709\u6700\u5927\u516c\u7ea6\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def gcd_with_zero(a, b):<\/p>\n<p>    if a == 0 and b == 0:<\/p>\n<p>        return None<\/p>\n<p>    return gcd(abs(a), abs(b))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(gcd_with_zero(0, 18))  # \u8f93\u51fa\uff1a18<\/p>\n<p>print(gcd_with_zero(0, 0))   # \u8f93\u51fa\uff1aNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>gcd_with_zero<\/code> \u51fd\u6570\u5904\u7406\u4e86\u96f6\u7684\u60c5\u51b5\uff0c\u786e\u4fdd\u7ed3\u679c\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><h4>6.3 \u5904\u7406\u5927\u6570<\/h4>\n<\/p>\n<p><p>Python \u7684\u6574\u6570\u7c7b\u578b\u53ef\u4ee5\u5904\u7406\u4efb\u610f\u5927\u5c0f\u7684\u6570\uff0c\u4f46\u8ba1\u7b97\u5927\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u65f6\uff0c\u7b97\u6cd5\u7684\u6548\u7387\u5c24\u4e3a\u91cd\u8981\u3002\u5efa\u8bae\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>math.gcd<\/code> \u6216\u4f18\u5316\u7684\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u5927\u6570<\/strong><\/h2>\n<p>a = random.randint(1, 10100)<\/p>\n<p>b = random.randint(1, 10100)<\/p>\n<h2><strong>\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570<\/strong><\/h2>\n<p>print(math.gcd(a, b))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u751f\u6210\u968f\u673a\u5927\u6570\uff0c\u5e76\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>math.gcd<\/code> \u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\uff0c\u786e\u4fdd\u7b97\u6cd5\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5b9a\u4e49\u6700\u5927\u516c\u7ea6\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u3001\u9012\u5f52\u6cd5\u548cPython\u5185\u7f6e\u51fd\u6570\u3002\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u548c\u6027\u80fd\u6bd4\u8f83\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u6cd5\u3002<strong>\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u6700\u5927\u516c\u7ea6\u6570\u5728\u5206\u6570\u5316\u7b80\u3001\u52a0\u5bc6\u7b97\u6cd5\u7b49\u5e94\u7528\u4e2d\u7684\u5b9e\u9645\u6848\u4f8b\uff0c\u5e76\u89e3\u51b3\u4e86\u5904\u7406\u8d1f\u6570\u3001\u96f6\u548c\u5927\u6570\u7b49\u5e38\u89c1\u95ee\u9898\u3002<\/strong> \u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u6700\u5927\u516c\u7ea6\u6570\u7684\u8ba1\u7b97\u65b9\u6cd5\u3002\u5982\u679c\u60a8\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u7559\u8a00\u4ea4\u6d41\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u6700\u5927\u516c\u7ea6\u6570\u5728Python\u4e2d\u662f\u5982\u4f55\u8ba1\u7b97\u7684\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\uff08GCD\uff09\u901a\u5e38\u4f7f\u7528\u5185\u7f6e\u7684<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>gcd<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002\u4f7f\u7528\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import math\n\na = 48\nb = 18\nresult = math.gcd(a, b)\nprint(f&quot;{a}\u548c{b}\u7684\u6700\u5927\u516c\u7ea6\u6570\u662f{result}&quot;)\n<\/code><\/pre>\n<p><strong>\u6211\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u5417\uff1f<\/strong><br \/>\u7edd\u5bf9\u53ef\u4ee5\uff01\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u8f97\u8f6c\u76f8\u9664\u6cd5\uff08\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\uff09\u6765\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">def gcd(x, y):\n    while y:\n        x, y = y, x % y\n    return x\n\nprint(gcd(48, 18))  # \u8f93\u51fa\uff1a6\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u591a\u4e2a\u6570\u5b57\u7684\u6700\u5927\u516c\u7ea6\u6570\uff1f<\/strong><br \/>\u8ba1\u7b97\u591a\u4e2a\u6570\u5b57\u7684\u6700\u5927\u516c\u7ea6\u6570\u53ef\u4ee5\u901a\u8fc7<code>functools<\/code>\u6a21\u5757\u4e2d\u7684<code>reduce<\/code>\u51fd\u6570\u914d\u5408<code>math.gcd<\/code>\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from functools import reduce\nimport math\n\nnumbers = [48, 18, 30]\nresult = reduce(math.gcd, numbers)\nprint(f&quot;\u5217\u8868{numbers}\u7684\u6700\u5927\u516c\u7ea6\u6570\u662f{result}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u4efb\u610f\u6570\u91cf\u7684\u6574\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6700\u5927\u516c\u7ea6\u6570\uff08Greatest Common Divisor, GCD\uff09\u662f\u6307\u80fd\u591f\u6574\u9664\u4e24\u4e2a\u6216\u591a\u4e2a\u6574\u6570\u7684\u6700\u5927\u6574\u6570\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1097911,"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\/1097905"}],"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=1097905"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097905\/revisions"}],"predecessor-version":[{"id":1097914,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097905\/revisions\/1097914"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097911"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}