GCD of two number in python

You can use the built-in math.gcd() function to find the greatest common divisor of two numbers in Python. Here’s an example:

import math

a = 48
b = 60

gcd = math.gcd(a, b)

print("The GCD of", a, "and", b, "is", gcd)

Output:

The GCD of 48 and 60 is 12

In this example, we first import the math module, which provides the gcd() function. We then assign the values of the two numbers to a and b. Finally, we use the math.gcd() function to calculate the GCD of a and b, and print the result.

GCD Using recursion:

You can also calculate the GCD of two numbers using recursion in Python. Here’s an example of a recursive function to find the GCD of two numbers:

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

a = 48
b = 60

gcd_result = gcd(a, b)

print("The GCD of", a, "and", b, "is", gcd_result)

Output:

The GCD of 48 and 60 is 12

In this example, the gcd() function takes two arguments a and b. It uses recursion to calculate the GCD of a and b. If b is equal to 0, then the function returns a, which is the GCD. Otherwise, it calls itself with b and a % b as arguments until b becomes 0.

Finally, we call the gcd() function with the values of a and b, and print the result.

GCD Using the Loop:

You can also calculate the GCD of two numbers using a loop in Python. Here’s an example of a loop-based function to find the GCD of two numbers:

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

a = 48
b = 60

gcd_result = gcd(a, b)

print("The GCD of", a, "and", b, "is", gcd_result)

Output:

The GCD of 48 and 60 is 12

In this example, the gcd() function takes two arguments a and b. It uses a loop to calculate the GCD of a and b. In each iteration of the loop, the function calculates the remainder of a divided by b using the modulo operator %, and stores it in a temporary variable temp. It then updates a to be equal to b, and b to be equal to temp. This process is repeated until b becomes 0.

Finally, the function returns a, which is the GCD of the two input numbers.

We then call the gcd() function with the values of a and b, and print the result.

GCD Using Euclid’s algorithm or Euclidean Algorithm:

Euclid’s algorithm, also known as the Euclidean algorithm, is a commonly used algorithm to find the GCD of two numbers. Here’s an example of a function that implements Euclid’s algorithm in Python:

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

a = 48
b = 60

gcd_result = gcd(a, b)

print("The GCD of", a, "and", b, "is", gcd_result)

Output:

The GCD of 48 and 60 is 12

In this example, the gcd() function takes two arguments a and b. It uses a loop to calculate the GCD of a and b using Euclid’s algorithm. In each iteration of the loop, the function updates a to be equal to b, and b to be equal to the remainder of a divided by b using the modulo operator %. This process is repeated until b becomes 0.

Finally, the function returns a, which is the GCD of the two input numbers.

We then call the gcd() function with the values of a and b, and print the result.