Problem 149161 · easy · Phase 01 Prerequisites & Setup

Greatest Common Divisor by Subtraction

loops · while · math

The greatest common divisor of two positive integers can be found with a very old trick: while the two numbers differ, replace the larger one by the difference between them. When they become equal, that value is the GCD. Write gcd_subtract(a, b) that implements this with a loop.

Examples

Input:  a = 48, b = 18
Output: 6
Explanation: (48,18) -> (30,18) -> (12,18) -> (12,6) -> (6,6)

Input:  a = 7, b = 7
Output: 7

Constraints

  • 1 <= a, b <= 10**5

Goals

  • Loop until two values become equal
  • Change whichever variable is larger each step
Starting Python…