update snippets 78 - 96

This commit is contained in:
Stefan Feješ
2017-12-25 14:33:49 +01:00
committed by Agamemnon Zorbas
parent c829ed692e
commit 9ef34b7392
18 changed files with 90 additions and 43 deletions

View File

@@ -1,4 +1,4 @@
### primes
### primes
Generates primes up to a given number, using the Sieve of Eratosthenes.
@@ -6,11 +6,14 @@ Generate an array from `2` to the given number. Use `Array.filter()` to filter o
```js
const primes = num => {
let arr = Array.from({length: num - 1}).map((x, i) => i + 2),
sqroot = Math.floor(Math.sqrt(num)),
numsTillSqroot = Array.from({length: sqroot - 1}).map((x, i) => i + 2);
numsTillSqroot.forEach(x => arr = arr.filter(y => ((y % x) !== 0) || (y == x)));
let arr = Array.from({length:num-1}).map((x,i)=> i+2),
sqroot = Math.floor(Math.sqrt(num)),
numsTillSqroot = Array.from({length:sqroot-1}).map((x,i)=> i+2);
numsTillSqroot.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y==x)));
return arr;
};
// primes(10) -> [2,3,5,7]
}
```
```js
primes(10) // [2,3,5,7]
```