[프로그래머스, 12977] 소수 만들기 (java)
Problem Solving/Programmers

[프로그래머스, 12977] 소수 만들기 (java)

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/12977

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


메모리: 92.2 MB, 시간: 1.48 ms

사용 알고리즘: 구현

class Solution {
    public int solution(int[] nums) {
        
        // 1 이상 3,000 이하의 수 중, 소수인 것들 구하기
        boolean[] isNotPrimeNumber = new boolean[3_001];
        isNotPrimeNumber[1] = true;
        
        for(int i = 2; i * i < 3_001; i++) {
            if(!isNotPrimeNumber[i]) {
                for(int j = i * i; j < 3_001; j += i) {
                    isNotPrimeNumber[j] = true;
                }
            }
        }
        
        
        int answer = 0;

        // nums에서 3개를 골랐을 때 소수이면 answer + 1
        int sum;
        for(int i = 0; i < nums.length - 2; i++) {
            for(int j = i + 1; j < nums.length - 1; j++) {
                for(int l = j + 1; l < nums.length; l++) {
                    sum = nums[i] + nums[j] + nums[l];
                    if(!isNotPrimeNumber[sum]) answer++;
                }
            }
        }

        return answer;
    }
}
728x90