합성수 찾기

반즈·2023년 12월 15일

프로그래머스 입문

목록 보기
45/51

문제 설명

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

입출력 예


자바

나의 풀이

class Solution {
    public int solution(int n) {
        int answer = 0;
        int num = 0;
        for(int i = 4; i <= n; i++){
            for(int j = 1; j <= i; j++){
                if(i % j == 0){
                    num++;
                }
            }
            if(num >= 3){
                answer++;
            }
            num = 0;
        }
        return answer;
    }
}

자바스크립트

나의 풀이

function solution(n) {
    let answer = 0;
    let num = 0;
    for(let i = 4; i <= n; i++){
        for(let j = 1; j <= i; j++){
            if(i % j == 0){
                num++;
            }
        }
        if(num >= 3){
            answer++;
        }
        num = 0;
    }
    return answer;
}
profile
나를 채우다

0개의 댓글