This is one tough medium problem and I had to look at the official solution to solve this. I can solve for smaller numbers but once it gets to 7+ digits then I always get the Time Limit Exceeded result.
class Solution {
fun isPrime(num: Int): Boolean {
if(num < 2) return false
return !(2..Math.sqrt(num.toDouble()).toInt()).any { num % it == 0 }
}
fun reverse(num: Int): Int {
var a = 0
var n = num
while(n > 0){
a = 10 * a + n % 10
n = n / 10
}
return a
}
fun primePalindrome(N: Int): Int {
var n = N
while(true){
if(n == reverse(n) && isPrime(n)) return n
n++
if(n > 10000000 && n < 100000000){
n = 100000000
}
}
return 0
}
}
Runtime: 160 ms, faster than 100.00% of Kotlin online submissions for Prime Palindrome.
Memory Usage: 31.3 MB, less than 100.00% of Kotlin online submissions for Prime Palindrome.