From 69b38977268dd5786f33ae28f8125920548ac956 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 15:40:47 +0530 Subject: [PATCH 1/4] Create factors.md --- snippets/factors.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 snippets/factors.md diff --git a/snippets/factors.md b/snippets/factors.md new file mode 100644 index 000000000..38760e47a --- /dev/null +++ b/snippets/factors.md @@ -0,0 +1,34 @@ +### factors + +Returns the array of factors of the given `int`. If the second argument is set to `true` returns only the prime factors of `int`.If the third argument is set to `true` will return an array of arrays each containing two elements, the `factor` and the number of times the `factor` divides `int`. +If `int` is `1` or `0` returns an empty array. +If `int` is less than `0` returns all the factors of `-int` together with their `additive inverses`. + +**Note**:- _Negative numbers are not considered prime._ +``` js +const factors = (int,prime = false,powers = false) => { + const howManyTimes = (num,divisor) => { + if([1,0].includes(divisor)) return Infinity + let i = 0 + while(Number.isInteger(num/divisor)){ + i++ + num = num / divisor + } + return i + } + const isPrime = num => { + const boundary = Math.floor(Math.sqrt(num)); + for (var i = 2; i <= boundary; i++) if (num % i == 0) return false; + return num >= 2 + } + let array = [] + const posFactor = num => { + let arr = [] + for (let i = 2; i <= num; i++) if (num % i === 0) arr.push(i) + return arr + } + if (int >= 0) array = posFactor(int) + else {posFactor(-int).forEach(el => array.push(el,-el))} + array = prime ? array.filter(el => isPrime(el)) : array; + return powers ? array.map(x => [x,howManyTimes(int,x)]) : array +}; From 3dfe28f2a70133baa8431e87a98a632db4dc5803 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 15:51:10 +0530 Subject: [PATCH 2/4] Add examples --- snippets/factors.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/snippets/factors.md b/snippets/factors.md index 38760e47a..8b4be7447 100644 --- a/snippets/factors.md +++ b/snippets/factors.md @@ -32,3 +32,14 @@ const factors = (int,prime = false,powers = false) => { array = prime ? array.filter(el => isPrime(el)) : array; return powers ? array.map(x => [x,howManyTimes(int,x)]) : array }; +``` +```js +factors(12); //[2,3,4,6,12] +factors(12,true); //[2,3] +factors(12,true,true); //[[2,2], [3,1]] +factors(12,false,true); //[[2,2], [3,1], [4,1], [6,1], [12,1]] +factors(-12); //[2, -2, 3, -3, 4, -4, 6, -6, 12, -12] +factors(12,true); //[2,3] +factors(12,true,true); //[[2,2], [3,1]] +factors(12,false,true); //[[2,2], [-2,2], [3,1], [-3,1], [4,1], [-4,1], [6,1], [-6,1], [12,1], [-12,1]] +``` From 81f8eb22f7316746f20fe790dea5d4b472b20621 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 3 Jan 2018 14:49:34 +0200 Subject: [PATCH 3/4] Use Array.from() Updated the code to utilize `Array.from()` to check for factors, this can be shortened even further (follow-up commits) --- snippets/factors.md | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/snippets/factors.md b/snippets/factors.md index 8b4be7447..0a8d32d24 100644 --- a/snippets/factors.md +++ b/snippets/factors.md @@ -18,21 +18,18 @@ const factors = (int,prime = false,powers = false) => { } const isPrime = num => { const boundary = Math.floor(Math.sqrt(num)); - for (var i = 2; i <= boundary; i++) if (num % i == 0) return false; + for (var i = 2; i <= boundary; i++) if (num % i === 0) return false; return num >= 2 } - let array = [] - const posFactor = num => { - let arr = [] - for (let i = 2; i <= num; i++) if (num % i === 0) arr.push(i) - return arr - } - if (int >= 0) array = posFactor(int) - else {posFactor(-int).forEach(el => array.push(el,-el))} + let isNeg = int < 0; + int = isNeg ? -int : int; + let array = Array.from({length: int - 1}).map((val,i) => int % (i+2) === 0 ? (i+2) : false).filter(val => val); + if(isNeg) array = array.reduce((acc,val) => {acc.push(val); acc.push(-val); return acc}, []); array = prime ? array.filter(el => isPrime(el)) : array; return powers ? array.map(x => [x,howManyTimes(int,x)]) : array -}; +} ``` + ```js factors(12); //[2,3,4,6,12] factors(12,true); //[2,3] From b0cda155ef1bcc6c8e0c276196f60651f9f3cc7a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 3 Jan 2018 15:04:03 +0200 Subject: [PATCH 4/4] Update factors.md --- snippets/factors.md | 51 +++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/snippets/factors.md b/snippets/factors.md index 0a8d32d24..dbe56a1b8 100644 --- a/snippets/factors.md +++ b/snippets/factors.md @@ -1,42 +1,35 @@ ### factors -Returns the array of factors of the given `int`. If the second argument is set to `true` returns only the prime factors of `int`.If the third argument is set to `true` will return an array of arrays each containing two elements, the `factor` and the number of times the `factor` divides `int`. -If `int` is `1` or `0` returns an empty array. -If `int` is less than `0` returns all the factors of `-int` together with their `additive inverses`. +Returns the array of factors of the given `num`. +If the second argument is set to `true` returns only the prime factors of `num`. +If `num` is `1` or `0` returns an empty array. +If `num` is less than `0` returns all the factors of `-int` together with their additive inverses. + +Use `Array.from()`, `Array.map()` and `Array.filter()` to find all the factors of `num`. +If given `num` is negative, use `Array.reduce()` to add the additive inverses to the array. +Return all results if `primes` is `false`, else determine and return only the prime factors using `isPrime` and `Array.filter()`. +Omit the second argument, `primes`, to return prime and non-prime factors by default. **Note**:- _Negative numbers are not considered prime._ + ``` js -const factors = (int,prime = false,powers = false) => { - const howManyTimes = (num,divisor) => { - if([1,0].includes(divisor)) return Infinity - let i = 0 - while(Number.isInteger(num/divisor)){ - i++ - num = num / divisor - } - return i - } +const factors = (num, primes = false) => { const isPrime = num => { - const boundary = Math.floor(Math.sqrt(num)); - for (var i = 2; i <= boundary; i++) if (num % i === 0) return false; - return num >= 2 + const boundary = Math.floor(Math.sqrt(num)); + for (var i = 2; i <= boundary; i++) if (num % i === 0) return false; + return num >= 2; } - let isNeg = int < 0; - int = isNeg ? -int : int; - let array = Array.from({length: int - 1}).map((val,i) => int % (i+2) === 0 ? (i+2) : false).filter(val => val); + const isNeg = num < 0; + num = isNeg ? -num : num; + let array = Array.from({length: num - 1}).map((val,i) => num % (i+2) === 0 ? (i+2) : false).filter(val => val); if(isNeg) array = array.reduce((acc,val) => {acc.push(val); acc.push(-val); return acc}, []); - array = prime ? array.filter(el => isPrime(el)) : array; - return powers ? array.map(x => [x,howManyTimes(int,x)]) : array + return primes ? array.filter(isPrime) : array; } ``` ```js -factors(12); //[2,3,4,6,12] -factors(12,true); //[2,3] -factors(12,true,true); //[[2,2], [3,1]] -factors(12,false,true); //[[2,2], [3,1], [4,1], [6,1], [12,1]] -factors(-12); //[2, -2, 3, -3, 4, -4, 6, -6, 12, -12] -factors(12,true); //[2,3] -factors(12,true,true); //[[2,2], [3,1]] -factors(12,false,true); //[[2,2], [-2,2], [3,1], [-3,1], [4,1], [-4,1], [6,1], [-6,1], [12,1], [-12,1]] +factors(12); // [2,3,4,6,12] +factors(12,true); // [2,3] +factors(-12); // [2, -2, 3, -3, 4, -4, 6, -6, 12, -12] +factors(-12, true); // [2,3] ```