Python code summarizing several posts
Below code summarized several posts\' algorithms: SolaAoi, EddieCarrillo, ivschukin.\n\nThe idea is we handle the case where return value is less than 100. \n\n
Below code summarized several posts\' algorithms: SolaAoi, EddieCarrillo, ivschukin.\n\nThe idea is we handle the case where return value is less than 100. \n\nThen all the rest of the cases, we only need to consider palindromes with lengths larger than 2.\n\nFrom SolaAoi\'s post, we only need to generate odd-length palindromes.\n\nFrom EddieCarrillo\'s, we can ensure the generated palindromes are sorted.\n\nAs we know the palindromes cannot end with even digits and \'5\', so we can only pick the seeds starting with \'1\',\'3\',\'7\',\'9\'.\n\nSince all palindromes ends with odd numbers, we only need to consider odd numbers starting from 3 to check if prime.\n\n```\nclass Solution(object):\n    def primePalindrome(self, N):\n        """\n        :type N: int\n        :rtype: int\n        """\n        # Create a palindrome, then check prime\n        \n        # First digit and (last) can only be [1, 3, 7, 9]\n        # Even-length palindromes (length longer than 2) are divisible by 11\n        \n        # Create a list of prime numbers\n\n        \n        # Handle the case that returned value is less than 100\n        \n        if N >= 9989900:\n            return 100030001\n        if len(str(N)) <= 2:\n            for c in [2,3,5,7,11]:\n                if c >= N:\n                    return c\n        \n                \n        def isPrime(n):\n            i = 3\n            while i*i <= n:\n                if n % i == 0:\n                    return False\n                i += 2\n            return True\n        \n        # Generate odd-length (length > 2) palindrome\n        def genOdd(seedint):\n            seed = str(seedint)\n            return int(seed + seed[:len(seed)-1][::-1])\n        \n        # Starting with length > 2 palindrome\n        seed = 10\n        \n        while True:\n            if str(seed)[0] in [\'1\',\'3\',\'7\',\'9\']:\n                cur = genOdd(seed)\n                if cur >= N and isPrime(cur):\n                    return cur\n            seed += 1\n
[]