Skip to content

Instantly share code, notes, and snippets.

@hueitan

hueitan/gogo.md

Created May 5, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.