Skip to content

Instantly share code, notes, and snippets.

@sdwvit sdwvit/hyi.js
Last active Aug 1, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.