簡単 0件解決済み 正答率0%

Greatest Common Divisor

Find the largest number that divides both values.

Return the greatest common divisor of a and b. Euclid's algorithm repeatedly replaces (a, b) with (b, a mod b) until b is 0.

Implement gcd(a, b).

Input

a: The first number. b: The second number.

Output

Return gcd(a, b). gcd(0, 0) is 0.

制約

0 ≤ a, b ≤ 10¹⁸

例

例 1
入力:
a = 48
b = 18

期待される出力:
expected = 6
入力:
long long a = 48;
long long b = 18;

期待される出力:
long long expected = 6;
入力:
long a = 48;
long b = 18;

期待される出力:
long expected = 6;

解説

48 mod 18 = 12, 18 mod 12 = 6, 12 mod 6 = 0, so the answer is 6.

例 2
入力:
a = 17
b = 5

期待される出力:
expected = 1
入力:
long long a = 17;
long long b = 5;

期待される出力:
long long expected = 1;
入力:
long a = 17;
long b = 5;

期待される出力:
long expected = 1;

解説

17 and 5 share no factor other than 1.

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。