python lcm
时间: 2023-10-19 10:31:20 浏览: 111
LCM (Least Common Multiple) in Python can be calculated using the following code:
```python
def lcm(a, b):
"""Function to calculate the LCM of two numbers"""
# Find the greater number among a and b
max_num = max(a, b)
# Loop until we find the LCM
while True:
if max_num % a == 0 and max_num % b == 0:
lcm = max_num
break
max_num += 1
return lcm
# Example usage
num1 = 4
num2 = 6
result = lcm(num1, num2)
print("The LCM of", num1, "and", num2, "is", result)
```
In this example, the `lcm()` function takes two numbers as input and finds the LCM by iterating from the greater number until it finds a number that is divisible by both input numbers.
阅读全文