<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python 程序:查找 LCM > 原文: [https://www.programiz.com/python-programming/examples/lcm](https://www.programiz.com/python-programming/examples/lcm) #### 在此程序中,您將學習找到兩個數字的 LCM 并顯示它。 要理解此示例,您應該了解以下 [Python 編程](/python-programming "Python tutorial")主題: * [Python `while`循環](/python-programming/while-loop) * [Python 函數](/python-programming/function) * [Python 函數參數](/python-programming/function-argument) * [Python 用戶定義函數](/python-programming/user-defined-function) * * * 兩個數字的最小公倍數(LCM)是可以被兩個給定數字完全整除的最小正整數。 例如 12 和 14 的 LCM 是 84。 ## 計算 LCM 的程序 ```py # Python Program to find the LCM of two input number def compute_lcm(x, y): # choose the greater number if x > y: greater = x else: greater = y while(True): if((greater % x == 0) and (greater % y == 0)): lcm = greater break greater += 1 return lcm num1 = 54 num2 = 24 print("The LCM is", compute_lcm(num1, num2)) ``` **輸出** ```py The LCM is 216 ``` **注意**:要測試該程序,請更改`num1`和`num2`的值。 該程序分別在`num1`和`num2`中存儲兩個數字。 這些數字將傳遞到`compute_lcm()`函數。 該函數返回兩個數字的 LCM。 在函數中,我們首先確定兩個數字中的較大者,因為 LCM 只能大于或等于最大數。 然后,我們使用無限`while`循環從該數字開始及以后。 在每次迭代中,我們檢查兩個數字是否完美地除以我們的數字。 如果是這樣,我們將數字存儲為 LCM。 并擺脫循環。 否則,數字將增加 1,然后循環繼續。 上面的程序運行較慢。 我們可以利用兩個數的乘積等于這兩個數的最小公倍數和最大公除數的乘積來提高效率。 ```py Number1 * Number2 = LCM * GCD. ``` 這是一個實現此目的的 Python 程序。 ## 使用 GCD 計算 LCM 的程序 ```py # Python program to find the LCM of two input number # This function computes GCD def compute_gcd(x, y): while(y): x, y = y, x % y return x # This function computes LCM def compute_lcm(x, y): lcm = (x*y)//compute_gcd(x,y) return lcm num1 = 54 num2 = 24 print("The LCM is", compute_lcm(num1, num2)) ``` 該程序的輸出與以前相同。 我們有兩個函數`compute_gcd()`和`compute_lcm()`。 我們需要 GCD. 的數字以計算其 LCM 因此,`compute_lcm()`調用函數`compute_gcd()`完成此操作。 GCD 使用歐幾里得算法可以有效地計算出兩個數之和。 單擊此處以了解有關[用 Python](/python-programming/examples/hcf "Python program to find GCD") 計算 GCD 的方法的更多信息。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看