ง่าย 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก