From 796b73dcce69b62b44692d1dd89200dd4ca2d31e Mon Sep 17 00:00:00 2001 From: Rohit Date: Thu, 21 Dec 2017 15:50:22 +0530 Subject: [PATCH 1/3] add snippet primes --- snippet-template.md | 2 +- snippets/primes.md | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 snippets/primes.md diff --git a/snippet-template.md b/snippet-template.md index 63d6665d8..f95eabc3d 100644 --- a/snippet-template.md +++ b/snippet-template.md @@ -8,4 +8,4 @@ Explain briefly how the snippet works. const functionName = arguments => {functionBody} // functionName(sampleInput) -> sampleOutput -``` +``` \ No newline at end of file diff --git a/snippets/primes.md b/snippets/primes.md new file mode 100644 index 000000000..31dafc045 --- /dev/null +++ b/snippets/primes.md @@ -0,0 +1,15 @@ +### primes + +It generates primes till a given number ```m + +Explain briefly how the snippet works. + +```js +const primes = num =>{ + var arr = Array.from({length:num-1}).map((x,i)=> i+2); + var sqroot = Math.floor(Math.sqrt(num)); + var numsTillSqroot = Array.from({length:numb-1}).map((x,i)=> i+2); + arra.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y== x))); + return arr; +}// primes(10) -> [2,3,5,7] +``` \ No newline at end of file From 9af15faf0831151a7c37c57a229d3f4c4d05ea72 Mon Sep 17 00:00:00 2001 From: Rohit Date: Thu, 21 Dec 2017 15:53:54 +0530 Subject: [PATCH 2/3] Fixed a typo --- snippets/primes.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/snippets/primes.md b/snippets/primes.md index 31dafc045..2714a8e60 100644 --- a/snippets/primes.md +++ b/snippets/primes.md @@ -1,15 +1,14 @@ ### primes -It generates primes till a given number ```m - -Explain briefly how the snippet works. +It generates primes till a given number. +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 -const primes = num =>{ +const primes = num => { var arr = Array.from({length:num-1}).map((x,i)=> i+2); var sqroot = Math.floor(Math.sqrt(num)); - var numsTillSqroot = Array.from({length:numb-1}).map((x,i)=> i+2); - arra.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y== x))); + var 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] ``` \ No newline at end of file From fbf81b7fa3c3c59f798896a080a3bf19981e7d56 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 21 Dec 2017 14:45:43 +0200 Subject: [PATCH 3/3] Update primes.md --- snippets/primes.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/snippets/primes.md b/snippets/primes.md index 2714a8e60..81e8f2999 100644 --- a/snippets/primes.md +++ b/snippets/primes.md @@ -1,14 +1,16 @@ ### primes -It generates primes till a given number. +Generates primes up to a given number, using the Sieve of Eratosthenes. + +Generate an array from `2` to the given number. Use `Array.filter()` to filter out the values divisible by any number from `2` to the square root of the provided number. -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 const primes = num => { - var arr = Array.from({length:num-1}).map((x,i)=> i+2); - var sqroot = Math.floor(Math.sqrt(num)); - var numsTillSqroot = Array.from({length:sqroot-1}).map((x,i)=> i+2); + 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] -``` \ No newline at end of file +} +// primes(10) -> [2,3,5,7] +```