An integer is said to be a palindrome if it is equal to its reverse. For example, 79197…
- User
An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes. In this task you will be given an integer N, 1 ≤ N ≤ 1000000. You must find the smallest integer M ≥ N such that M is a prime number and M is a palindrome.
For example, if N is 31 then the answer is 101. Input A single integer N, (1 ≤ N ≤ 1000000), on a single line.Output Your output must consist of a single integer, the smallest prime palindrome greater than or equal to N.
Example
Input: 31 Output: 101
Assistant
pythondef is_prime(n): if n < 2: return False if n == 2: return True if n % 2 == 0: return False max_div = int(n**0.5) + 1 for i in range(3, max_div, 2): if n % i == 0: return False return True N = int(input()) length = len(str(N)) while True: even = (length % 2 == 0) if even: k = length // 2 start = 10 ** (k - 1) end = 10 ** k - 1 else: k = (length - 1) // 2 start = 10 ** k end = 10 ** (k + 1) - 1 for prefix in range(start, end + 1): s = str(prefix) if even: pal_str = s + s[::-1] else: pal_str = s + s[:-1][::-1] M = int(pal_str) if M < N: continue if is_prime(M): print(M) exit() length += 1
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.