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.124 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.123 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.122 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.121 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.120 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.119 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.118 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.117 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.116 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.115 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.114 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.113 tracks/javascript/exercises/nth-prime/example.js
trackler-2.2.1.111 tracks/javascript/exercises/nth-prime/example.js