Welcome to my actionscript snippet library!

Actionscript Snips is a small repository of actionscript code that aims to save developers and designers time when developing a website.  

Determine Whether Number is a Prime Number


function isPrime(num) {
for (var i=(num-1); i > 1; i--) {
if ((num % i) == 0) {
return false;
}
}
return true;
}

trace(isPrime(2)); //Outputs true
trace(isPrime(4)); //Outputs false
trace(isPrime(17)); //Outputs true

1 comments:

Anonymous said...

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

Post a Comment