백준 - 9020 골드바흐의 추측
문제
9020 골드바흐의 추측
답
kotlin code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fun main() {
q9020()
}
fun q9020() {
val inputs = IntArray(readln().toInt()) { readln().toInt() }
for(input in inputs) {
for(n1 in input/2 downTo 2) {
val n2 = input - n1
if((n1 == n2 && n1.isPrime()) || (n1.isPrime() && n2.isPrime())) {
println("$n1 $n2")
break
}
}
}
}
private fun Int.isPrime(): Boolean = when {
this == 2 -> true
this < 2 || this.isOdd() -> false
else -> {
var result = true
for (i in 3..this/2) {
if (this % i == 0) {
result = false
break
}
}
result
}
}
private fun Int.isOdd() = this % 2 == 0
This post is licensed under CC BY 4.0 by the author.