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
pythonn = int(input()) if n == 1: print(2) exit() def is_prime(num): if num < 2: return False if num == 2: return True if num % 2 == 0: return False for i in range(3, int(num**0.5) + 1, 2): if num % i == 0: return False return True def generate_palindromes(length): palindromes = [] half = length // 2 if length % 2 == 0: start = 10**(half - 1) end = 10**half - 1 for x in range(start, end + 1): s = str(x) palindrome = s + s[::-1] palindromes.append(int(palindrome)) else: start = 10**(half) end = 10**(half + 1) - 1 for x in range(start, end + 1): s = str(x) palindrome = s + s[:-1][::-1] palindromes.append(int(palindrome)) return palindromes original_length = len(str(n)) # The upper limit for the loop is arbitrary but set to cover possible cases for l in range(original_length, 10): pals = generate_palindromes(l) for m in pals: if m < n: continue if is_prime(m): print(m) exit()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.