Cải thiện bài viết
Lưu bài viết
Yếu tố chung cao nhất [HCF], còn được gọi là GCD, có thể được tính toán bằng Python bằng cách sử dụng một hàm duy nhất được cung cấp bởi mô -đun toán học và do đó có thể giúp các nhiệm vụ dễ dàng hơn trong nhiều tình huống.math module and hence can make tasks easier in many situations.
Phương pháp ngây thơ để tính toán GCD
Cách 1: Sử dụng đệ quy Using Recursion
Python3
def
hcfnaive[a, b]:
Các
The gcd of 60 and 48 is : 125
The gcd of 60 and 48 is : 126
The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 128
The gcd of 60 and 48 is : 120
The gcd of 60 and 48 is : 121
The gcd of 60 and 48 is : 125
The gcd of 60 and 48 is : 126
The gcd of 60 and 48 is : 124
The gcd of 60 and 48 is : 125
The gcd of 60 and 48 is : 126
The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 121
The gcd of 60 and 48 is : 129
# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
0____11 # Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
2# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
3# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
4# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
5# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
6The gcd of 60 and 48 is : 121
# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
8# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
3The H.C.F. is 60
The gcd of 60 and 48 is : 129
The H.C.F. is 62
# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
2The H.C.F. is 64
Đầu ra
The gcd of 60 and 48 is : 12
Cách 2: Sử dụng các vòng & NBSP;Using Loops
Python3
def
The H.C.F. is 66
if
The H.C.F. is 69
The gcd of 60 and 48 is : 125
# Function to find HCF the Using Euclidian algorithm
def compute_hcf[x, y]:
while[y]:
x, y = y, x % y
return x
hcf = compute_hcf[300, 400]
print["The HCF is", hcf]
1The gcd of 60 and 48 is : 121
# Function to find HCF the Using Euclidian algorithm
def compute_hcf[x, y]:
while[y]:
x, y = y, x % y
return x
hcf = compute_hcf[300, 400]
print["The HCF is", hcf]
3
The gcd of 60 and 48 is : 120
The gcd of 60 and 48 is : 121
The gcd of 60 and 48 is : 125
# Function to find HCF the Using Euclidian algorithm
def compute_hcf[x, y]:
while[y]:
x, y = y, x % y
return x
hcf = compute_hcf[300, 400]
print["The HCF is", hcf]
1The gcd of 60 and 48 is : 121
def
0
def
2 def
3__
The gcd of 60 and 48 is : 125
if
hcfnaive[a, b]:
4The gcd of 60 and 48 is : 125
def
3__
9if
0
The gcd of 60 and 48 is : 121
if
2
The gcd of 60 and 48 is : 126
if
5The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 121
The gcd of 60 and 48 is : 129
# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
0____11 # Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
2Đầu ra
Cách 2: Sử dụng các vòng & NBSP;
Đầu ra
The gcd of 60 and 48 is : 12
Cách 2: Sử dụng các vòng & NBSP;Using Euclidean Algorithm
Python3
def
The H.C.F. is 66
The gcd of 60 and 48 is : 1217
The gcd of 60 and 48 is : 1218
if
The H.C.F. is 69
The gcd of 60 and 48 is : 125
# Function to find HCF the Using Euclidian algorithm
def compute_hcf[x, y]:
while[y]:
x, y = y, x % y
return x
hcf = compute_hcf[300, 400]
print["The HCF is", hcf]
1The gcd of 60 and 48 is : 121
# Function to find HCF the Using Euclidian algorithm
def compute_hcf[x, y]:
while[y]:
x, y = y, x % y
return x
hcf = compute_hcf[300, 400]
print["The HCF is", hcf]
3The gcd of 60 and 48 is : 127
The gcd of 60 and 48 is : 121
The gcd of 60 and 48 is : 129
# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
0____11 # Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
2Đầu ra
Cách 2: Sử dụng các vòng & NBSP;
Output:
The gcd of 60 and 48 is : 12
def
The H.C.F. is 6
6if
The H.C.F. is 6
9
Trong ví dụ này, bạn sẽ học cách tìm GCD của hai số bằng hai phương pháp khác nhau: hàm và vòng lặp và, thuật toán Euclidean
Để hiểu ví dụ này, bạn nên có kiến thức về các chủ đề lập trình Python sau:
- Chức năng Python
- Đệ quy Python
- Đối số chức năng Python
Yếu tố chung cao nhất [H.C.F] hoặc ước số chung lớn nhất [G.C.D] của hai số là số nguyên dương lớn nhất phân chia hoàn hảo hai số đã cho. Ví dụ, H.C.F của 12 và 14 là 2.
Mã nguồn: Sử dụng các vòng lặp
# Python program to find H.C.F of two numbers
# define a function
def compute_hcf[x, y]:
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range[1, smaller+1]:
if[[x % i == 0] and [y % i == 0]]:
hcf = i
return hcf
num1 = 54
num2 = 24
print["The H.C.F. is", compute_hcf[num1, num2]]
Đầu ra
The H.C.F. is 6
Ở đây, hai số nguyên được lưu trữ trong các biến NUM1 và NUM2 được truyền đến hàm
The gcd of 60 and 48 is : 1247. Hàm tính toán H.C.F. Hai số này và trả lại nó.
Trong hàm, trước tiên chúng tôi xác định số nhỏ hơn của hai số vì H.C.F chỉ có thể nhỏ hơn hoặc bằng số nhỏ nhất. Sau đó, chúng tôi sử dụng một vòng def
2 để đi từ 1 đến số đó.
Trong mỗi lần lặp, chúng tôi kiểm tra xem số của chúng tôi có phân chia hoàn hảo cả hai số đầu vào không. Nếu vậy, chúng tôi lưu trữ số dưới dạng H.C.F. Khi hoàn thành vòng lặp, chúng tôi kết thúc với con số lớn nhất phân chia hoàn hảo cả hai số.
Phương pháp trên rất dễ hiểu và thực hiện nhưng không hiệu quả. Một phương pháp hiệu quả hơn nhiều để tìm H.C.F. là thuật toán Euclide.
Thuật toán Euclide
Thuật toán này dựa trên thực tế là H.C.F. của hai con số phân chia sự khác biệt của chúng là tốt.
Trong thuật toán này, chúng tôi chia lớn hơn cho nhỏ hơn và lấy phần còn lại. Bây giờ, chia nhỏ hơn cho phần còn lại này. Lặp lại cho đến khi phần còn lại là 0.
Ví dụ: nếu chúng ta muốn tìm H.C.F. của 54 và 24, chúng tôi chia 54 cho 24. Phần còn lại là 6. Bây giờ, chúng tôi chia 24 cho 6 và phần còn lại là 0. Do đó, 6 là h.c.f.
Mã nguồn: Sử dụng thuật toán Euclide
# Function to find HCF the Using Euclidian algorithm
def compute_hcf[x, y]:
while[y]:
x, y = y, x % y
return x
hcf = compute_hcf[300, 400]
print["The HCF is", hcf]
Ở đây chúng tôi lặp cho đến khi y trở thành không. Tuyên bố
The gcd of 60 and 48 is : 1249 thực hiện hoán đổi các giá trị trong Python. Nhấn vào đây để tìm hiểu thêm về việc hoán đổi các biến trong Python.
Trong mỗi lần lặp, chúng tôi đặt giá trị của y trong x và phần còn lại
The gcd of 60 and 48 is : 1250 trong y, đồng thời. Khi y trở thành 0, chúng ta có H.C.F. trong x.