Last active
March 26, 2016 21:49
-
-
Save z4o4z/9e797515f76d3b029821 to your computer and use it in GitHub Desktop.
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 greatestCommonPrimeDivisor(a, b) { | |
var aCPDs = getCPDs(a); | |
var bCPDs = getCPDs(b); | |
var max = -1; | |
for (var i = 0; i < aCPDs.length; i++) { | |
for (var j = 0; j < bCPDs.length; j++) { | |
if (aCPDs[i] === bCPDs[j] && max < aCPDs[i]) { | |
max = aCPDs[i]; | |
} | |
} | |
} | |
function getCPDs (n) { | |
var cpds = []; | |
for (var i = 2; i < n; i++) { | |
if (n % i === 0 && checkP(i)) { | |
cpds.push(i); | |
} | |
} | |
return cpds; | |
} | |
function checkP (n) { | |
for (var i = 2; i < n; i++) { | |
if (n % i === 0) { | |
return false | |
} | |
} | |
return true; | |
} | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment