This function takes a number and returns its prime factors as an array. I ran a bunch of test cases and all comes out correct except this number 90000004000000001.
The code is not optimized at all, so most numbers that large will time out. However, this one test case computes for some reason and it gives me the factors [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 89, 241, 1049] which is obviously nonsense since 90000004000000001 is an odd number.
When multiplying the factors together the resulting number is 90000004000000000. 1 lower than the original number.
That's strange, so I tried 90000004000000001 % 2 which returns0.
I'm starting to think my function might not be the problem here but rather modulus or numbers in javascript. How come an even number can be divisible by 2 as in this case?
I put a few test cases as console logs together with the code snippet so you can see the results.
Thanks!
// Assuming it works will factor an integer into its prime components and return them as an array
function factorize(num) {
num = Math.floor(num);
// the array to store all factors
let factors = [];
// goes through all numbers starting at 2
// break when a factor is found
// will also break if num is prime (its own factor)
let i = 2;
while (true) {
if (num % i === 0) {
factors.push(i);
break;
}
i++
}
// i is the factor from the while loop
// if num is not prime recursively factorize the remainder after factoring out i
if (i !== num) {
factors = factors.concat(factorize(num / i));
}
return factors;
}
console.log(factorize(16)); //[2, 2, 2, 2]
console.log(factorize(101)); //[101]
console.log(factorize(100001)); //[11, 9091]
console.log(factorize(141)); //[3, 47]
console.log(factorize(90000004000000001)); //[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 89, 241, 1049]
console.log(factorize(90000004000000001).reduce((a, b) => a * b)); //90000004000000000