-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm21.js
More file actions
33 lines (30 loc) · 1.21 KB
/
algorithm21.js
File metadata and controls
33 lines (30 loc) · 1.21 KB
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
let getCombinations = function (arr, selectNumber) {
const results = [];
if (selectNumber === 1) return arr.map((value) => [value]); // 1개씩 택할 때, 바로 모든 배열의 원소 return
arr.forEach((fixed, index, origin) => {
const rest = origin.slice(index + 1); // 해당하는 fixed를 제외한 나머지 뒤
const combinations = getCombinations(rest, selectNumber - 1); // 나머지에 대해서 조합을 구한다.
const attached = combinations.map((combination) => [fixed, ...combination]); // 돌아온 조합에 떼 놓은(fixed) 값 붙이기
results.push(...attached); // 배열 spread syntax 로 모두다 push
})
return results
}
function isPrime(number){
let test_number = Math.floor(Math.sqrt(number))
for(let i=2; i<=test_number; i++){
if((number % i) === 0){
return false
}
}
return true
}
function solution(nums) {
var answer = 0;
let comb = getCombinations(nums, 3).map((value) => {return value.reduce((sum, currValue) => {return sum + currValue}, 0)})
for(let i=0; i<comb.length; i++){
if(isPrime(comb[i]) == true){
answer += 1
}
}
return answer;
}