Write a program which finds the greatest common divisor of two natural numbers a and b…
- User
Write a program which finds the greatest common divisor of two natural numbers a and b
Hint
You can use the following observation:
For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y)
Constrants
1 ≤ a, b ≤ 109 Input
a and b are given in a line sparated by a single space.
Output
Output the greatest common divisor of a and b.
Examples
Input
54 20
Output
2
Input
147 105
Output
21
Assistant
We need to write a program that finds the greatest common divisor of two natural numbers a and b. The hint: use Euclidean algorithm: if x ≥ y, then gcd(x, y) = gcd(y, x % y). Constraints: up to 1e9, so need 64-bit integers. Implementation: read a and b, compute gcd using recursion or loop. Output the gcd.
But careful: the Euclidean algorithm works for any integers, not necessarily x ≥ y, because if x < y, then gcd(x,y) = gcd(y, x) but the property holds with mod: gcd(x,y) = gcd(y, x%y) if y != 0. Typically, we can do while b != 0: (a, b) = (b, a % b). So we'll implement that.
We'll write solution in Python? The prompt: "Write a program" probably expects code in any language? Usually it's from an online judge, so Python is fine. I'll provide Python solution.
Edge cases: one of them zero? But constraints say natural numbers, so >=1.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.