문제
소수찾기
문제풀이
const getPermutations = function (arr, selectNumber) {
const results = [];
if (selectNumber === 1) return arr.map((el) => [el]);
arr.forEach((fixed, index, origin) => {
const rest = [...origin.slice(0, index), ...origin.slice(index+1)]
const permutations = getPermutations(rest, selectNumber - 1);
const attached = permutations.map((el) => [fixed, ...el]);
results.push(...attached);
});
return results;
}
function isPrime(num) {
if(num === 2) {
return true;
}
for(let i = 2; i <= Math.floor(Math.sqrt(num)); i++){
if(num % i === 0){
return false;
}
}
return true;
}
function solution(numbers) {
var answer = 0;
var num = [... numbers];
var arr = [];
for (var l = 0; l < num.length; l++) {
var combNum = getPermutations(num, l + 1);
arr.push(...combNum);
}
var newArr = [];
for (var i = 0; i < arr.length; i++) {
var num = parseInt(arr[i].join(''));
if (num != '0' && num != '1') newArr.push(parseInt(num));
}
var set = new Set(newArr);
var setArr = [...set];
var cnt = 0;
for (var j = 0; j < setArr.length; j++) {
var sosu = isPrime (setArr[j]);
if (sosu == true) {
cnt++;
}
}
return cnt;
}