GCD and LCM is a foundational concept in number theory and widely used in areas such as simplifying fractions, cryptography, and solving Diophantine equations.
Table of Contents
GCD (Greatest Common Divisor)
- GCD of two numbers is the largest number that divides both without leaving a remainder.
- Also known as, HCF(highest common factor)
GCD of 12 and 18
- Factors of 12: 1, 2, 3, 4, 6, 12
- Factors of 18: 1, 2, 3, 6, 9, 18
- Common factors: 1, 2, 3, 6
So, the GCD is 6
Euclidean Algorithm for GCD
This is an efficient method to find GCD:
gcd(a,b)=gcd(b,a mod b)
gcd(a, b) = gcd(b, a % b)
Repeat until b becomes 0.
When b is 0, a is the GCD.
Pythongcd(1220, 516) => gcd(516,188) => gcd(188,140)
gcd(188,140) => gcd(140,48) => gcd(48,44)
gcd(48,44) => gcd(44,4) => gcd(4,0) => 4
PythonCode
def gcd(a, b):
# Euclidean Algorithm
while b != 0:
a, b = b, a % b # Replace a with b, and b with a mod b
return a
PythonEuclidean Algorithm, Why it works:
It keeps reducing the problem into smaller pairs while preserving the GCD.
In the Euclidean algorithm:
gcd(a,b) = gcd(b,a%b)
PythonAs we know, standard Modulo concept
a % b = a - q·b for some integer q.
PythonSo, you’re effectively replacing (a, b) with (b, a – q·b) — and since any common divisor of a and b also divides a – q·b, the GCD is preserved.
That’s why you can keep reducing the numbers like this without changing the result.
LCM (Least Common Multiple)
The LCM of two integers is the smallest number that is a multiple of both numbers.
Find LCM of 12 and 18.
- Multiples of 12: 12, 24, 36, 48, 60…
- Multiples of 18: 18, 36, 54, 72…
- Common multiples: 36, 72, …
So, the LCM is 36
Relation between GCD and LCM:
For any two positive integers a and b:
LCM(a,b) × GCD(a,b) = a×b
PythonCode
def lcm(a, b):
# LCM formula: (a * b) // GCD(a, b)
return abs(a * b) // gcd(a, b)
PythonUsing math module (Python 3.5+)
import math
a = 12
b = 18
gcd = math.gcd(a, b)
lcm = abs(a * b) // gcd
print("GCD:", gcd)
print("LCM:", lcm)
PythonWhy Euclidean algorithm is efficient?
Because a % b is always smaller than b, the numbers shrink quickly. In fact, it finishes in logarithmic time.
O(log min(a,b)) => O(log n)
PythonGCD Properties
- Commutative: gcd(a,b)=gcd(b,a)
- Associative: gcd(a,gcd(b,c))=gcd(gcd(a,b),c)
- If a divides b, then: gcd(a,b)=a
- GCD with 0: gcd(a,0)= |a|
- GCD for consecutive Numbers: gcd(n,n+1)=1
- Two numbers are coprime if gcd(a, b) == 1
- consecutive integers are always coprime
- If gcd(a, b) = d, then: a%b=a%(b%d)
LCM Properties
- Associative: lcm(a,lcm(b,c))=lcm(lcm(a,b),c)
Practice
Beginner Level
Problem | Concepts |
---|---|
1071. Greatest Common Divisor of Strings | GCD logic with strings |
231. Power of Two | Binary tricks, GCD base |
136. Single Number | XOR logic (good warm-up) |
Intermediate Level
Problem | Concepts |
---|---|
149. Max Points on a Line | GCD for slope normalization |
365. Water and Jug Problem | Linear Diophantine Eq. (GCD-based) |
2447. Number of Subarrays With GCD Equal to K | Sliding window + GCD |
Advanced Level
Problem | Concepts |
---|---|
1250. Check If It Is a Good Array | Bézout’s Identity, GCD of array |
1998. GCD Sort of an Array | Union-Find + GCD |
1185. Day of the Week | LCM indirectly used (date calc) |
Bonus Math-Based
Problem | Concepts |
---|---|
1014. Best Sightseeing Pair | Indirect use of number theory |
172. Factorial Trailing Zeroes | Multiples, primes — LCM logic insight |
1492. The kth Factor of n | Divisors, used in LCM/GCD contexts |
Conclusion
Understanding the GCD and the Euclidean Algorithm provides a solid base for tackling a wide range of computational and mathematical problems. Its efficiency—thanks to logarithmic complexity—makes it ideal for use even with very large numbers. Mastering this concept unlocks deeper topics such as LCM computation, coprimality checks, modular arithmetic, and plays a critical role in many competitive programming challenges.