Prime Palindrome - Problem
Find the Prime Palindrome
Your mission is to find the smallest number that satisfies both special properties: being a prime number and reading the same forwards and backwards (a palindrome).
Given an integer
What makes a number prime?
A prime number has exactly two divisors: 1 and itself. Examples:
What makes a number a palindrome?
It reads the same from left to right as from right to left. Examples:
Example: If
If
Your mission is to find the smallest number that satisfies both special properties: being a prime number and reading the same forwards and backwards (a palindrome).
Given an integer
n, return the smallest prime palindrome that is greater than or equal to n.What makes a number prime?
A prime number has exactly two divisors: 1 and itself. Examples:
2, 3, 5, 7, 11, 13What makes a number a palindrome?
It reads the same from left to right as from right to left. Examples:
101, 121, 12321Example: If
n = 6, the answer is 7 because 7 is both prime and a palindrome (single digits are palindromes).If
n = 8, the answer is 11 because it's the next number that's both prime and palindromic. Input & Output
example_1.py โ Basic Case
$
Input:
n = 6
โบ
Output:
7
๐ก Note:
Starting from 6: 6 is a palindrome but not prime (divisible by 1, 2, 3, 6). 7 is both a palindrome (single digit) and prime, so we return 7.
example_2.py โ Skip Non-Primes
$
Input:
n = 8
โบ
Output:
11
๐ก Note:
8 is a palindrome but not prime (even number). 9 is a palindrome but not prime (3ร3). 10 is not a palindrome. 11 is both palindrome and prime.
example_3.py โ Larger Number
$
Input:
n = 987654321
โบ
Output:
1003001001
๐ก Note:
For very large inputs, we need to find palindromes with more digits. 1003001001 is the next prime palindrome after 987654321.
Constraints
- 1 โค n โค 2 ร 108
- The answer always exists and is within the given range
- n is a positive integer
Visualization
Tap to expand
Understanding the Visualization
1
Smart Generation
Instead of checking every book, create palindromes by writing the first half and mirroring it
2
Prime Testing
For each palindrome generated, test if the catalog number is prime
3
Length Progression
If no palindrome of current length works, move to next length systematically
Key Takeaway
๐ฏ Key Insight: Instead of checking every number sequentially, generate palindromes systematically by mirroring their first half. This reduces the search space from potentially millions of numbers to just a few hundred palindromes per digit length!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code