Created
June 13, 2020 17:29
-
-
Save Steffan153/3ef33ddc33644136f48436235849f7a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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