[프로그래머스]소수 만들기(javascript,자바스크립트)

스카치·2023년 3월 8일
0

문제

풀이 1

function solution(nums) {
    var answer = 0;
    function primeCheck(num) {
        for (let i=2; i<=Math.sqrt(num); i++){
            if (num % i===0) return false;
        }
        return true;
    };
    
    function getCombinations(arr, selectNum) {
        const results = [];
        if (selectNum === 1) return arr.map(v => [v]);
        
        arr.forEach( (fixed, index, origin ) => {
            const rest = origin.slice(index+1);
            const combinations = getCombinations(rest, selectNum-1);
            const attached = combinations.map( (combination) => [fixed, ...combination]);
            results.push(...attached)
        
        });
        return results;
    };
    
    getCombinations(nums,3)
        .map( v => v.reduce((a,b) => a+b))
        .map(v => {
        if(primeCheck(v)) return answer++
        });
   
    
    return answer;
}

0개의 댓글