I am trying to optimise the usage of a function to check whether a number is prime or not.
I have written the following function:
function isPrime(num) {
var start = 2;
// code to check whether num has already been checked for Prime
while(start <= Math.sqrt(num)) {
if (num % start++ < 1) {
return false;
}
}
return num > 1;
}
However, before the execution of my while
loop I want to check if a number already has been passed through my isPrime function, so that I can return whether it is prime or not without having to execute the while
loop.
Note, I want to do this without the usage of a global variable or without extending Object.prototype
.