Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to calculate the GCD (greatest common divisor) of two numbers in Node.js

2 Answers

0 votes
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
  
*/

 





answered Jan 21, 2022 by avibootz
0 votes
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
  
*/

 





answered Jan 21, 2022 by avibootz
...