👍🏻 2022년 2월 2일
<script>
const permutation = (arr, n) => {
let result = [];
if (n === 1) return arr.map((v) => [v]);
arr.forEach((v, idx, arr) => {
const fix = v;
const rest = arr.filter((_, index) => index !== idx);
const permuationArr = permutation(rest, n - 1);
const combineFix = permuationArr.map((v) => [fix, ...v]);
result.push(...combineFix);
});
return result;
}
const findDecimal = (num) => {
let cnt = 0;
if (num === 1) return false;
else if (num === 2) return true;
else if (num % 2 === 0) return false;
else {
for (let i=0; i<parseInt(num/2); i++) {
if (num % i === 0) cnt++
if (cnt === 2) return false;
}
}
return true;
}
function solution(numbers) {
var answer = 0;
numbers = numbers.split("");
let uniquePer = [];
for (let i=1; i<=numbers.length; i++) {
let per = permutation(numbers, i);
per.forEach((v,i) => {
let num = Number(v.join(""))
if (uniquePer.indexOf(num) === -1) {
uniquePer.push(num);
if (findDecimal(num)) {
answer++;
}
}
})
}
return answer;
}
</script>