Greatest common divisor · 最大公约数
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
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.
中文
最大公约数
两个数的最大公约数是能同时整除它们的最大整数。欧几里得算法非常简洁:当 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. · 点击“运行”查看此处输出。