Skip to content

Instantly share code, notes, and snippets.

@DellWard
Created March 30, 2018 22:00
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 DellWard/f2ad079dd9f20d723b33a2539ffb1f7e to your computer and use it in GitHub Desktop.
Save DellWard/f2ad079dd9f20d723b33a2539ffb1f7e to your computer and use it in GitHub Desktop.
Given two arrays, the find an integer denoting the number of integers that are BOTH multiples of all numbers in the first array and factors of all numbers in the second array.
function getBetweenTwoSets(a1, a2) {
function gcd(a, b){
while(b > 0){
var temp = b;
b = a % b;
a = temp;
}
return a
}
function gcdArray(array){
var result = array[0];
for(var i = 1; i < array.length; i++){
result = gcd(result, array[i])
}
return result;
}
function lcm(a, b){
return a * (b / gcd(a, b));
}
function lcmArray(array){
var result = array[0];
for(var i = 1; i < array.length; i++){
result = lcm(result, array[i])
}
return result;
}
var l = lcmArray(a1);
var r = gcdArray(a2);
var count = 0;
for(var i = 1; i*l <= r; i++){
if(r % (l * i) == 0)
count++
}
return count++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment