I am trying to sort all the divisors of a given number in the most efficient way.
Below is my code for sorting the divisors:
num = 100;
end = num ** (1 / 2);
divisors = [1];
disvisorPairs = [];
pairCount = 0;
for (var i = 2; i <= end; i++) {
if (num % i == 0) {
divisors.push(i);
pair = num / i;
if (pair != i) {
pairCount++;
disvisorPairs.push(pair);
}
}
}
for (var i = pairCount - 1; i >= 0; i--) {
divisors.push(disvisorPairs[i]);
}
if (num != 1) divisors.push(num);
console.log(divisors);
- Is there anything else I can do to increase the performance of my code?
- Is there a data structure other than arrays that can possibly sort the the divisors in constant time?