Ref Math Lcm
The `math.lcm()` function was introduced in Python 3.9. It is used to calculate the Least Common Multiple (LCM) of two or more integer arguments.
In number theory, the Least Common Multiple (LCM) of two or more integers is the smallest positive integer that is divisible by all of them.
---
## Syntax and Parameters
### Syntax
```python
import math
math.lcm(*integers)
```
### Parameter Description
* `*integers`: One or more integer values.
### Return Value
* Returns the least common multiple of all input integers.
* If only one integer is passed, it returns the absolute value of that integer.
* If any of the arguments is `0`, then the returned value is `0`.
* Calling `math.lcm()` without any arguments returns `1` (according to standard Python documentation).
---
## Examples
### Example 1: Basic Usage - Calculating LCM of Two Numbers
```python
import math
print("LCM of 4 and 6:", math.lcm(4, 6)) # Output: 12
print("LCM of 5 and 7:", math.lcm(5, 7)) # Output: 35
print("LCM of 12 and 18:", math.lcm(12, 18)) # Output: 36
# Verifying the formula: LCM(a, b) = a * b / GCD(a, b)
a, b = 4, 6
print(f"Verify: LCM({a}, {b}) = {a} * {b} / GCD({a}, {b}) = {a * b // math.gcd(a, b)}")
```
**Output:**
```text
LCM of 4 and 6: 12
LCM of 5 and 7: 35
LCM of 12 and 18: 36
Verify: LCM(4, 6) = 4 * 6 / GCD(4, 6) = 12
```
### Example 2: Calculating LCM of Multiple Numbers
```python
import math
print("LCM of 3, 4, 5:", math.lcm(3, 4, 5)) # Output: 60
print("LCM of 6, 8, 12:", math.lcm(6, 8, 12)) # Output: 24
print("LCM of 2, 3, 4, 5:", math.lcm(2, 3, 4, 5)) # Output: 60
```
**Output:**
```text
LCM of 3, 4, 5: 60
LCM of 6, 8, 12: 24
LCM of 2, 3, 4, 5: 60
```
### Example 3: Handling Special Values
```python
import math
print("Single integer 7:", math.lcm(7))
print("LCM of 0 and 5:", math.lcm(0, 5))
print("LCM of -4 and 6:", math.lcm(-4, 6))
```
**Output:**
```text
Single integer 7: 7
LCM of 0 and 5: 0
LCM of -4 and 6: 12
```
### Example 4: Practical Application - Fraction Addition (Common Denominator)
```python
import math
# Calculate 1/4 + 1/6
denom1, denom2 = 4, 6
common_denom = math.lcm(denom1, denom2)
numer1 = 1 * (common_denom // denom1)
numer2 = 1 * (common_denom // denom2)
result = numer1 + numer2
print(f"1/{denom1} + 1/{denom2} = {numer1}/{common_denom} + {numer2}/{common_denom} = {result}/{common_denom}")
```
**Output:**
```text
1/4 + 1/6 = 3/12 + 2/12 = 5/12
```
### Example 5: Practical Application - Cycle Synchronization
```python
import math
# Three traffic lights have cycles of 30, 45, and 60 seconds
light_cycles = [30, 45, 60]
sync_interval = math.lcm(*light_cycles)
print(f"Traffic light cycles: {light_cycles}")
print(f"Interval for all lights to turn green simultaneously: {sync_interval} seconds")
```
**Output:**
```text
Traffic light cycles: [30, 45, 60]
Interval for all lights to turn green simultaneously: 180 seconds
```
---
## Relationship with GCD (Greatest Common Divisor)
The Least Common Multiple (LCM) and Greatest Common Divisor (GCD) are related by the following formula:
$$ ext{LCM}(a, b) = rac{|a imes b|}{ ext{GCD}(a, b)}$$
We can verify this mathematical relationship in Python:
```python
import math
for a, b in [(4, 6), (8, 12), (15, 20)]:
lcm_val = math.lcm(a, b)
gcd_val = math.gcd(a, b)
print(f"LCM({a}, {b}) = {lcm_val}, GCD({a}, {b}) = {gcd_val}, Verify: {lcm_val * gcd_val} == {a * b} -> {lcm_val * gcd_val == a * b}")
```
**Output:**
```text
LCM(4, 6) = 12, GCD(4, 6) = 2, Verify: 24 == 24 -> True
LCM(8, 12) = 24, GCD(8, 12) = 4, Verify: 96 == 96 -> True
LCM(15, 20) = 60, GCD(15, 20) = 5, Verify: 300 == 300 -> True
```
---
## Important Considerations
* `math.lcm()` is only available in **Python 3.9 and newer versions**.
* Negative integers are automatically converted to their absolute values before calculation.
* If any of the arguments is `0`, the least common multiple returned will be `0`.
YouTip