Sha256: 31941f40a83fcedf0bc5ca6ad12f1ca471d5a9e841da719fcd080825c81b4b5d

Contents?: true

Size: 581 Bytes

Versions: 181

Compression:

Stored size: 581 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 i, 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

181 entries across 181 versions & 1 rubygems

Version Path
trackler-2.0.7.0 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.44 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.43 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.42 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.41 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.40 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.39 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.38 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.37 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.36 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.35 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.34 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.33 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.32 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.31 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.30 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.29 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.28 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.27 tracks/ecmascript/exercises/sieve/example.js
trackler-2.0.6.26 tracks/ecmascript/exercises/sieve/example.js