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

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ã

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 : 120

The gcd of 60 and 48 is : 121

 

The gcd of 60 and 48 is : 122 The gcd of 60 and 48 is : 123

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125______46The gcd of 60 and 48 is : 120The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 122The gcd of 60 and 48 is : 123

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 127

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 129The gcd of 60 and 48 is : 120

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 123The gcd of 60 and 48 is : 124 The gcd of 60 and 48 is : 125

 

The gcd of 60 and 48 is : 126The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 128

The gcd of 60 and 48 is : 129The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 1201

 

The gcd of 60 and 48 is : 1202

The gcd of 60 and 48 is : 1203The gcd of 60 and 48 is : 1204The gcd of 60 and 48 is : 1205The gcd of 60 and 48 is : 1206The gcd of 60 and 48 is : 120The gcd of 60 and 48 is : 1208

The gcd of 60 and 48 is : 1203The gcd of 60 and 48 is : 1210The gcd of 60 and 48 is : 128The gcd of 60 and 48 is : 1212The gcd of 60 and 48 is : 1201The gcd of 60 and 48 is : 1214

Đầu raThe 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 : 120

The gcd of 60 and 48 is : 1216

 

The gcd of 60 and 48 is : 122 The gcd of 60 and 48 is : 1218

 

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125 The gcd of 60 and 48 is : 1221

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 1223The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 1225

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 129The gcd of 60 and 48 is : 120

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 1223The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 1232

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 1234 The gcd of 60 and 48 is : 1235The gcd of 60 and 48 is : 1236 The gcd of 60 and 48 is : 1237The gcd of 60 and 48 is : 1204The gcd of 60 and 48 is : 1239The gcd of 60 and 48 is : 1240The gcd of 60 and 48 is : 1241 The gcd of 60 and 48 is : 1239The gcd of 60 and 48 is : 123

The gcd of 60 and 48 is : 124The gcd of 60 and 48 is : 125The gcd of 60 and 48 is : 1246The gcd of 60 and 48 is : 124 The gcd of 60 and 48 is : 1235The gcd of 60 and 48 is : 120The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 122The gcd of 60 and 48 is : 1252The gcd of 60 and 48 is : 1253 The gcd of 60 and 48 is : 1254The gcd of 60 and 48 is : 124 The gcd of 60 and 48 is : 1235The gcd of 60 and 48 is : 120The gcd of 60 and 48 is : 120 The gcd of 60 and 48 is : 122The gcd of 60 and 48 is : 1260

Chủ đề