[Programmers] 코딩테스트 입문 120846. 합성수 찾기

이지현·2023년 2월 16일
0

Algorithm

목록 보기
28/81
post-thumbnail

✔️ Problem URL

합성수 찾기


✔️ Problem

약수의 개수가 세 개 이상인 수를 합성수라고 합니다. 자연수 n이 매개변수로 주어질 때 n이하의 합성수의 개수를 return하도록 solution 함수를 완성해주세요.


✔️ Code

class Solution {
    public int solution(int n) {
        int answer = 0;
        
        for(int i = 4; i <= n; i++) {
            if(isDivisor(i)) answer++;
        }
        return answer;
    }
    
    public boolean isDivisor(int number) {
        int cnt = 0;
        
        for(int i = 1; i <= number; i++) {
            if(number % i == 0) cnt++;
        }
        return cnt >= 3;
    }
}
profile
2023.09 ~ 티스토리 이전 / 2024.04 ~ 깃허브 블로그 이전

0개의 댓글