-
Notifications
You must be signed in to change notification settings - Fork 0
/
소수찾기.js
40 lines (35 loc) · 924 Bytes
/
소수찾기.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
function solution(numbers) {
const n = numbers.length;
const set = new Set();
for (let m = 1; m <= n; m += 1) {
let visited = Array.from({ length: n }, () => 0);
let temp = Array.from({ length: m }, () => 0);
DFS(0, m, visited, temp);
}
function DFS(L, m, visited, temp) {
if (L === m) {
const number = Number(temp.join(""));
const isPrimeNumber = isPrime(number);
if (isPrimeNumber) set.add(number);
} else {
for (let i = 0; i < n; i += 1) {
if (!visited[i]) {
visited[i] = 1;
temp[L] = numbers[i];
DFS(L + 1, m, visited, temp);
visited[i] = 0;
}
}
}
}
return set.size;
}
function isPrime(n) {
if (n === 0 || n === 1) return false;
let flag = true;
for (let i = 2; i <= Math.floor(n / 2) + 1; i += 1) {
if (!flag) return false;
if (n % i === 0) flag = false;
}
return true;
}