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.1.0.25 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.24 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.23 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.22 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.21 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.20 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.19 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.18 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.17 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.16 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.15 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.14 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.13 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.12 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.11 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.10 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.9 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.8 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.7 tracks/ecmascript/exercises/sieve/example.js
trackler-2.1.0.6 tracks/ecmascript/exercises/sieve/example.js