Algorithm/백준
[백준] 1978번 소수 찾기
sirius
2021. 2. 25. 10:13
https://www.acmicpc.net/problem/1978
1978번: 소수 찾기
첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다.
www.acmicpc.net
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
int ans = 0;
for(int i=0; i < count; i++) {
boolean isPrime = true;
int num = sc.nextInt();
// 1인경우 넘어감
if(num == 1) {
continue;
}
// 소수니까 2부터 판별
// 제곱근 이하까지만 계산하면 됨
for(int j = 2; j <= Math.sqrt(num); j++) {
// 나눠지는 수가 있으면 소수가 아님
if(num % j == 0) {
isPrime = false;
break;
}
}
// 소수
if(isPrime) {
ans++;
}
}
System.out.println(ans);
}
}