If your engine doesn't need to render objects outside the viewport, then you don't need to generate those objects until you actually need to render it.
n Number of factors
2 2 (1, 2)
2x3 4 (1, 2, 3, 6)
2x3x5 8
2x3x5x7 16
Notice that each time you add a new prime number, the factor count doubles (2x3x5x...x500499th prime)x2
has 2^500499 + 2^500498 factors.
This is because with the extra 2 we added, it produces additional factors with existing factors that are a multiples of 2. (2x3x5x...x500499th prime) x (2 x 2)
has 2^500499 + 2^500498 + 2^500498 = 2^500500 factors.
This is a better solution, since 2x2 is smaller than the 500500th prime. factor count n
2 2
4 2*3 <1>
8 2*3*2*2 <2>
16 2*3*2*2*5 <1>
32 2*3*2*2*5*7 <1>
64 2*3*2*2*5*7*3*3 <2>
128 2*3*2*2*5*7*3*3*11 <1>
For 16 factors the number works out to be 120 (just like the example!). For numbers shown in the questions, sieving the prime takes some time, I also found it helpful to use a binary heap for speeding up finding the next smallest factors. Backend.CallA(param, function() {
aDone = true;
if (aDone && bDone) { doC(); }
});
Backend.CallB(param, function() {
bDone = true;
if (aDone && bDone) { doC(); }
});
Promises: var promiseA = Backend.CallA(param);
var promiseB = Backend.CallB(param);
$q.all([promiseA, promiseB]).then(doC);