Last active
August 16, 2016 17:05
-
-
Save craftybones/c44fe29d33223f0e2ca18822c2c257a8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var isPrime=function(x){ | |
for (var i = 2; i <= Math.sqrt(x); i++) { | |
if(x%i==0){return false;} | |
} | |
return true; | |
} | |
var primeFactors=function(x) { | |
var factors=[]; | |
for (var i = 2; i <= x; i++) { | |
if(x%i==0 && isPrime(i)) { | |
factors.push(i); | |
} | |
} | |
return factors; | |
} | |
var canSheEqualise=function(a,xyz) { | |
var allFactors={}; | |
var numberOfFactors={}; | |
a.forEach(function(i){ | |
primeFactors(i).forEach(function(p){ | |
allFactors[p]=true; | |
numberOfFactors[p]=numberOfFactors[p]||0; | |
numberOfFactors[p]+=1; | |
}); | |
}); | |
var uncommonFactors=Object.keys(numberOfFactors).filter(function(n){ | |
return numberOfFactors[n]!=Object.keys(a).length; | |
}); | |
var result=uncommonFactors.every(function(i){ | |
return xyz[i]; | |
}); | |
if(result) | |
return "She can"; | |
return "She can't"; | |
} | |
console.log(canSheEqualise([2,4],{2:true})); | |
console.log(canSheEqualise([2,3,6,7],{2:true,3:true})); | |
console.log(canSheEqualise([49,98],{2:true})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment