기본 콘텐츠로 건너뛰기

프로그래머스 - 소수 찾기

label이 break, continue와 사용가능해서 좋은 자바:)
좀 더 좋은 소수찾는 방법으로 풀어봐야지 한번 더 시도해봐야지
public class NumOfPrime {
    int numberOfPrime(int n) {
        int result = 0;
        loop1: 
        for(int i=2;i<=n;i++) {

            for(int j=2;j<i;j++) {
                if(i%j==0) {
                    continue loop1;
                }
            }
        result++;   
        }
        // 함수를 완성하세요.

        return result;
    }

    public static void main(String[] args) {
        NumOfPrime prime = new NumOfPrime();
        System.out.println(prime.numberOfPrime(10));
    }

}

댓글

이 블로그의 인기 게시물

프로그래머스 - 시저 암호

public class Caesar { String caesar ( String s , int n ) { n %= 26 ; String result = "" ; char [] temp = s . toCharArray (); for ( int i = 0 ; i < temp . length ; i ++) { if ( temp [ i ]>= 'a' && temp [ i ]<= 'z' ) temp [ i ] = ( char ) (( temp [ i ]+ n )> 'z' ? ( temp [ i ]+ n - 1 )% 'z' + 'a' : ( temp [ i ]+ n )); else if ( temp [ i ]>= 'A' && temp [ i ]<= 'Z' ) temp [ i ] = ( char ) (( temp [ i ]+ n )> 'Z' ? ( temp [ i ]+ n - 1 )% 'Z' + 'A' : ( temp [ i ]+ n )); result += temp [ i ]; } return result ; } public static void main ( String [] args ) { Caesar c = new Caesar (); System . out . println ( "s는 'a B z', n은 4인 경우: " + c . caesar ( ...