Chương trình tính toán LCM bằng GCD
Trước khi tìm hiểu cách tìm lcm trong python bằng gcd hay HCF (Highest Common Factor) trước tiên chúng ta nên biết gcd là gì
GCD là viết tắt của Ước chung lớn nhất. Ước chung lớn nhất của hai hay nhiều số là số nguyên dương lớn nhất chia hết cho mỗi số
Chúng ta có thể tạo cả hai số nếu biết lcm và HCF của hai số. Công thức cơ bản là
axb=lcm(a,b)∗gcd(a,b)a x b = lcm(a, b) * gcd(a, b)axb=lcm(a,b)∗gcd(a,b)
Vì thế,
LCM(a,b)=(a∗b)/gcd(a,b)LCM(a, b) = (a * b) / gcd(a, b)LCM(a,b)=(a∗b)
Bây giờ, chúng ta có thể tính toán gcd của hai hoặc nhiều số bằng cách sử dụng đệ quy và hàm gcd của mô-đun toán học
Hệ số chung cao nhất (HCF), còn được gọi là gcd, có thể được tính toán trong python bằng một hàm duy nhất do mô-đun toán học cung cấp và do đó có thể thực hiện các tác vụ dễ dàng hơn trong nhiều tình huống
Phương pháp ngây thơ để tính toán gcd
cách 1. Sử dụng đệ quy
Python3
The gcd of 60 and 48 is : 12
0The gcd of 60 and 48 is : 12
1
The gcd of 60 and 48 is : 12
2 The gcd of 60 and 48 is : 12
3The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
5______46The gcd of 60 and 48 is : 12
0The gcd of 60 and 48 is : 12
0 The gcd of 60 and 48 is : 12
2The gcd of 60 and 48 is : 12
3The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
5 The gcd of 60 and 48 is : 12
6The gcd of 60 and 48 is : 12
7The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
9The gcd of 60 and 48 is : 12
0The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
5 The gcd of 60 and 48 is : 12
3The gcd of 60 and 48 is : 12
4 The gcd of 60 and 48 is : 12
5
The gcd of 60 and 48 is : 12
6The gcd of 60 and 48 is : 12
0 The gcd of 60 and 48 is : 12
8The gcd of 60 and 48 is : 12
9The gcd of 60 and 48 is : 12
0 The gcd of 60 and 48 is : 12
01
The gcd of 60 and 48 is : 12
02The gcd of 60 and 48 is : 12
03The gcd of 60 and 48 is : 12
04The gcd of 60 and 48 is : 12
05The gcd of 60 and 48 is : 12
06The gcd of 60 and 48 is : 12
0The gcd of 60 and 48 is : 12
08The gcd of 60 and 48 is : 12
03The gcd of 60 and 48 is : 12
10The gcd of 60 and 48 is : 12
8The gcd of 60 and 48 is : 12
12The gcd of 60 and 48 is : 12
01The gcd of 60 and 48 is : 12
14Đầu ra
The gcd of 60 and 48 is : 12
Cách 2. Sử dụng vòng lặp
Python3
The gcd of 60 and 48 is : 12
0The gcd of 60 and 48 is : 12
16
The gcd of 60 and 48 is : 12
2 The gcd of 60 and 48 is : 12
18
The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
5 The gcd of 60 and 48 is : 12
21The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
23The gcd of 60 and 48 is : 12
0 The gcd of 60 and 48 is : 12
25The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
9The gcd of 60 and 48 is : 12
0The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
23The gcd of 60 and 48 is : 12
0 The gcd of 60 and 48 is : 12
32The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
34 The gcd of 60 and 48 is : 12
35The gcd of 60 and 48 is : 12
36 The gcd of 60 and 48 is : 12
37The gcd of 60 and 48 is : 12
04The gcd of 60 and 48 is : 12
39The gcd of 60 and 48 is : 12
40The gcd of 60 and 48 is : 12
41 The gcd of 60 and 48 is : 12
39The gcd of 60 and 48 is : 12
3The gcd of 60 and 48 is : 12
4The gcd of 60 and 48 is : 12
5The gcd of 60 and 48 is : 12
46The gcd of 60 and 48 is : 12
4 The gcd of 60 and 48 is : 12
35The gcd of 60 and 48 is : 12
0The gcd of 60 and 48 is : 12
0 The gcd of 60 and 48 is : 12
2The gcd of 60 and 48 is : 12
52The gcd of 60 and 48 is : 12
53 The gcd of 60 and 48 is : 12
54The gcd of 60 and 48 is : 12
4 The gcd of 60 and 48 is : 12
35The gcd of 60 and 48 is : 12
0The gcd of 60 and 48 is : 12
0 The gcd of 60 and 48 is : 12
2The gcd of 60 and 48 is : 12
60