Skip to content

Instantly share code, notes, and snippets.

@hueitan
Created May 5, 2015 02:32
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 hueitan/0726ee41a3c5e5581885 to your computer and use it in GitHub Desktop.
Save hueitan/0726ee41a3c5e5581885 to your computer and use it in GitHub Desktop.
stupid approach answer - prime factor - https://codefights.com/feed/wncb8W4K9uJnT2uG7
function PrimeOperations(X, Y) {
var x = getPrimeFactors(X),
    y = getPrimeFactors(Y),
    k = 0;
  
  for(i=0;x[i];i++) {
    for (j=0;y[j];j++) {
      if (x[i] == y[j]) {
        k += 2;
      }
    }
  }
  
  return (x.length + y.length) - k;
}

var getPrimeFactors = function(num) {
    num = Math.floor(num);
    var root, factors = [], x, sqrt = Math.sqrt, doLoop = 1 < num;
    while( doLoop ){
        root = sqrt(num);
        x = 2;
        if (num % x) {
            x = 3;
            while ((num % x) && ((x += 2) < root));
        }
        x = (x > root) ? num : x;
        factors.push(x);
        doLoop = ( x != num );
        num /= x;
    }
    return factors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment