Skip to content

Instantly share code, notes, and snippets.

@hueitan
Created May 5, 2015 02:32

Revisions

  1. Huei Tan created this gist May 5, 2015.
    35 changes: 35 additions & 0 deletions gogo.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    ```js
    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;
    }
    ```