GCD of Two Numbers in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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.

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.

Sharpen Your Fundamentals with Free Learning

Calculating GCD of Two Numbers in Python 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 in python.

Note: For calculating the gcd of two numbers using gcd() function. It is mandatory to import the math module. If the math module is not imported it will throw ImportError.

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 int it will throw TypeError.

Example

Input:

Output:

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

GCD of Two Numbers in Python Using Recursion

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

Input:

Output:

GCD of Two Numbers in Python Using Euclidean Algorithm

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:

Output:

USING RECURSION

Input:

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

GCD of Two Numbers in Python Using Loops

Input:

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.

Click here to know more about min() in python.

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.

See Also: