@ParasArora/

between two sets

Nodejs

No description

fork
loading
Files
  • index.js
index.js
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
function gcd(a, b){
  if(b == 0) return a;
  return gcd(b, a % b);
}

function lcm(a, b){
  return (a * b) / gcd(a, b);
}

function getTotalX(a, b) {

  let n = 0;
  for(let i = 0; i < b.length; i++){
    n = gcd(n, b[i]);
  }

  let i = a[0];

  for(let j = 1; j < a.length; j++){
    i = lcm(i, a[j]);
  }

  let count = 0;

  for(let k = i; k <= n; k+=i){
    let tempCount = 0
    for(let m = 0; m < b.length; m++){
      if(b[m] % k == 0) tempCount++;
    }
    if(tempCount == b.length) count++;
  }

  return count;

}

// getTotalX([2, 4], 16, 32, 96]);
getTotalX([1], [100]);
node v10.16.0