bit.ly/3j2UpqA
const isPrime = (number) => { let list = [...Array(Math.floor(number ** 0.5)).keys()] .map((x) => x + 1) .slice(1); return !list.some((n) => number % n === 0); }; const findPrimes = (number) => [...Array(number + 1).keys()].slice(2).filter((n) => isPrime(n)); const factorize = (number, prime) => { let result = 0; for (let i = number; i >= prime; ) { let div = Math.floor(i / prime); result += div; i = div; } return result > 1 ? `${prime}^${result}` : `${prime}`; }; const decomp = (n) => findPrimes(n) .map((prime) => factorize(n, prime)) .join(" * ");
You can check for divisors in [2;ceiling(sqrt(n))], that should be way faster for large n
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
bit.ly/3j2UpqA
You can check for divisors in [2;ceiling(sqrt(n))], that should be way faster for large n