-
Notifications
You must be signed in to change notification settings - Fork 0
/
소수찾기.js
87 lines (74 loc) · 2.59 KB
/
소수찾기.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
처음 작성한 답안
오랫동안 해결하지 못한 문제였지만, 순열과 조합을 이해하고 작성할 수 있게 된 후에는
쉽게 구할 수 는 있었다.
순열로 모든경우 -> 1차 중복 필터 -> 소수판별 -> result에 넣기
-> result 최종 중복 필터 -> 개수 리턴
근데 중복을 제거하기 위해 처음부터 Set을 사용했으면 조금더 간결했을 법하다.
*/
// function solution(numbers) {
// let result = [];
// numbers = numbers.split("");
// for (let i = 1; i <= numbers.length; i++) {
// let numberList = combination(numbers, i);
// numberList = numberList.map((v) => v.join("") * "1");
// numberList = deleteOverlap(numberList);
// numberList = numberList.filter((v) => isDecimal(v));
// numberList.forEach((v) => result.push(v));
// }
// return deleteOverlap(result).length;
// }
// function combination(arr, selectNum) {
// const result = [];
// if (selectNum === 1) return arr.map((v) => [v]);
// arr.forEach((v, idx, arr) => {
// const fixed = v;
// const restArr = arr.filter((v, index) => index !== idx);
// const combinationArr = combination(restArr, selectNum - 1);
// const combineFixed = combinationArr.map((v) => [fixed, ...v]);
// result.push(...combineFixed);
// });
// return result;
// }
// function isDecimal(x) {
// if (x < 2) return false;
// //i 의 범위를 x의 절반으로 하면 시간 감축가능
// for (let i = 2; i <= Math.sqrt(x); i++) {
// if (x % i === 0) return false;
// }
// return true;
// }
// function deleteOverlap(arr) {
// return [...new Set(arr)];
// }
function solution(numbers) {
let result = new Set();
numbers = numbers.split("");
for (let i = 1; i <= numbers.length; i++) {
let numberList = combination(numbers, i);
numberList = numberList.map((v) => v.join("") * "1");
numberList.forEach((v) => result.add(v));
}
return [...result].filter((v) => isDecimal(v)).length;
}
function combination(arr, selectNum) {
const result = [];
if (selectNum === 1) return arr.map((v) => [v]);
arr.forEach((v, idx, arr) => {
const fixed = v;
const restArr = arr.filter((v, index) => index !== idx);
const combinationArr = combination(restArr, selectNum - 1);
const combineFixed = combinationArr.map((v) => [fixed, ...v]);
result.push(...combineFixed);
});
return result;
}
function isDecimal(x) {
if (x < 2) return false;
//i 의 범위를 x의 절반으로 하면 시간 감축가능
for (let i = 2; i <= Math.sqrt(x); i++) {
if (x % i === 0) return false;
}
return true;
}
console.log(solution("110"));