Python 基礎知識簡單

最小公倍數 (LCM)

「最低公倍數 (LCM)」問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 find_lcm(a, b),它接受兩個正整數 ab 並傳回它們的最小公倍數 (LCM)。 LCM 是可被 ab 整除的最小正整數。

提示:您可以使用關係式 LCM(a, b) = (a * b) / GCD(a, b)。

約束條件
  • 1 <= a, b <= 10^6

範例

Example 1
Input
find_lcm(4, 6)
Output
12
Explanation

Multiples of 4: 4, 8, 12, 16, ... Multiples of 6: 6, 12, 18, ... The smallest common multiple is 12.

Example 2
Input
find_lcm(3, 5)
Output
15
Explanation

Multiples of 3: 3, 6, 9, 12, 15, ... Multiples of 5: 5, 10, 15, ... The smallest common multiple is 15.

Example 3
Input
find_lcm(12, 18)
Output
36
Explanation

GCD(12,18) = 6. LCM = (12 * 18) / 6 = 216 / 6 = 36.

Need a Hint?
考慮使用特定於數字的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。