Travis build: 930

This commit is contained in:
Travis CI
2018-01-03 13:07:44 +00:00
parent d3e8c2f09e
commit 52f3011cd2
4 changed files with 95 additions and 7 deletions

View File

@ -287,6 +287,15 @@
</details> </details>
### _Uncategorized_
<details>
<summary>View contents</summary>
* [`factors`](#factors)
</details>
--- ---
## 🔌 Adapter ## 🔌 Adapter
@ -5203,6 +5212,54 @@ yesNo('Foo', true); // true
<br>[⬆ Back to top](#table-of-contents) <br>[⬆ Back to top](#table-of-contents)
---
## _Uncategorized_
### factors
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 = (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 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;
}, []);
return primes ? array.filter(isPrime) : array;
};
```
```js
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]
```
<br>[⬆ back to top](#table-of-contents)
## Collaborators ## Collaborators

File diff suppressed because one or more lines are too long

View File

@ -18,13 +18,20 @@ const factors = (num, primes = false) => {
const boundary = Math.floor(Math.sqrt(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; return num >= 2;
} };
const isNeg = num < 0; const isNeg = num < 0;
num = isNeg ? -num : num; num = isNeg ? -num : num;
let array = Array.from({length: num - 1}).map((val,i) => num % (i+2) === 0 ? (i+2) : false).filter(val => val); let array = Array.from({ length: num - 1 })
if(isNeg) array = array.reduce((acc,val) => {acc.push(val); acc.push(-val); return acc}, []); .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;
}, []);
return primes ? array.filter(isPrime) : array; return primes ? array.filter(isPrime) : array;
} };
``` ```
```js ```js

View File

@ -39,6 +39,7 @@ escapeRegExp:string
everyNth:array everyNth:array
extendHex:utility extendHex:utility
factorial:math factorial:math
factors:uncategorized
fibonacci:math fibonacci:math
fibonacciCountUntilNum:math fibonacciCountUntilNum:math
fibonacciUntilNum:math fibonacciUntilNum:math