-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindingPrimeNumber.js
66 lines (53 loc) · 1.52 KB
/
FindingPrimeNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function solution(numbers) {
const numArr = numbers.split("");
const arr = new Set();
// 소수인지 확인하는 함수
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
function makeArr(number, used) {
if (number.length > 0) arr.add(Number(number));
for (let i = 0; i < numArr.length; i++) {
if (!used[i]) {
const newUsed = [...used];
newUsed[i] = true;
makeArr(number + numArr[i], newUsed);
}
}
}
makeArr("", Array(numArr.length).fill(false));
// 숫자로 변환한 후 소수만 필터링
const primeArr = Array.from(arr).filter(isPrime);
return primeArr.length;
}
// 2차 풀이
function solution(numbers) {
const numArr = numbers.split("");
const set = new Set();
// 소수인지 확인하는 로직
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i < Math.sqrt(num); i++) {
if (num % 2 === 0) return false;
}
return true;
}
// 돌아가면서 숫자 경우를 생성하는 로직
function makeNum(number, used) {
if (number.length > 0) set.add(Number(number));
for (let i = 0; i < numArr.length; i++) {
if (!used[i]) {
const newUsed = [...used];
newUsed[i] = true;
makeNum(number + numArr[i], newUsed);
}
}
}
makeNum("", Array(numArr.length).fill(false));
const primeArr = Array.from(set).filter(isPrime);
return primeArr.length;
}