Hướng dẫn how do you calculate gcd in python? - làm thế nào để bạn tính toán gcd trong python?

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

    

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

# 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))
6
The gcd of 60 and 48 is : 12
1
# 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))
3
The H.C.F. is 6
0
The gcd of 60 and 48 is : 12
9
The H.C.F. is 6
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))
2
The H.C.F. is 6
4

Đầ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 6
6

    if

The H.C.F. is 6
9

The gcd of 60 and 48 is : 12
5
# 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)
1
The gcd of 60 and 48 is : 12
1
# 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 : 12
0
The gcd of 60 and 48 is : 12
1

The gcd of 60 and 48 is : 12
5
# 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)
1
The gcd of 60 and 48 is : 12
1 def0

    def2 def3__

The gcd of 60 and 48 is : 12
5ifhcfnaive(a, b):4
The gcd of 60 and 48 is : 12
5 def3__

    9if0

The gcd of 60 and 48 is : 12
1 if2

    

The gcd of 60 and 48 is : 12
6 if5

The gcd of 60 and 48 is : 12
7
The gcd of 60 and 48 is : 12
1
The gcd of 60 and 48 is : 12
9

# 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 6
6

    

The gcd of 60 and 48 is : 12
17
The gcd of 60 and 48 is : 12
18

    if

The H.C.F. is 6
9

The gcd of 60 and 48 is : 12
5
# 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)
1
The gcd of 60 and 48 is : 12
1
# 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 : 12
7
The gcd of 60 and 48 is : 12
1
The gcd of 60 and 48 is : 12
9

# 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
    
    6
  •     if
    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 : 12
47. 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 def2 để đ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 : 12
49 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 : 12
50 trong y, đồng thời. Khi y trở thành 0, chúng ta có H.C.F. trong x.

Python có GCD không?

Ưu điểm phổ biến lớn nhất hoặc GCD là một biểu thức toán học để tìm số lượng cao nhất có thể chia cả hai số mà GCD phải tìm thấy với phần còn lại là không.Nó có nhiều ứng dụng toán học.Python có chức năng GCD sẵn có trong mô -đun toán học có thể được sử dụng cho mục đích này.Python has a inbuilt gcd function in the math module which can be used for this purpose.

Công thức tính toán GCD là gì?

Theo phương pháp LCM, chúng ta có thể có được GCD của bất kỳ hai số nguyên dương nào bằng cách tìm sản phẩm của cả hai số và bội số phổ biến nhất của cả hai số.Phương pháp LCM để có được ước số chung lớn nhất được đưa ra dưới dạng GCD (A, B) = (A × B)/ LCM (A, B).GCD (a, b) = (a × b)/ LCM (a, b).