Skip to content

Instantly share code, notes, and snippets.

@PatheticMustan
Created December 8, 2023 09:33
Show Gist options
  • Save PatheticMustan/e85cf125a48593e589f7a572ae444a1b to your computer and use it in GitHub Desktop.
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)
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