> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Prime Palindrome Python Solution with Tests

> Tested Python solution for LeetCode 866 with 30 pytest cases. Generate a practice environment with lcpy.

LeetCode 866, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Number Theory](/catalog/topics/number-theory), Primality Test. [View on LeetCode](https://leetcode.com/problems/prime-palindrome/description/).

Generate this problem as a practice environment: tested reference solution, 30 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 866   # by problem number
lcpy gen -s prime_palindrome   # by problem name
```

## Problem

Given an integer `n`, return *the smallest prime palindrome* greater than or equal to `n`.

An integer is **prime** if it has exactly two divisors: `1` and itself. Note that `1` is not a prime number.

* For example, `2`, `3`, `5`, `7`, `11`, and `13` are all primes.

An integer is a **palindrome** if it reads the same from left to right as it does from right to left.

* For example, `101` and `12321` are palindromes.

The test cases are generated so that the answer always exists and is in the range `[2, 2 * 10^8]`.

### Examples

```
Input: n = 6
Output: 7
```

```
Input: n = 8
Output: 11
```

```
Input: n = 13
Output: 101
```

### Constraints

* 1 \<= n \<= 10\<sup>8\</sup>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/prime_palindrome/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/prime_palindrome/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(sqrt(m) * log(m)) over the palindrome candidates up to the answer m
    # Space: O(1)
    def prime_palindrome(self, n: int) -> int:
        def is_prime(x: int) -> bool:
            if x < 2:
                return False
            if x % 2 == 0:
                return x == 2
            i = 3
            while i * i <= x:
                if x % i == 0:
                    return False
                i += 2
            return True

        for x in (2, 3, 5, 7, 11):
            if x >= n:
                return x
        # Every palindrome with an even number of digits is divisible by 11,
        # so 11 above is the only even-length prime palindrome. Walk the
        # odd-length ones by mirroring their first half.
        for length in (3, 5, 7, 9):
            half = 10 ** (length // 2)
            for root in range(half, half * 10):
                s = str(root)
                candidate = int(s + s[-2::-1])
                if candidate >= n and is_prime(candidate):
                    return candidate
        raise ValueError(f"no prime palindrome at or above {n}")
```

## Complexity

| Time | Space |
| - | - |
| O(sqrt(m) \* log(m)) over the palindrome candidates up to the answer m | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.