Last active
April 3, 2022 13:39
-
-
Save tetsuok/7fdab9eb8b58d9fe099d678357e6f146 to your computer and use it in GitHub Desktop.
素因数分解
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
'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