Contact: aviboots(AT)netvision.net.il
31,166 questions
40,722 answers
573 users
function gcd(a, b) { return b == 0 ? a : gcd(b, a % b); } const a = 12, b = 20; console.log("The GCD (greatest common divisor) of " + a + " and " + b + " is: " + gcd(a, b)); /* run: The GCD (greatest common divisor) of 12 and 20 is: 4 */
const a = 12, b = 20 let gcd = 0; let i = a < b ? a : b; for (;i <= a && i <= b; i--) { if (a % i == 0 && b % i == 0) { gcd = i; break; } } console.log("The GCD (greatest common divisor) of " + a + " and " + b + " is: " + gcd); /* run: The GCD (greatest common divisor) of 12 and 20 is: 4 */