Skip to content

Commit 576c5df

Browse files
committed
Prime factors / 중급
1 parent c45bd34 commit 576c5df

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://school.programmers.co.kr/learn/courses/30/lessons/120852
2+
3+
// n이 주어질때, n의 소인수를 오름차순으로 담은 배열 리턴하기
4+
function solution(n) {
5+
const factors = new Set()
6+
for (let i = 2; i * i <= n; i++) {
7+
while (n % i === 0) {
8+
factors.add(i)
9+
n /= i
10+
}
11+
}
12+
// 마지막으로 남은 수가 1보다 크면 그 자체가 소수이므로 추가
13+
if (n > 1) factors.add(n)
14+
return Array.from(factors)
15+
}

0 commit comments

Comments
 (0)