Generate prime number considering a couple of facts. first one needs the factors for a number to see if its prime. for example 4 has prime while 5 has factors of 1,5=6 so 5 is the prime. given these facts write the following functions using only javascript functional programming elements.
---------------------------------------------------------------------------------------------
Javascript Code:
function findPrimeFactors (num) {
var primeFactors = [];
while (num % 2 === 0) {
primeFactors.push(2);
num = num / 2;
}
var sqrtNum = Math.sqrt(num);
for (var i = 3; i <= sqrtNum; i++) {
while (num % i === 0) {
primeFactors.push(i);
num = num / i;
}
}
if (num > 2) {
primeFactors.push(num);
}
return primeFactors;
}
//Output Example:
console.log(findPrimeFactors(10)); // [2, 5]
console.log(findPrimeFactors(11)); // [11]
Get Answers For Free
Most questions answered within 1 hours.