在1~100之间产生3个随机整数,a,b,c,求a,b,c最大公约数和最小公倍数
时间: 2024-05-05 11:15:51 浏览: 101
java求两个整数最大公约数和最小公倍数
5星 · 资源好评率100%
可以先写一个求两个数最大公约数和最小公倍数的函数gcd(a,b)和lcm(a,b),然后分别调用三次即可。
求最大公约数的函数:
```python
def gcd(a, b):
if a < b:
a, b = b, a
while b != 0:
a, b = b, a % b
return a
```
求最小公倍数的函数:
```python
def lcm(a, b):
return a * b // gcd(a, b)
```
然后产生三个随机数,调用函数即可:
```python
import random
a = random.randint(1, 100)
b = random.randint(1, 100)
c = random.randint(1, 100)
print("a = ", a)
print("b = ", b)
print("c = ", c)
gcd_ab = gcd(a, b)
gcd_ac = gcd(a, c)
gcd_bc = gcd(b, c)
lcm_abc = lcm(lcm(a, b), c)
print("gcd(a, b, c) = ", gcd(gcd_ab, c))
print("lcm(a, b, c) = ", lcm_abc)
```
输出结果示例:
```
a = 48
b = 20
c = 56
gcd(a, b, c) = 4
lcm(a, b, c) = 6720
```
阅读全文