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.
מחלק משותף גדול ביותר
ה-MCD של שני מספרים הוא השלם הגדול ביותר המחלק את שניהם. אלגוריתם אוקליד קצר ומרהיב: כל עוד 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. · לחץ על הרץ כדי לראות את התוצא כאן.