Sha256: 673b1f38752bcb76330d20955339cd7f0208beb3ebb389ca6fe02a8c253fc13b

Contents?: true

Size: 578 Bytes

Versions: 30

Compression:

Stored size: 578 Bytes

Contents

function newArrayWithRange(first, last) {
  let i;
  const array = [];
  for ( i = first; i <= last; i++ ) {
    array.push(i);
  }
  return array;
}

function indivisibleBy(value) {
  return value % this !== 0;
}

function sieve(n) {
  let prime, possibilities;
  const primes = [];

  possibilities = newArrayWithRange(2, n);

  do {
    prime = possibilities.shift();
    primes.push(prime);
    possibilities = possibilities.filter( indivisibleBy, prime );
  } while (possibilities.length > 0);

  return primes;
}

export default function (n) {
  this.primes = sieve(n);
}

Version data entries

30 entries across 30 versions & 1 rubygems

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