Skip to content

Instantly share code, notes, and snippets.

@sdwvit
Last active October 28, 2020 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdwvit/06beee94d9a5555e1e434b9a9dbc855d to your computer and use it in GitHub Desktop.
Save sdwvit/06beee94d9a5555e1e434b9a9dbc855d to your computer and use it in GitHub Desktop.
let test = [[3, [666, 123, 57, 191919, 2]]];
function gcd(a, b) {
while (true) {
if (a !== 0 && b !== 0) {
if (a > b) {
a = a % b;
} else {
b = b % a;
}
} else {
return a + b;
}
}
}
function generalizedGCD(num, arr) {
if (arr.length < 1) {
return 1;
}
if (arr.length === 1) {
return arr[0];
}
let _gcd = 0;
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] <= 1 || arr[i + 1] <= 1) {
return 1;
}
_gcd = _gcd ? gcd(_gcd, arr[i + 1]) : gcd(arr[i], arr[i + 1]);
}
return _gcd;
}
test.forEach(el => console.log(generalizedGCD(...el)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment