<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國際加速解決方案。 廣告
                # Kotlin 程序:查找兩個數字的 LCM > 原文: [https://www.programiz.com/kotlin-programming/examples/lcm](https://www.programiz.com/kotlin-programming/examples/lcm) #### 在此程序中,您將學習使用 GCD 而不是 GCD 查找兩個數字的 lcm。 這是通過 Kotlin 中的`while`循環完成的。 兩個整數的 LCM 是可以被兩個數字完全除(沒有余數)的最小正整數。 ## 示例 1:使用`while`循環和`if`語句計算 LCM ```kt fun main(args: Array<String>) { val n1 = 72 val n2 = 120 var lcm: Int // maximum number between n1 and n2 is stored in lcm lcm = if (n1 > n2) n1 else n2 // Always true while (true) { if (lcm % n1 == 0 && lcm % n2 == 0) { println("The LCM of $n1 and $n2 is $lcm.") break } ++lcm } } ``` 運行該程序時,輸出為: ```kt The LCM of 72 and 120 is 360. ``` 在此程序中,將找到其 LCM 的兩個數字分別存儲在變量`n1`和`n2`中。 然后,我們最初將`lcm`設置為兩個數字中的最大值。 這是因為 LCM 不能小于最大數量。 與 Java 類似,在無限`while`循環(`while(true)`)中,我們檢查`lcm`是否完美地劃分了`n1`和`n2`。 如果是這樣,我們已經找到 LCM。 我們打印 LCM 并使用`break`語句退出`while`循環。 否則,我們將`lcm`加 1,然后重新測試除數條件。 這是等效的 Java 代碼:[查找兩個數字的 LCM 的 Java 程序](/java-programming/examples/lcm "How to find LCM of two numbers in Java?")。 * * * 我們還可以使用 GCD 通過以下公式查找兩個數字的 LCM: ```kt LCM = (n1 * n2) / GCD ``` 如果您不知道如何用 Java 計算 GCD,請檢查 [Kotlin 程序以找到兩個數字](/kotlin-programming/examples/hcf-gcd "How to find GCD of two numbers in Kotlin?")的 GCD。 ## 示例 2:使用 GCD 計算 LCM ```kt fun main(args: Array<String>) { val n1 = 72 val n2 = 120 var gcd = 1 var i = 1 while (i <= n1 && i <= n2) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) gcd = i ++i } val lcm = n1 * n2 / gcd println("The LCM of $n1 and $n2 is $lcm.") } ``` 該程序的輸出與示例 1 相同。 在這里,在`while`循環內,我們計算了兩個數字的 GCD - `n1`和`n2`。 計算后,我們使用上面的公式來計算 LCM。
                  <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>

                              哎呀哎呀视频在线观看