Sha256: 88bad4e31bc93848cc0993550f747fbbe842cc0ca99ad8aff6ad8dca3cf618c9

Contents?: true

Size: 973 Bytes

Versions: 53

Compression:

Stored size: 973 Bytes

Contents

'use strict';

module.exports = {
  nth: function (nthPrime) {
    if (nthPrime === 0) { throw new Error('Prime is not possible'); }
    this.generatePrimes(200000);
    return this.realPrimes[nthPrime - 1];
  },
  generatePrimes: function (uptoNumber) {
    var i;
    var j;
    var currentPrime;
    var possiblePrimes = [];

    for (i = 2; i <= uptoNumber; i++) {
      possiblePrimes.push({ number: i, prime: true });
    }

    for (i = 2; i < Math.sqrt(possiblePrimes.length); i++) {
      for (j = 0; j < possiblePrimes.length; j++) {
        currentPrime = possiblePrimes[j];
        if (currentPrime.number !== i && currentPrime.number % i === 0) {
          currentPrime.prime = false;
        }
      }
    }

    this.realPrimes = [];

    for (i = 0; i < possiblePrimes.length; i++) {
      currentPrime = possiblePrimes[i];
      if (currentPrime.prime) {
        this.realPrimes.push(currentPrime.number);
      }
    }
    return this.realPrimes;
  }

};

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
trackler-2.2.1.164 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.163 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.162 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.161 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.160 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.159 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.158 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.157 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.156 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.155 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.154 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.153 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.152 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.151 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.150 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.149 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.148 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.147 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.146 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.145 tracks/javascript/exercises/nth-prime/example.js