AlgoLounge

Greatest Common Divisor (GCD)

easy

Find the greatest common divisor of two integers using the Euclidean algorithm.

Examples

Example 1

Input:  a = 12, b = 8
Output: 4

Divisors of 12: [1,2,3,4,6,12], Divisors of 8: [1,2,4,8], GCD = 4

Example 2

Input:  a = 15, b = 25
Output: 5

Example 3

Input:  a = 0, b = 42
Output: 42

Constraints

  • -10⁹ ≤ a, b ≤ 10⁹
  • Handle negative numbers and zero cases
  • GCD is always positive
TypeScript
Loading editor…
List of test cases (10)
Case 1
a = 12
b = 8
Case 2
a = 15
b = 25
Case 3
a = 0
b = 42
Case 4
a = 17
b = 19
Case 5
a = -12
b = 8
Case 6
a = 100
b = 50
Case 7
a = 7
b = 13
Case 8
a = 48
b = 18
Case 9
a = 36
b = 60
Case 10
a = 0
b = 0