How do you find the gcd between two numbers in python?

Greatest Common Divisor (GCD) is a mathematical term to find the greatest common factor that can perfectly divide the two numbers. A GCD is also known as the Highest Common Factor (HCF). For example, the HCF/ GCD of two numbers 54 and 24 is 6. Because 6 is the largest common divisor that completely divides 54 and 24.

How do you find the gcd between two numbers in python?

GCD Using gcd() Function

In python, a gcd() is an inbuilt function offered by the math module to find the greatest common divisor of two numbers.

Syntax

Where a and b are the two integer number passes as an argument to the function gcd().

Let's create a program to print the GCD of two number using the inbuilt function of math.gcd() in python.

math_fun.py

Output:

How do you find the gcd between two numbers in python?

In the above example, math.gcd() function generates the GCD of two given numbers. In the gcd() function, a and b pass as an argument that returns the greatest common divisor of two integer numbers, completely dividing the numbers.

GCD Using recursion

Recursion is a memory consuming function defined in python that calls itself via self-referential expression. It means that the function will continuously call and repeat itself until the defined condition is met to return the greatest common divisor of the number.

Pseudo Code of the Algorithm

Step 1: Take two inputs, x and y, from the user.

Step 2: Pass the input number as an argument to a recursive function.

Step 3: If the second number is equal to zero (0), it returns the first number.

Step 4: Else it recursively calls the function with the second number as an argument until it gets the remainder, which divides the second number by the first number.

Step 5: Call or assign the gcd_fun() to a variable.

Step 6: Display the GCD of two numbers.

Step 7: Exit from the program.

Let's understand the program to find the GCD of two number using the recursion.

gcdRecur.py

Output:

How do you find the gcd between two numbers in python?

GCD Using the Loop

Let's create program to find the GCD of two number in python using the loops.

gcdFile.py

Output:

How do you find the gcd between two numbers in python?

As we can see in the above program, we take two values as input and pass these numbers to the GCD_Loop () function to return a GCD.

GCD Using Euclid's algorithm or Euclidean Algorithm

Euclid's algorithm is an efficient method to find the greatest common divisor of two numbers. It is the oldest algorithm that divides the greater number into smaller ones and takes the remainder. Again, it divides the smaller number from the remainder, and this algorithm continuously divides the number until the remainder becomes 0.

For example, suppose we want to calculate the H.C.F of two numbers, 60 and 48. Then we divide the 60 by 48; it returns the remainder 12. Now we again divide the number 24 by 12, and then it returns the remainder 0. So, in this way, we get the H.C.F is 12.

Pseudo Code of the Euclid Algorithm

Step 1: There are two integer numbers, such as a and b.

Step 2: if a = 0, then the GCD(a, b) is b.

Step 3: if b = 0, the GCD(a, b) is a.

Step 4: a mod b find the

Step 5: Suppose a = b and b = R

Step 6: Repeat steps 4 and 3 until a mod b is equal or greater than 0.

Step 7: GCD = b and then print the result.

Step 8: Stop the program.

Let's find the H.C.F or GCD of two numbers using Euclid's algorithm in python.

Euclid.py

Output:

How do you find the gcd between two numbers in python?

Learn how to calculate the GCD of two numbers in python.

GCD of two numbers in Python

Introduction

GCD stands for Greatest Common Divisor. It is used to calculate the HCF(Highest Common Factor), i.e., GCD(greatest common divisor) for two numbers is a number that can perfectly divide the two numbers.

Scope of Article

  • In this article, we will learn how to calculate the GCD of two numbers in python.
  • This article defines what GCD is in mathematical terms and how we can implement that same GCD in python.
  • We will be discussing the various methods to calculate the GCD of two numbers by using
    • gcd() function
    • recursion
    • loops
    • Euclidean Algorithm

Introduction to GCD of two numbers in Python

GCD(Greatest Common Divisor) is a mathematical term that explains calculating the greatest common factor of two numbers. The GCD of two or more integers that are not all zero is the largest positive integer dividing both integers.

GCD is also known as HCF(Highest Common factor).

In this example, we will see how to calculate the GCD of two numbers. Example: There are two numbers, 4 and 10. What is the GCD/HCF of 4 and 10?

As we discuss the definition of GCD, it tells us the highest common factor that divides two numbers. In the case of 4 and 10, the highest common factor is 2.

Calculating GCD using gcd() function

There are various methods to calculate the GCD of two numbers. One of the methods is using the gcd() function that is available in the math module.

