Update factors.md

This commit is contained in:
Angelos Chalaris
2018-01-03 15:04:03 +02:00
committed by GitHub
parent 81f8eb22f7
commit b0cda155ef

View File

@ -1,42 +1,35 @@
### factors ### 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`. Returns the array of factors of the given `num`.
If `int` is `1` or `0` returns an empty array. If the second argument is set to `true` returns only the prime factors of `num`.
If `int` is less than `0` returns all the factors of `-int` together with their `additive inverses`. 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._ **Note**:- _Negative numbers are not considered prime._
``` js ``` js
const factors = (int,prime = false,powers = false) => { const factors = (num, primes = 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 isPrime = num => {
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;
} }
let isNeg = int < 0; const isNeg = num < 0;
int = isNeg ? -int : int; num = isNeg ? -num : num;
let array = Array.from({length: int - 1}).map((val,i) => int % (i+2) === 0 ? (i+2) : false).filter(val => val); 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}, []); if(isNeg) array = array.reduce((acc,val) => {acc.push(val); acc.push(-val); return acc}, []);
array = prime ? array.filter(el => isPrime(el)) : array; return primes ? array.filter(isPrime) : array;
return powers ? array.map(x => [x,howManyTimes(int,x)]) : array
} }
``` ```
```js ```js
factors(12); //[2,3,4,6,12] factors(12); // [2,3,4,6,12]
factors(12,true); //[2,3] factors(12,true); // [2,3]
factors(12,true,true); //[[2,2], [3,1]] factors(-12); // [2, -2, 3, -3, 4, -4, 6, -6, 12, -12]
factors(12,false,true); //[[2,2], [3,1], [4,1], [6,1], [12,1]] factors(-12, true); // [2,3]
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]]
``` ```