Functions

Efficient Algorithm

Enter a number between 2 and a few quadrillion.

Inefficient Algorithm

Just for fun, I tried finding factors using bitwise operators instead of "%", the modulo operater. The Google Chrome engine processed it very slowly.

Enter a number between 2 and not much greater than a billion.


Here's the code for the more efficient, modulo-operator algorithm:

let N2 = 1;
let ar2 = [];

var first2 = function first (e) {
    ar2 = [];
    if (e.keyCode == 13) {
        let num = parseInt(e.target.value);
        main2(num);
    };
};

function main2 (n) {
    N2 = n;
    for(let i = 2; i <= N2; i+=1){
        while (N2 % i === 0) {
            ar2.push(" " + i);
            N2 = N2 / i;
            if (N2 == 1) return
        }
    }
}

This is the code for the bitwise-operator algorithm:

function main (n) {
    if (n === 1) return;
    N = n;
    var buf = new ArrayBuffer(Math.ceil(N/8));
    var view = new Uint8Array(buf); 
    var top = Math.ceil(Math.sqrt(N));
    let i = 2;
    for(i; i <= N; i+=1){
        if(test(view, i)) {
            for(let j = i; j <= N; j+=i) {
                set(view, j);
                if (j === N) {
                    ar.push(" " + i);
                    N = N / i;
                    main(N);
                }
            }
        }
    }
};

function set(ar,i) {
  var a = Math.floor(i / 8)
  var b = i % 8
  ar[a] |= (1 << b)
};

function test (ar,i) {
  var a = Math.floor(i / 8);
  var b = i % 8;
  var value = ar[a] & (1 << b);
  return value === 0;
}
Game of Score