[백준/코틀린] 1978번: 소수 찾기
브론즈 2
링크
풀이
에라토스테네스의 체를 사용해 1000 이하의 모든 소수를 미리 구합니다.
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import kotlin.math.sqrt
fun main() {
val max = 1000
val isPrime = MutableList(max + 1) { it >= 2 }
val n = readln().toInt()
val nums = readln().split(" ").map { it.toInt() }
for (i in 2..sqrt(max.toDouble()).toInt()) {
for (j in i * 2..max step i) {
isPrime[j] = false
}
}
println(nums.count { isPrime[it] })
}
This post is licensed under CC BY 4.0 by the author.