[백준(Baekjoon)][자바(java)] (1978) 소수 찾기 / 수학 2

728x90

 

 

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);

		int i, j, s, cnt = 0;
		int n = sc.nextInt();
		int a[] = new int[n];
		
		for( i = 0; i < n; i++ )
			a[i] = sc.nextInt();
		
		loop:
		for( i = 0; i < n; i++ ) {
			if( a[i] < 2 ) continue;
			s = (int)Math.sqrt( a[i] );
			for( j = 2; j <= s; j++ )
				if( a[i] % j == 0 )
					continue loop;
			cnt++;
		}
		
		System.out.println(cnt);
		
		sc.close();
	}
}

 

소수 : 자신보다 작은 두 개의 자연수를 곱하여 만들 수 없는 1보다 큰 자연수

       ( 약수가 1과 자기 자신뿐인 1보다 큰 자연수 )

 

 

반응형