Greatest common divisor · ตัวหารร่วมมากที่สุด
Greatest common divisor
The GCD of two numbers is the largest integer that divides both. Euclid's algorithm is beautifully short: while b isn't 0, replace the pair (a, b) with (b, a % b). When b reaches 0, a is the answer.
ตัวหารร่วมมาก
GCD ของจำนวนสองจำนวนคือจำนวนเต็มที่มีค่ามากที่สุดที่หารทั้งสองจำนวนได้ อัลกอริทึมของยูคลิด มีรูปแบบที่สั้นอย่างสวยงาม: пока b ไม่เป็น 0 ให้แทนคู่ (a, b) ด้วย (b, a % b) เมื่อ b ถึง 0 ค่า a คือคำตอบ
Complete int gcd(int a, int b) to return the greatest common divisor of a and b. Euclid's method: repeatedly replace (a, b) with (b, a % b) until b is 0. · เติม int gcd(int a, int b) ให้กลับค่าผลหารร่วมมากของ a และ b วิธีของยูคลิด: แทนที่ (a, b) ด้วย (b, a % b) ซ้ำๆ จนกว่า b จะเท่ากับ 0
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่