★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-oogsvmvc-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Find the smallest prime palindrome greater than or equal to N
.git
Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1. github
For example, 2,3,5,7,11 and 13 are primes.微信
Recall that a number is a palindrome if it reads the same from left to right as it does from right to left. less
For example, 12321 is a palindrome. spa
Example 1:code
Input: 6
Output: 7
Example 2:orm
Input: 8
Output: 11
Example 3:htm
Input: 13
Output: 101
Note:blog
1 <= N <= 10^8
2 * 10^8
.求出大於或等於 N
的最小回文素數。
回顧一下,若是一個數大於 1,且其因數只有 1 和它自身,那麼這個數是素數。
例如,2,3,5,7,11 以及 13 是素數。
回顧一下,若是一個數從左往右讀與從右往左讀是同樣的,那麼這個數是迴文數。
例如,12321 是迴文數。
示例 1:
輸入:6 輸出:7
示例 2:
輸入:8 輸出:11
示例 3:
輸入:13 輸出:101
提示:
1 <= N <= 10^8
2 * 10^8
。1 class Solution { 2 func primePalindrome(_ N: Int) -> Int { 3 if N < 10{ 4 let resDic = [2,2,2,3,5,5,7,7,11,11] 5 return resDic[N] 6 } 7 let s = String(N) 8 var half = Int(s.prefix(s.count/2)) ?? 1 9 var hasMidHandle = s.count % 2 == 1 10 11 while 1==1 { 12 let maxHalf = maxHalf9(half) 13 if !hasMidHandle { 14 for h in half...maxHalf{ 15 let tail = String(String(h).reversed()) 16 let num = Int(String(h)+tail) ?? 1 17 if num < N { continue } 18 if isPrimer(num){ 19 return num 20 } 21 } 22 hasMidHandle = true 23 half = (maxHalf + 1) / 10 24 } 25 for h in half...maxHalf{ 26 let tail = String(String(h).reversed()) 27 for m in 0...9{ 28 let num = Int(String(format: "%d%d",h,m)+tail) ?? 1 29 if num < N { continue } 30 if isPrimer(num){ 31 return num 32 } 33 } 34 } 35 hasMidHandle = false 36 half = maxHalf + 1 37 } 38 } 39 40 // 123 -> 999 41 func maxHalf9(_ num:Int) -> Int { 42 var n = 1 43 for _ in 0..<String(num).count{ 44 n *= 10 45 } 46 return n-1 47 } 48 49 func isPrimer(_ num:Int) -> Bool{ 50 if num % 2 == 0 { return false } 51 let top = Int(sqrt(Double(num))) 52 var i = 3 53 while i <= top{ 54 if num % i == 0{ return false } 55 i += 2 56 } 57 return true 58 } 59 }
1 class Solution { 2 func primePalindrome(_ N: Int) -> Int { 3 if 8 <= N && N <= 11 4 { 5 return 11 6 } 7 for x in 1..<100000 8 { 9 var s:String = String(x) 10 var r:String = String(s.reversed()) 11 var y:Int = Int(s + r.subString(1)) ?? 0 12 if y >= N && isPrime(y) {return y} 13 } 14 return -1 15 } 16 17 func isPrime(_ x:Int) -> Bool 18 { 19 if x < 2 || x % 2 == 0 {return x == 2} 20 var i:Int = 3 21 while(i * i <= x) 22 { 23 if x % i == 0 24 { 25 return false 26 } 27 i += 2 28 } 29 return true 30 } 31 } 32 33 extension String { 34 // 截取字符串:從index到結束處 35 // - Parameter index: 開始索引 36 // - Returns: 子字符串 37 func subString(_ index: Int) -> String { 38 let theIndex = self.index(self.endIndex, offsetBy: index - self.count) 39 return String(self[theIndex..<endIndex]) 40 } 41 }