Kotlin 88分TLE求助

P1217 [USACO1.5] 回文质数 Prime Palindromes

AritxOnly_ @ 2024-08-11 13:30:47

import kotlin.math.sqrt
import java.util.Scanner

fun JudgePrime(x: Int): Boolean {
    if(x < 2)
        return false
    if(x == 2 || x == 3)
        return true
    if(x % 6 != 1 && x % 6 != 5)
        return false
    for(i in 3..sqrt(x.toDouble()).toInt() + 1 step 2) {
        if(x % i == 0)
            return false
    }
    return true
}

fun JudgeReverse(x: Int): Boolean {
    val str: String = x.toString()
    val rev: String = str.reversed()
    if(str == rev)
        return true
    else
        return false
}

fun main() {
    val read = Scanner(System.`in`)
    val lower = read.nextInt()
    val upper = read.nextInt()
    // 读入
    var cnt: Int = 0
    for(i in lower..upper) {
        if(JudgeReverse(i))
            if(JudgePrime(i))
                println(i)
    }
}

|