Is a number prime - Node.js Number

Node.js examples for Number:Algorithm

Description

Is a number prime

Demo Code


Math.isPrime = function(n) {
  if(n==2) { return true; }
  if( (n < 2) || (n%2 == 0) ) { return false; }
  for(var i=3; (i*i)<=n; i+=2) {
    if(n%i == 0) { return false; }
  }/*from   ww  w .  j av  a2 s.  c om*/
  return true;
};

Related Tutorials