Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Last active April 3, 2022 13:39
Show Gist options
  • Save tetsuok/7fdab9eb8b58d9fe099d678357e6f146 to your computer and use it in GitHub Desktop.
Save tetsuok/7fdab9eb8b58d9fe099d678357e6f146 to your computer and use it in GitHub Desktop.
素因数分解
'use strict';
// 自然数nを素因数分解して、素因数の配列を返す.
function calcPrimeFactor(n) {
const result = [];
let x = n;
for (let i = 2; i * i <= x; i++) {
while (x % i === 0) {
result.push(i);
x /= i;
}
}
if (x !== 1) {
result.push(x);
}
return result;
}
console.log(calcPrimeFactor(286));
console.log(calcPrimeFactor(20211225));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment