Advertisement
AlexanderHristov

Greatest Common Divisor

Jan 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function greatestCommonDivisor(firstNum, secondNum) {
  2.  
  3.     let higherNum = Math.max(firstNum, secondNum);
  4.     let lowerNum = Math.min(firstNum, secondNum);
  5.     let GCD = 0;
  6.  
  7.     let devisor = Math.floor(higherNum / lowerNum);
  8.     let residue = higherNum % lowerNum;
  9.  
  10.     if (residue === 0) {
  11.  
  12.         console.log(lowerNum);
  13.        
  14.     } else {
  15.  
  16.         while (residue !== 0) {
  17.  
  18.             GCD = residue;
  19.  
  20.             devisor = Math.floor(lowerNum / residue);
  21.             residue = lowerNum % residue;
  22.         };
  23.  
  24.         console.log(GCD);
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement