Sha256: 6796b1808b5b51b8799984523cd45619bd9e9913d2742e5a52d9c9aa3b5eb070

Contents?: true

Size: 598 Bytes

Versions: 119

Compression:

Stored size: 598 Bytes

Contents

'use strict';

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

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

function sieve(n) {
  var prime;
  var possibilities;
  var primes = [];

  possibilities = newArrayWithRange(2, n);

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

  return primes;
}

module.exports = function (n) {
  this.primes = sieve(n);
};

Version data entries

119 entries across 119 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.138 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.137 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.136 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.135 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.134 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.133 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.132 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.131 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.130 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.129 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.128 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.127 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.126 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.125 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.124 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.123 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.122 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.121 tracks/javascript/exercises/sieve/example.js
trackler-2.2.1.120 tracks/javascript/exercises/sieve/example.js