lcm两个正整数的最小公倍数
时间: 2024-06-13 12:09:15 浏览: 13
以下是两种求两个正整数最小公倍数的方法:
1.使用辗转相除法
```python
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def lcm(a, b):
return a * b // gcd(a, b)
c = int(input("请输入第一个数:"))
d = int(input("请输入第二个数:"))
print("这两个数的最小公倍数:")
print(lcm(c, d))
```
2.使用for循环递减的方式
```python
def lcm(a, b):
for i in range(max(a, b), a * b + 1):
if i % a == 0 and i % b == 0:
return i
c = int(input("请输入第一个数:"))
d = int(input("请输入第二个数:"))
print("这两个数的最小公倍数:")
print(lcm(c, d))
```
相关问题
求两个正整数的最小公倍数
好的,下面是一个求两个正整数最小公倍数的 Python 函数:
```python
def lcm(a, b):
"""
计算两个正整数的最小公倍数
"""
# 先求出两个数的最大公约数
def gcd(x, y):
while y:
x, y = y, x % y
return x
# 最小公倍数为两数之积除以最大公约数
return a * b // gcd(a, b)
```
使用示例:
```python
>>> lcm(6, 9)
18
>>> lcm(12, 18)
36
>>> lcm(8, 12)
24
```
这个函数先通过辗转相除法求出两个数的最大公约数,然后用两个数的积除以最大公约数得到最小公倍数。
两个正整数的最小公倍数
根据引用\[1\]和引用\[2\]中的内容,求两个正整数的最小公倍数可以使用以下几种算法:辗转相除法、穷举法和Stein算法。其中,辗转相除法是最常用的方法。辗转相除法的原理是通过反复用较小数去除较大数,直到余数为0,此时较小数就是最大公约数,然后通过两个数的乘积除以最大公约数得到最小公倍数。代码实现如下:
```
public static int lcm(int a, int b) {
int gcd = gcd(a, b); // 求最大公约数
return a * b / gcd; // 求最小公倍数
}
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
```
通过调用lcm函数,传入两个正整数a和b,即可得到它们的最小公倍数。例如,对于两个正整数a=12和b=18,调用lcm(12, 18)即可得到它们的最小公倍数为36。
#### 引用[.reference_title]
- *1* *3* [求两个正整数的最大公约数和最小公倍数。](https://blog.csdn.net/dtrder/article/details/82500870)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [求两个正整数的最大公因数和最小公倍数](https://blog.csdn.net/csdnwqy030429/article/details/120942502)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)