Finding GCD Using Euclid's Algo

Introduction

Euclid's algorithm is a method for finding the greatest common divisor (GCD) of two integers. The GCD is the largest positive integer that divides both numbers without leaving a remainder. In this article, we will discuss the understanding of Euclid's algorithm, the approach to finding the GCD, and provide a step-by-step solution with sample code.



Understanding Euclid's Algorithm

Euclid's algorithm is based on the principle that the GCD of two numbers remains the same if the larger number is replaced by the difference between the larger and smaller numbers. The algorithm repeatedly applies this principle until the remainder becomes zero, at which point the smaller number becomes the GCD.



Approach

To find the GCD of two numbers using Euclid's algorithm, we start with two positive integers 'a' and 'b'. We repeatedly divide the larger number by the smaller number and update the numbers until the remainder becomes zero. The last non-zero remainder is the GCD of the original two numbers.



Step-by-Step Solution

a. Set 'temp' equal to the value of 'y'. 

b. Set 'y' equal to the remainder when 'x' is divided by 'y'. 

c. Set 'x' equal to the value of 'temp'.


Code

Here's an example implementation for finding the GCD of two numbers using Euclid's algorithm:

Conclusion

Euclid's algorithm provides an efficient method for finding the GCD of two numbers. By repeatedly dividing the larger number by the smaller number and updating the values, we can find the GCD using a few iterations. The code provided demonstrates a simple implementation, allowing you to input the numbers and obtain their GCD. Understanding this approach enables us to solve problems involving the GCD and apply them in various mathematical and computational tasks.


Exercise

Write a program to find LCM of 2 numbers.

Hint : A * B = GCD * LCM