Sha256: fbcfa4796d13eb8b6c32fba44500b47ad4f844b90cf492342a101ab8a0af023c

Contents?: true

Size: 857 Bytes

Versions: 211

Compression:

Stored size: 857 Bytes

Contents

let realPrimes = [];

function generatePrimes(uptoNumber) {
  if (realPrimes.length > 0) {
    return realPrimes;
  }

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

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

  return possiblePrimes
    .filter(candidate => candidate.prime)
    .map(p => p.number);
}

class Prime {
  nth(nthPrime) {
    if (nthPrime === 0) {
      throw new Error('Prime is not possible');
    }
    realPrimes = generatePrimes(200000);
    return realPrimes[nthPrime - 1];
  }
}

export default Prime;

Version data entries

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/nth-prime/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/nth-prime/example.js