Note: For calculating the gcd of two numbers using gcd() function. It is mandatory to import the mathmodule. If themathmodule is not imported it will throwImportError.

Syntax The syntax of gcd() function:

Parameters

  • x 'x' is a non-negative integer whose GCD/HCF we have to compute.
  • y 'y' is also a non-negative integer whose GCD/HCF we have to compute.

Return Type math.gcd() function will return a non-negative integer, the highest common factor i.e., the GCD of x,y.

Note: If we entered x and y both as 0. The function will return 0, and if we are using any other data type instead of intit will throwTypeError.

Example

Input:

# math module contains the gcd function
import math

# now, let's calculate the gcd of 2 numbers.
x = 10
y = 4
hcf = math.gcd(x,y)

print(f"The GCD of {x} and {y} is {hcf}.")

Output:

The GCD of 10 and 4 is 2.

:::

Using Recursion to calculate the GCD

We will now use the Recursion technique to calculate the GCD of two numbers.

Input:

def gcd(x,y):
    if y == 0:
        return x
    else:
        return gcd(y,x%y)
        
print(gcd(100,3))

Output:

Using Euclidean Algorithm to calculate the GCD

Euclidean Algorithm is the most efficient algorithm to calculate the GCD of two numbers.

So, the Euclidean algorithm states that we first store the greater number and the smaller number, then we divide the greater number by the smaller number and store the remainder.

The stored remainder should be divided by a smaller number, and keep repeating this process until the remainder is equal to 0.

Example: We have two numbers, 24 and 54. Now according to Euclidean Algorithm, we divide 54%24 = 6 and store 6. Now divide 24%6 = 0. Now our remainder is 0. So, our result is 6 i.e., the GCD of 24 and 54 is 6.

Using LOOPS

Input:

x = 24
y = 54
while y:
    x,y = y, x%y
print(x)

Output:

USING RECURSION

Input:

def gcd(x,y):
    if x == y or y == 0:
        return x
    if x == 0:
        return y
    
    else:
        if x>y:
            return gcd(x-y,y)
        else:
            return gcd(x,y-x)

print(gcd(27,90))

Output:

Using Loops to calculate the GCD

Input:

x = 24
y = 100
n = min(x,y)

hcf = 0
for i in range(1,n+1):
    if x%i == 0 and y%i == 0:
        hcf = i
        
print(hcf)

Output:

Explanation: n stores the minimum value of x and y value because the HCF(highest common factor) of two numbers always lies between the 1 and minimum of two numbers. So, n can store the minimum value of two numbers.

The for loop will run for n+1 times because n+1 is exclusive in the for loop. For every step, check that both the numbers are divisible by the current value. If both values are divisible by the current value of for loop, then hcf will be the current value of for loop. After the successful execution of for loop, our program will result in the HCF(highest common factor) of x and y.

Conclusion

  • GCD is a mathematical tool that tells us about the highest common factor of two numbers.
  • We can use the built-in gcd() function, which is available in the math module to find gcd of 2 numbers.
  • We can find gcd using one of the popular algorithms, i.e., Euclidean Algorithm to calculate the GCD.

How do I run a GCD in Python?

# write a program to understand the GCD of two number in python using the recursion. x =int (input ("Enter the first number: ")) # take first no. y =int (input ("Enter the second number: ")) # take second no.

Is there a GCD function in Python?

Greatest common divisor or gcd is a mathematical expression to find the highest number which can divide both the numbers whose gcd has to be found with the resulting remainder as zero. It has many mathematical applications. Python has a inbuilt gcd function in the math module which can be used for this purpose.

How do you find the GCD between two numbers?

The steps to calculate the GCD of (a, b) using the LCM method is:.
Step 1: Find the product of a and b..
Step 2: Find the least common multiple (LCM) of a and b..
Step 3: Divide the values obtained in Step 1 and Step 2..
Step 4: The obtained value after division is the greatest common divisor of (a, b)..

How do you find the LCM and GCD of two numbers in Python?

Python Program - Find LCM of Two Numbers.
x = 20 y = 25 if x > y: x, y = y, x for i in range(1,x+1): if x%i == 0 and y%i == 0: gcd = i lcm = (x*y)/gcd print("LCM of", x, "and", y, "is:", lcm).
p = x = 20 q = y = 25 while x != y: if x > y: x = x - y else: y = y - x lcm = (p*q)/x print("LCM of", p, "and", q, "is:", lcm).