The Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides both numbers without leaving a remainder. Python provides multiple ways to compute the GCD.
Example:
import math
a = 60
b = 48
print(math.gcd(a, b))
Output
12
Explanation: The common divisors of 60 and 48 are [1, 2, 3, 4, 6, 12] and the greatest one is 12.
Syntax
math.gcd(x, y)
Parameters: x, y Two integers (at least one of them must be non-zero).
Returns:
- The largest integer that divides both numbers.
- If one number is 0, the GCD is the absolute value of the non-zero number.
Using the math.gcd() Function
Python's math module provides a built-in gcd() function. It computes the GCD of two numbers easily.
import math
a = 60
b = 48
print(math.gcd(a, b))
Output
12
Explanation: The common divisors of 60 and 48 are [1, 2, 3, 4, 6, 12]. The greatest one is 12.
Examples
Example 1: GCD of a number and 0
import math
print(math.gcd(0, 25))
Output
25
Explanation: When one number is 0, the gcd() returns the absolute value of the other number.
Example 2: GCD of two co-prime numbers
import math
print(math.gcd(17, 29))
Output
1
Explanation: 17 and 29 are co-prime (no common divisors except 1), so gcd() returns 1.
Iterative (Brute Force) Approach
This method checks all numbers from 1 to the smaller of the two integers and finds the largest number that divides both.
def gcd_iterative(a, b):
a, b = abs(a), abs(b)
gcd = 1
for i in range(1, min(a, b) + 1):
if a % i == 0 and b % i == 0:
gcd = i
return gcd
print(gcd_iterative(60, 48))
Output
12
Explanation:
- a, b = abs(a), abs(b): Converts both numbers to positive to handle negative inputs.
- gcd = 1: Initializes the variable to store the greatest common divisor.
- for i in range(1, min(a, b) + 1): Loops through all numbers from 1 to the smaller of a and b.
- if a % i == 0 and b % i == 0: Checks if i divides both a and b without a remainder.
- gcd = i: Updates gcd whenever a common divisor is found.
Euclidean Algorithm
The Euclidean algorithm is an efficient way to compute GCD using the property:
def gcd_euclidean(a, b):
a, b = abs(a), abs(b)
while b != 0:
a, b = b, a % b
return a
print(gcd_euclidean(60, 48))
Output
12
Explanation:
- a, b = abs(a), abs(b): Converts both numbers to positive to handle negative inputs.
- while b != 0: Loops until b becomes 0.
- a, b = b, a % b: Updates a to b and b to the remainder of a ÷ b. This is the key step of the Euclidean algorithm.
- return a: When b becomes 0, a contains the greatest common divisor.
GCD of Multiple Numbers Using functools.reduce()
To find the GCD of a list of numbers, math.gcd() can be applied one by one to all the numbers using functools.reduce(). This gives the greatest common divisor of the entire list.
import math
from functools import reduce
nums = [48, 64, 80]
res = reduce(math.gcd, nums)
print(res)
Output
16
Explanation:
- reduce(math.gcd, nums) applies math.gcd() to the first two elements, then applies it to the result and the next element, and so on.
- For nums = [48, 64, 80], first gcd(48, 64) = 16, then gcd(16, 80) = 16.