Nodejs Number Prime Number Check isPrime()

Here you can find the source of isPrime()

Method Source Code

// PROBLEM //// w ww .j a  va 2 s . c o m

/*
Using the JavaScript language, have the function PrimeMover(num) return the
numth prime number. The range will be from 1 to 10^4. For example: if num is
16 the output should be 53 as 53 is the 16th prime number.
*/

// HELPERS //

Number.prototype.isPrime=function(){
   for(var n=2;n<this;)
      if(!(this%n++))
         return !1;
   return !0
}

// ANSWER //

var getNextPrime = function(number, max) {
  while(++number <= max)
    if (number.isPrime())
      return number;
}

function PrimeMover(num) {
  var prime = 1;
  for (var i = 0; i < num; i++)
    prime = getNextPrime(prime, 1000)
  return prime;
}

PrimeMover(readline());

Related

  1. isPrime(n)
    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; }
      return true;
    };
    ...
    
  2. 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);
    
  3. 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;
    
  4. 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());
    ...
    
  5. isPrime()
    Number.prototype.isPrime = function () {
      var i = 2;
      while (i<=this - 1) {
        if (this % i ==0) {
          return false;
          break;
        i++;
      if (i == this) {
        return true;
    };
    
  6. primeFactorization()
    Number.prototype.primeFactorization = function(){
         var ans = [];
        var primes = [];
        var temp = this;
        for(let i=2; i<=this/2; i++){
            if(i.divisors().length==2){
                primes.push(i)
        for(let i = 0; i<primes.length;i++){
            while(temp.isDivisor(primes[i])){
                ans.push(primes[i]);
                temp = temp/primes[i];
        return ans;
    
  7. primeFactorization()
    Number.prototype.primeFactorization = function() {
      var ans = [];
      var primes = [];
      var temp = this;
      for (let i = 0; i <= temp/2; i++) {
        if (i.allDivisors().length == 2) {
          primes.push(i)
      for (let i = 0; i < primes.length; i++) {
        while (temp.isDivisor(primes[i])) {
          ans.push(primes[i]);
          temp = temp / primes[i];
      console.log(ans);
    
  8. primeFactorization()
    Number.prototype.primeFactorization = function(){
      var ans = [];
      var temp = this;
      var primes = [];
      for(let i = 2;i<=this/2;i++){
        if(i.divisors().length == 2){
          primes.push(i);
          if(temp==1){
            break;
    ...