https://programmers.co.kr/learn/courses/30/lessons/42839#
Input | Output |
---|---|
"17" | 3 |
"011" | 2 |
function solution(numbers) {
var answer = 0;
const ch = Array.from({length:numbers.length}, ()=>0);
let temp = [];
const myArr = [];
const DFS = (L) => {
if(L === numbers.length){
temp = [];
}
for(let i = 0; i < numbers.length; i++) {
if(ch[i] === 0) {
ch[i] = 1;
temp.push(numbers[i]);
myArr.push(Number(temp.join("")));
DFS(L+1);
ch[i] = 0;
}
}
}
DFS(0);
const mySet = new Set(myArr);
const nums = (Array.from(mySet));
nums.forEach((num) => {
if(isPrime(num)) answer++;
})
return answer;
}
const isPrime = (num) => {
if (num === 0 || num === 1 || num === 2) return false;
for(let i = 2; i < Math.floor(Math.sqrt(num)); i++) {
if(num % i === 0) return false;
}
return true;
}