Last active
October 28, 2020 01:35
-
-
Save sdwvit/06beee94d9a5555e1e434b9a9dbc855d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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