Programs

Python Program to Find Two GCD Numbers

The Greatest Common Divisor in Mathematics is abbreviated to GCD. This number represents the largest value that has the ability to divide the two given numbers completely. It is also referred to as the greatest common factor or GCF in few cases. Let us consider an example of two numbers, 12 and 28. The numbers that can divide both of these numbers are 2 and 4. However, ‘4’ is the greatest number that can divide both 12 and 24 completely. Hence, the GCD of 12 and 24 is 4. The GCD can be found for any number of inputs. It is just the common largest number that can divide all the input numbers. Finding GCD of a number finds a wide range of applications in encryption technology. It is also used in the simplification of fractions. 

Check out our other data science courses at upGrad. 

gcd() in Python:

A built-in function exists in the Python interpreter’s math module that can compute the GCD of any two input numbers. This inbuilt function is the GCD() function. The gcd() function in Python accepts two parameters of integer type and returns an integer that is equivalent to the GCD value of the two input parameters. 

The general syntax of the gcd() function in Python is:

Gcd(x,y)

The mathematical phrase for determining the biggest common factor that may precisely divide two numbers is “greatest common divisor” (GCD). The Highest Common Factor (HCF) is another name for a GCD. The HCF/ GCD, for instance, of the two numbers 48 and 24 is 8 because 8 entirely divides 48 and 24.

Explore our Popular Data Science Courses

Parameters:

The gcd() function accepts two parameters to compute the gcd. Both the parameters must be positive integers.

Read our popular Data Science Articles

Return value of gcd in Python:

The gcd() function in Python returns a positive integer whose value is equivalent to the greatest common divisor of the input parameters. 

Apart from using the gcd() function, there are several other methods and approaches to determine the GCD of two numbers in Python. The approaches include:

  • Determining GCD using recursions
  • Finding GCD using loops
  • Implementation of GCD using Euclidean Algorithm

You can learn both theoretical and practical aspects of Python with upGrad’s Professional Certificate in Data Science and Business Analytics from the University of Maryland. This course helps you learn Python from scratch. Even if you are new to programming and coding, upGrad will offer you a two-week preparatory course so that you can pick up on the basics of programming. you will learn about various tools like Python, SQL,, while working on multiple industry projects.

Techniques To Find GCD Of Two Numbers In Python 

1. Using gcd()

The first method to find the GCD of two numbers in Python uses an in-built function called gcd(). The syntax for the same is ‘gcd(a,b)’, where a and b  are the two integer numbers passed as an argument to the function. Below is a GCD program in Python to achieve the same:

import math

print (math.gcd(3, 6))

Output:

3

Note: To use the gcd() function to evaluate the GCD of two numbers in Python, importing the math module is required. ImportError will be thrown if the math module is not imported.

2. Using Loops

The second method to find GCD in Python is using the loops. In computer programming, a loop is a collection of instructions repeatedly carried out until a particular condition is satisfied. Below mentioned is the Python program to do the same:

def calculate_gcd(a,b):

if a>b:

var = b

else:

var = a

for j in range(1, var + 1):

if((a % j == 0) and (b % j == 0)):

req_gcd = j

return req_gcd

x=24

y=36

print(calculate_gcd(x,y))

Output:

12

3. Using Recursion

Recursion is the third way to find the GCD. It is the act of repeating things in a self-similar war. Recursive calling of the function is used in programming languages to describe when a program allows calling a function inside another function. The Python code to achieve the same is listed below:

def cal_gcd(x,y):

if(y == 0):

return abs(x)

else:

return cal_gcd(y, x % y)

a=24

b=36

print(cal_gcd(a,b))

Output:

12

4. Euclidean Algorithm

Using the Euclidean Algorithm is also a unique way to find GCD. The foundation of this technique is the fact that the HCF of any two numbers also divides the difference of the two numbers. In this procedure, the greater is divided by, the smaller, and the residual is taken. Divide the smaller by the leftover now. Continue until there is no more to add.

The Python program to accomplish the same task is provided below:

def cal_gcd(a, b):

while(b):

a,b = b,a % b

return abs(a)

a=24

b=36

print(cal_gcd(a,b))

Output:

12

Want to share this article?

Lead the Data Driven Technological Revolution

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks