The gcd() function - Python math module

The gcd() function from the Python math module finds the greatest common divisor for the given set of numbers.

Math Updated 25 Sep 2016 1 min read

Overview

  • The gcd() function of Python math maodule returns the greatest common divisor of a set of integers.
  • The greatest common divisor (GCD) of a set of integers is the largest integer that divides each of them without a remainder.

Example

python
import math

# Find the GCD of 12, 44

number1 = 12

number2 = 44

GCD = math.gcd(number1,number2)

print("GCD of {} and {} is:{}".format(number1, number2, GCD))

# Find the GCD of 22, 121

number1 = 22

number2 = 121

GCD = math.gcd(number1,number2)

print("GCD of {} and {} is:{}".format(number1, number2, GCD))

Output

Output
GCD of 12 and 44 is:4

GCD of 22 and 121 is:11