Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created April 18, 2019 16:46
Show Gist options
  • Save JackHowa/250914bd1e31de807a058c3933a26b50 to your computer and use it in GitHub Desktop.
Save JackHowa/250914bd1e31de807a058c3933a26b50 to your computer and use it in GitHub Desktop.
solution for greatest common denominator
'use strict';
/**
* Find the greatest common divisor (GCD) of two positive integers.
*
* @param {number} a - The first integer
* @param {number} b - The second integer
*
* @return {number} The largest integer that evenly divides both numbers
*/
const gcd = (a, b) => {
// implement base case checking if result is divisble, eg 2 % 2 === 0
if (b === 0) {
return a;
}
// otherwise, find another potential divisible from remainder of two integers or initial second integer
return gcd(b, a % b);
};
console.log('gcd(60, 500) = 20:', gcd(60, 500));
console.log('gcd(3125, 100) = 25:', gcd(3125, 100));
console.log('gcd(32, 8) = 8:', gcd(32, 8));
console.log('gcd(4, 9) = 1:', gcd(4, 9));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment