function f(number) {
var result = [];
var d = 2;
while (number > 1) {
if (number % d === 0) {
number /= d;
result.push(d);
d = 2;
} else {
d++;
}
}
return result;
}
// Результат работы
f(10); // -> [2, 5]
f(3559); // -> [3559]
f(123456); // -> [2, 2, 2, 2, 2, 2, 3, 643]
// Проверка
f(123321).reduce(function(a, b) { return a * b }); // -> 123321
f(200).reduce(function(a, b) { return a * b }) === 200; // -> true
function primeFactorization(num){
var root = Math.sqrt(num),
result = arguments[1] || [],
x = 2;
if(num % x){
x = 3;
while((num % x) && ((x = x + 2) < root)){}
}
x = (x <= root) ? x : num;
result.push(x);
return (x === num) ? result : primeFactorization(num/x, result) ;
}