-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd.js
More file actions
39 lines (32 loc) · 877 Bytes
/
gcd.js
File metadata and controls
39 lines (32 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Demo amazon problem...
function generalizedGCD(num, arr)
{
var nums = arr.sort((a, b) => { return a-b; });
var gcd = 1;
var ans = 1;
for (var j = 0; j < num-1; j++) {
gcd = j === 0 ? nums[j] : ans;
ans = getGCD(gcd, nums[j+1]);
}
return ans;
function getGCD(a, b) {
var r = 1;
var largest = a > b ? a : b;
var smallest = a > b ? b : a;
var gcd = 1;
while (r !== 0) {
r = largest % smallest;
if (r === 0) {
gcd = smallest;
} else {
largest = smallest;
smallest = r;
}
}
return gcd;
}
}
console.log(generalizedGCD(5, [2,3,4,5,6]));
console.log(generalizedGCD(5, [2,4,6,8,10]));
console.log(generalizedGCD(3, [1,1,1]));
console.log(generalizedGCD(3, [10,10,5]));