Programming Python Euclid’s Algorithm for Finding the GCD of Two Numbers


Greatest Common Divisor
In mathematics, the greatest common divisor of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4. The greatest common divisor is also known as the greatest common factor (gcf), highest common factor (hcf), greatest common measure (gcm), or highest common divisor.

<img src="python.png" alt="python">


Factors are the numbers that can be multiplied to produce a particular number. Just look at this simple multiplication:

4 × 6 = 24

In the above math problem, 4 and 6 are factors of 24. Another name for factor is divisor. The number 24 also has some other factors such as:

8 × 3 = 24
12 × 2 = 24
24 × 1 = 24

From the above three math problems, 8 and 3 are also factors of 24, as are 12 and 2, and 24 and 1. So the factors of 24 are: 1, 2, 3, 4, 6, 8, 12, and 24. Next, let’s look at the factors of 30:

1 × 30 = 30
2 × 15 = 30
3 × 10 = 30
5 × 6 = 30

So the factors of 30 are 1, 2, 3, 5, 6, 10, 15, and 30. Notice that any number will always have 1 and itself as factors. From the list of factors for 24 and 30, there are factors that 24 and 30 have in common, 1, 2, 3, and 6. The greatest number of these is 6. So 6 is the greatest common factor or, more commonly, the greatest common divisor of 24 and 30.

Euclid’s Algorithm for Finding the GCD of Two Numbers
A mathematician who lived 2,000 years ago named Euclid came up with an algorithm for finding the greatest common divisor of two numbers.
In mathematics, the Euclidean algorithm, or Euclid's algorithm, is an efficient method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder. It is named after the ancient Greek mathematician Euclid, who first described it in Euclid's Elements (c. 300 BC). It is an example of an algorithm, a step-by-step procedure for performing a calculation according to well-defined rules, and is one of the oldest algorithms in common use. It can be used to reduce fractions to their simplest form, and is a part of many other number-theoretic and cryptographic calculations.
The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number. For example, 21 is the GCD of 252 and 105 (252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 147 = 252 − 105. Since this replacement reduces the larger of the two numbers, repeating this process gives successively smaller pairs of numbers until one of the two numbers reaches zero. When that occurs, the other number (the one that is not zero) is the GCD of the original two numbers. By reversing the steps, the GCD can be expressed as a sum of the two original numbers each multiplied by a positive or negative integer, e.g., 21 = 5 × 105 + (−2) × 252. The fact that the GCD can always be expressed in this way is known as Bézout's identity.
The version of the Euclidean algorithm described above (and by Euclid) can take many subtraction steps to find the GCD when one of the given numbers is much bigger than the other. A more efficient version of the algorithm shortcuts these steps, instead replacing the larger of the two numbers by its remainder when divided by the smaller of the two. With this improvement, the algorithm never requires more steps than five times the number of digits (base 10) of the smaller integer. This was proven by Gabriel Lamé in 1844, and marks the beginning of computational complexity theory. Additional methods for improving the algorithm's efficiency were developed in the 20th century.
The Euclidean algorithm has many theoretical and practical applications. It is used for reducing fractions to their simplest form and for performing division in modular arithmetic. Computations using this algorithm form part of the cryptographic protocols that are used to secure internet communications, and in methods for breaking these cryptosystems by factoring large composite numbers. The Euclidean algorithm may be used to solve Diophantine equations, such as finding numbers that satisfy multiple congruences according to the Chinese remainder theorem, to construct continued fractions, and to find accurate rational approximations to real numbers. Finally, it is a basic tool for proving theorems in number theory such as Lagrange's four-square theorem and the uniqueness of prime factorizations. The original algorithm was described only for natural numbers and geometric lengths (real numbers), but the algorithm was generalized in the 19th century to other types of numbers, such as Gaussian integers and polynomials of one variable. This led to modern abstract algebraic notions such as Euclidean domains.
Euclid’s GCD algorithm is short. Here’s a source code program that implements his algorithm as Python code.

#Python source code untuk mencari Faktor Persekutuan Terbesar
#Loki Lang

def main():
    first = int(input("Masukkan bilangan pertama "))
    second = int(input("Masukkan bilangan kedua "))
    result = gcd(first, second)
    print("Faktor persekutuan terbesar dari %s dan %s ialah %s" %(first, second, result))

def gcd(a, b):
    while a != 0:
        a, b = b % a, a
    return b

if __name__ == '__main__':
    main()