Skip to content

Instantly share code, notes, and snippets.

@z4o4z
Last active March 26, 2016 21:49
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 z4o4z/9e797515f76d3b029821 to your computer and use it in GitHub Desktop.
Save z4o4z/9e797515f76d3b029821 to your computer and use it in GitHub Desktop.
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