Created
March 30, 2018 22:00
-
-
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.
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
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