Nodejs Number Prime Number Check isPrime(n)

Here you can find the source of isPrime(n)

Method Source Code

Number.prototype.type = 'number';

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  w  ww . j  a  va 2s .c o m
   return true;
};

Math.isNumber = function isNumber(vtn) { //ref: http://stackoverflow.com/questions/9716468/is-there-any-function-like-isnumeric-in-javascript-to-validate-numbers
  return !isNaN(parseFloat(vtn)) && isFinite(vtn);
}

Related

  1. IsPrime()
    Number.prototype.IsPrime = function() {
      var n = 2,
          isPrime = true;
      while (n < this / 2 && isPrime) {
        isPrime = this % n != 0;
        n++;
      return isPrime;
    var primeCount = 0,
        current = 1;
    while (primeCount <= 10001) {
      current++;
      if (current.IsPrime()) {
        primeCount++;
    console.log(current);
    
  2. isPrime()
    Number.prototype.isPrime = function() {
      var primeCandidate = this;
      if(primeCandidate <= 1 || primeCandidate%1 !== 0) return false
      var i = 2;
      var top = Math.floor(Math.sqrt(primeCandidate));
      while(i<=top){
        if(primeCandidate%i === 0){ return false; }
        i++;
      return true;
    
  3. isPrime()
    Number.prototype.isPrime=function(){
        for(var n=2;n<this;)
          if(!(this%n++))
             return !1;
        return !0}
    function PrimeTime(num) {
      return num.isPrime();
    PrimeTime(readline());
    ...
    
  4. isPrime()
    Number.prototype.isPrime=function(){
       for(var n=2;n<this;)
          if(!(this%n++))
             return !1;
       return !0
    var getNextPrime = function(number, max) {
      while(++number <= max)
        if (number.isPrime())
    ...