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
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
Posted by
admin
on Wednesday, August 5, 2009
Labels:
Number
1 comments:
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