본문 바로가기

프로그래머스/lv0

합성수 찾기

/* 합성수 찾기
 * 약수의 개수가 세 개 이상인 수를 합성수라고 합니다.
 * 자연수 n이 매개변수로 주어질 때 n이하의 합성수의 개수를 return
 *
 * n    result
 * 10   5
 * 15   8
 */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class programmer_0_80 {
    static int a1 = 10
    static int a2 = 15;
 
    public int getCnt(int n){
        int cnt = 0;
        for(int i=1; i*<= n; i++){
            if(n%i == 0){
                cnt++;
                if(i*< n) cnt ++;
            }
        }
        return cnt;
    }
    public int solution(int n) {
        int answer = 0;
        for(int i = 1; i <= n; i++){
            if(getCnt(i) > 2)   answer++;
        }
        return answer;
    }
    public static void main(String args[]){
        programmer_0_80 t = new programmer_0_80();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
    } 
}
 
cs

'프로그래머스 > lv0' 카테고리의 다른 글

배열 회전시키기  (0) 2022.12.02
주사위의 갯수  (0) 2022.12.02
팩토리얼  (0) 2022.12.02
최댓값 만들기 (2)  (0) 2022.12.02
소인수분해  (0) 2022.12.02