문제
9020 골드바흐의 추측
답
kotlin code
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