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 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, 13

What makes a number a palindrome?
It reads the same from left to right as from right to left. Examples: 101, 121, 12321

Example: 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
๐Ÿ” Prime Palindrome Discovery Process๐Ÿ“š Brute ForceCheck every number:6โ†’7โ†’8โ†’9โ†’10โ†’11...โŒ Very slowMany non-palindromes๐ŸŽฏ Smart GenerationGenerate palindromes:10โ†’101, 11โ†’11112โ†’121, 13โ†’131...โœ… Much faster!โšก Prime CheckTest only palindromes:101: Prime โœ“111: 3ร—37 โœ—Found: 101!๐Ÿ’ก Key Insight: Palindrome Structure๐Ÿ”ธ 3-digit palindrome: ABA (A mirrors to both sides)๐Ÿ”ธ 4-digit palindrome: ABBA (AB mirrors to ABBA)๐Ÿ”ธ Generate from half: 123 โ†’ 12321 (odd) or 12 โ†’ 1221 (even)1Brute Force: O(nโˆšn) - Check every number2Optimized: O(10^(k/2)โˆšn) - Generate palindromes only
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!
Asked in
Google 12 Microsoft 8 Amazon 6 Meta 4
23.1K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen