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-1.0.0 tracks/ecmascript/exercises/sieve/example.js