Repository of Small Useful Code
function isPrime(num) { for (var i=(num-1); i > 1; i--) { if ((num % i) == 0) { return false; } } return true;}trace(isPrime(2)); //Outputs truetrace(isPrime(4)); //Outputs falsetrace(isPrime(17)); //Outputs true
One small issue with this, it returns true for 1, where 1 is not prime. If the following clause is added it seems to work great:if (num == 1) return false;Thanks for the code! :-)rob
One small issue with this, it returns true for 1, where 1 is not prime. If the following clause is added it seems to work great:
ReplyDeleteif (num == 1) return false;
Thanks for the code! :-)
rob