[프로그래머스] [LV1] 소수 찾기

업데이트:

📚 소수 찾기

링크📎 : https://programmers.co.kr/learn/courses/30/lessons/12921

난이도 ⭐️

📖 문제

이미지

📝 내 풀이

import java.util.*;

class Solution {
    public int solution(int n) {
        int answer = 0;
        int[] list = new int[n+1];
        
        Arrays.fill(list, 1);
        
        list[0] = 0;
        list[1] = 0;
        
        for(int i = 2; i <= n; i++){
            if(list[i] == 0) continue;
            
            for(int j = 2; i * j <= n; j++)
                if(list[i*j] == 1) list[i*j] = 0;
        }
        
        for(int i : list)
            answer += i;
        
        return answer;
    }
}

끝-!

댓글남기기