Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Tips and Tricks to Find GCD
Next article icon

Tips and Tricks to Find GCD

Last Updated : 14 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Let us first discuss the standard method to find GCD of two numbers a and b.

  • Step 1: List all the divisors of the number ‘a’.
  • Step 2: List all the divisors of the number ‘b’.
  • Step 3: Identify the common divisors of both ‘a’ and ‘b’.
  • Step 4: Select the largest number from the common divisors.

GCD of Two Numbers : 12 and 18
Divisors of 12 : 1, 2, 3, 4, 6 and 12
Divisors of 18 : 1, 2, 3, 6, 9 and 18
The common divisors are 1, 2, 3 and 6 and largest of these is 6 hence 6 is the GCD

How do we speed up our computations? Below are the tricks that you can use when manually computing GCD.

  1. gcd(a, 0) = a and gcd(0, b) = b because everything divides 0.
  2. If a and b are both even, gcd(a, b) = 2*gcd(a/2, b/2) because 2 is a common divisor.
  3. If a is even and b is odd, gcd(a, b) = gcd(a/2, b). Similarly, if a is odd and b is even, then 
    gcd(a, b) = gcd(a, b/2). It is because 2 is not a common divisor.
  4. If both a and b are odd, then gcd(a, b) = gcd(|a-b|/2, b). Note that difference of two odd numbers is even
  5. Repeat steps 3–5 until a = b, or until a = 0. In either case, the GCD is power(2, k) * b, where power(2, k) is 2 raise to the power of k and k is the number of common factors of 2 found in step 3.



Next Article
Tips and Tricks to Find GCD

K

kartik
Improve
Article Tags :
  • Mathematical
  • School Learning
  • DSA
  • GCD-LCM
Practice Tags :
  • Mathematical

Similar Reads

    Tips and Tricks to Check for Prime
    Checking for a large number whether it is prime or not can be difficult. This is so difficult that RSA algorithm uses this fact to encrypt data on Internet. However there are certain trick that we can use. The idea is reduce the number faster using divisibility rules. 1. If the given number has 0s a
    2 min read
    Prime Factorization Tips and Tricks
    Let us first go through the standard Prime Factorization by the Division Method. Then we will be talking about tips and tricks to make it faster.Step 1: Divide the number by the smallest prime number (i.e. 2) until we are able to divide the given number without leaving any remainder.Step 2: Move on
    2 min read
    Find any pair with given GCD and LCM
    Given gcd G and lcm L. The task is to print any pair which has gcd G and lcm L.Examples: Input: G = 3, L = 12 Output: 3, 12 Input: G = 1, L = 10 Output: 1, 10 A normal solution will be to perform iteration over all the factor pairs of g*l and check if any pair has gcd g and lcm as l. If they have, t
    8 min read
    Find two numbers whose sum and GCD are given
    Given sum and gcd of two numbers a and b . The task is to find both the numbers a and b. If the numbers do not exist then print -1 .Examples: Input: sum = 6, gcd = 2 Output: a = 4, b = 2 4 + 2 = 6 and GCD(4, 2) = 2Input: sum = 7, gcd = 2 Output: -1 There are no such numbers whose sum is 7 and GCD is
    5 min read
    GCD and Fibonacci Numbers
    You are given two positive numbers M and N. The task is to print greatest common divisor of M'th and N'th Fibonacci Numbers.The first few Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .... Note that 0 is considered as 0'th Fibonacci Number.Examples: Input : M = 3, N = 6 Output
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences