• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects

백준 - 9020 골드바흐의 추측

06 Jun 2022

Reading time ~1 minute

문제

9020 골드바흐의 추측

screencapture

답

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


baekjoonkotlin코틀린백준실버수학정수론소수소수 판정에라토스테네스의 체 Share Tweet +1