본문 바로가기
프로그래머스/[프로그래머스 - JAVA] Lv.0

[프로그래머스 - JAVA] 합성수 찾기

by 코딩하는 흰둥이 2023. 3. 11.
반응형

 

  • 내 풀이
class Solution {
    public int solution(int n) {
        int answer = 0;
        int count = 0;
        
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                if (i % j == 0){
                    count++;
                }
            }
            if (count >= 3){
                answer++;
                count = 0;
            }else {
                count = 0;
            }
        }
        
        return answer;
    }
}

해당 n값 만큼 for문을 돌려서 나머지가 딱 떨어지는 3개 이상의 값을 구해서 answer에 넣어주도록 했다.

합성수....이런건 우리 안찾아도 되지않을까?...

 

 

  • 다른 사람 풀이
import java.util.stream.IntStream;

class Solution {
    public int solution(int n) {
        return (int) IntStream.rangeClosed(1, n).filter(i -> (int) IntStream.rangeClosed(1, i).filter(i2 -> i % i2 == 0).count() > 2).count();
    }
}




class Solution {
    public int solution(int n) {
        int answer = 0;

        for (int i = 1; i <= n; i++) {
            int cnt = 0;
            for (int j = 1; j <= i; j++) {
                if (i % j == 0) cnt++;
            }
            if (cnt >= 3) answer++;
        }

        return answer;
    }
}

댓글