Explanation

485.Find the GCD of Two Numbers

Easy
Given two positive integers a and b, return their greatest common divisor (GCD) — the largest number that divides both evenly. The brute-forceBrute ForceChecking every candidate from min(a, b) down to 1, stopping at the first one that divides both numbers. approach checks candidates one at a time, which is simple but can take as many as min(a, b) steps. The Euclidean algorithmEuclidean AlgorithmRepeatedly replacing (a, b) with (b, a % b) — since any common divisor of a and b also divides a % b — until b reaches 0., one of the oldest algorithms in existence, shrinks the pair of numbers geometrically fast instead, usually finishing in a handful of rounds even for huge inputs.

Example 1:

Input: a = 12, b = 18

Output: 6

Example 2:

Input: a = 7, b = 13

Output: 1

Example 3:

Input: a = 20, b = 20

Output: 20

+ 4 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ a ≤ 1000000
  • ●1 ≤ b ≤ 1000000

a =

12

b =

18