Skip to content

Instantly share code, notes, and snippets.

@Steffan153
Created June 13, 2020 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Steffan153/3ef33ddc33644136f48436235849f7a7 to your computer and use it in GitHub Desktop.
Save Steffan153/3ef33ddc33644136f48436235849f7a7 to your computer and use it in GitHub Desktop.
function isPrime(n) {
if (n === 2) return true;
for (let i = 2; i <= Math.sqrt(n); i++)
if (n % i === 0) return false;
return true;
}
function primeFactorization(n) {
let d = [];
for (let i = 2; i < n; i++)
if (n % i === 0) { d.push(n / i, i); break; }
if (d.length === 0) return [1, n];
for (let i in d) if (!isPrime(d[i])) d.splice(i, 1, ...primeFactorization(d[i]));
return d.sort((a, b) => a - b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment