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.144 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.143 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.142 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.141 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.140 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.139 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.138 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.137 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.136 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.135 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.134 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.133 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.132 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.131 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.130 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.129 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.128 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.127 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.126 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.125 tracks/javascript/exercises/nth-prime/example.js