Làm thế nào để bạn sử dụng lcm và gcd trong python?

LCM là viết tắt của Bội số chung nhỏ nhất. Bội số chung nhỏ nhất hoặc Bội số chung thấp nhất xuất phát từ số học và lý thuyết số. Lcm của hai số trong python là số hoặc giá trị dương nhỏ nhất có thể chia hết cho cả hai số được cung cấp

LCM của hai số (giả sử a và b) được ký hiệu là lcm(a, b)

Ghi chú

Như chúng ta đã biết, khi chúng ta chia một số cho 0, kết quả là không xác định. Vì vậy, cả a và b không được bằng 0

Ví dụ: LCM của 15 và 20 là 60;

Có một số cách tính lcm của hai số trong python. Chúng ta có thể tìm LCM của hai hoặc nhiều số bằng vòng lặp, phương thức gcd và các hàm dựng sẵn

Tham khảo hình ảnh cung cấp dưới đây để hiểu rõ hơn

Làm thế nào để bạn sử dụng lcm và gcd trong python?

Hãy cùng tìm hiểu về các cách tính lcm của hai số trong python

Chương trình Python để tính toán LCM

Viết chương trình tìm ƯCLN của hai số. Trước khi thực sự đi sâu vào mã, trước tiên chúng ta hãy tạo một biểu đồ luồng để trực quan hóa hoạt động của mã

Làm thế nào để bạn sử dụng lcm và gcd trong python?

Tên chức năng. lcm(a, b)

Đầu tiên, chúng ta sẽ tính số lớn hơn giữa a và b. Sau khi tính toán số lượng lớn hơn trong số các tham số, chúng tôi sẽ chạy một vòng lặp vô hạn. Ta sẽ chia số lớn hơn cho a và b. Nếu cả hai số có thể chia hết số lớn hơn, chúng tôi đã tìm thấy lcm của mình. Nếu không, chúng ta sẽ tăng số lớn hơn lên 111 và thực hiện quá trình tương tự như trên cho đến khi số lớn hơn chia hết hai số a và b

Hãy tính lcm của 4 và 6

Mã số

def lcm(a, b):
    if a > b:
        greater = a
    else:
        greater = b
    while(True):
        if((greater % a == 0) and (greater % b == 0)):
            lcm = greater
            break
        greater += 1
    return lcm


if __name__ == '__main__':
    print("LCM = ", lcm(4, 6))

đầu ra

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
0

The 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
3

The gcd of 60 and 48 is : 12
4
The gcd of 60 and 48 is : 12
5______46
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
2
The gcd of 60 and 48 is : 12
3

The 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
6
The gcd of 60 and 48 is : 12
7

The gcd of 60 and 48 is : 12
4
The gcd of 60 and 48 is : 12
9
The gcd of 60 and 48 is : 12
0

The 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
3
The 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
6
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
8

The gcd of 60 and 48 is : 12
9
The 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
02

The gcd of 60 and 48 is : 12
03
The gcd of 60 and 48 is : 12
04
The gcd of 60 and 48 is : 12
05
The gcd of 60 and 48 is : 12
06
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
08

The gcd of 60 and 48 is : 12
03
The gcd of 60 and 48 is : 12
10
The gcd of 60 and 48 is : 12
8
The gcd of 60 and 48 is : 12
12
The gcd of 60 and 48 is : 12
01
The 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
0

The 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
4
The gcd of 60 and 48 is : 12
5
The gcd of 60 and 48 is : 12
21

The gcd of 60 and 48 is : 12
4
The gcd of 60 and 48 is : 12
23
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
25

The gcd of 60 and 48 is : 12
4
The gcd of 60 and 48 is : 12
9
The gcd of 60 and 48 is : 12
0

The gcd of 60 and 48 is : 12
4
The gcd of 60 and 48 is : 12
23
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
32

The gcd of 60 and 48 is : 12
4
The gcd of 60 and 48 is : 12
34
The gcd of 60 and 48 is : 12
35
The gcd of 60 and 48 is : 12
36
The gcd of 60 and 48 is : 12
37
The gcd of 60 and 48 is : 12
04
The gcd of 60 and 48 is : 12
39
The gcd of 60 and 48 is : 12
40
The gcd of 60 and 48 is : 12
41
The gcd of 60 and 48 is : 12
39
The gcd of 60 and 48 is : 12
3

The 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
46
The gcd of 60 and 48 is : 12
4
The gcd of 60 and 48 is : 12
35
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
2
The gcd of 60 and 48 is : 12
52
The gcd of 60 and 48 is : 12
53
The gcd of 60 and 48 is : 12
54
The gcd of 60 and 48 is : 12
4
The gcd of 60 and 48 is : 12
35
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
0
The gcd of 60 and 48 is : 12
2
The gcd of 60 and 48 is : 12
60