Fixed a typo
This commit is contained in:
@ -1,15 +1,14 @@
|
|||||||
### primes
|
### primes
|
||||||
|
|
||||||
It generates primes till a given number ```m
|
It generates primes till a given number.
|
||||||
|
|
||||||
Explain briefly how the snippet works.
|
|
||||||
|
|
||||||
|
It works with the Sieve of Eratosthenes. It generate an array from 2 to the given number. Then it filters out the values (Using Array.filter()) divisible by any number from 2 to square root of the provided number.
|
||||||
```js
|
```js
|
||||||
const primes = num =>{
|
const primes = num => {
|
||||||
var arr = Array.from({length:num-1}).map((x,i)=> i+2);
|
var arr = Array.from({length:num-1}).map((x,i)=> i+2);
|
||||||
var sqroot = Math.floor(Math.sqrt(num));
|
var sqroot = Math.floor(Math.sqrt(num));
|
||||||
var numsTillSqroot = Array.from({length:numb-1}).map((x,i)=> i+2);
|
var numsTillSqroot = Array.from({length:sqroot-1}).map((x,i)=> i+2);
|
||||||
arra.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y== x)));
|
numsTillSqroot.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y==x)));
|
||||||
return arr;
|
return arr;
|
||||||
}// primes(10) -> [2,3,5,7]
|
}// primes(10) -> [2,3,5,7]
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user