Created
December 8, 2023 09:33
-
-
Save PatheticMustan/e85cf125a48593e589f7a572ae444a1b to your computer and use it in GitHub Desktop.
get all prime factors of a number (useful for lcm of many numbers)
This file contains 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
const divisors = new Set(); | |
let n = 12379872; | |
const bound = Math.sqrt(n); | |
while (n > 1) { | |
for (let i=2; i<=bound; i++) { | |
if (n % i === 0) { | |
divisors.add(i); | |
n /= i; | |
break; | |
} | |
} | |
} | |
if (n !== 1) divisors.add(n); | |
console.log(Array.from(divisors)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment