↓Skip to main content
  1. LeetCode/

LeetCode 3918: Sum of Primes Between Number and Its Reverse

·2 mins· ·
LeetCode Medium Math Number-Theory
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-05-03

  • Submissions: Wrong Answer → Accepted

Approach
#

With r the reverse of n, the range is [min(n, r), max(n, r)]. It is small, so trial-divide each number and sum the primes. (For larger ranges, a sieve computes them all at once.)

My Attempts
#

Attempt 1’s primality test:

  • tested divisibility with n // i == n / i, relying on floating point, and
  • tried range(2, ceil(√n) + 1), so for n = 2 it tried i = 2, where 2 // 2 == 2 / 2, judging 2 composite.

For n = 2, the reverse is still 2 and the answer is 2; it returned 0. Handling 2 first, trying only odd divisors, and using % passed.

Solution
#

from math import isqrt

class Solution:
    def sumOfPrimesInRange(self, n: int) -> int:
        def is_prime(x: int) -> bool:
            if x < 2:
                return False
            if x % 2 == 0:
                return x == 2
            for d in range(3, isqrt(x) + 1, 2):
                if x % d == 0:
                    return False
            return True

        r = int(str(n)[::-1])
        return sum(x for x in range(min(n, r), max(n, r) + 1) if is_prime(x))

Complexity
#

Time O(R √R) with R the range’s upper end; space O(1).

Mistakes to Avoid
#

  • Testing divisibility with floating-point division.
  • Using ceil(√n) as the limit lets small numbers try themselves; use isqrt and stop at √x.

Interview Explanation
#

Reversing n gives the range; trial division tests each number and the primes are summed. Trial division stops at √x, divisibility uses %, and 2 is handled first. For a bigger range, switch to a sieve.

Related

LeetCode 204: Count Primes
·2 mins
LeetCode Medium Array Math Enumeration Number-Theory
LeetCode 204 note: the sieve of Eratosthenes, crossing out multiples of each prime p starting at p², with the outer loop only up to √n
LeetCode 3618: Split Array by Prime Indices
·2 mins
LeetCode Medium Math Number-Theory Sieve Array
LeetCode 3618 note: primality is about the index; a sieve of Eratosthenes marks all of 0..n-1 at once, faster than trial division per index
LeetCode 3899: Angles of a Triangle
·2 mins
LeetCode Medium Math Geometry
LeetCode 3899 note: check the triangle inequality first, compute the three angles with the law of cosines, and remember to sort them
LeetCode 633: Sum of Square Numbers
·2 mins
LeetCode Medium Two-Pointers Math Binary-Search
LeetCode 633 note: converge a from 0 and b from isqrt(c); shrink b when the sum is too big and grow a when it is too small; watch for 0, a == b, and floating-point square roots
LeetCode 1401: Circle and Rectangle Overlapping
·2 mins
LeetCode Medium Math Geometry
LeetCode 1401 note: the rectangle’s nearest point to the center is the center clamped into the rectangle; they overlap when that point is within the radius
LeetCode 3824: Minimum K to Reduce Array Within Limit
·2 mins
LeetCode Medium Binary-Search Math
LeetCode 3824 note: binary search on k with a monotonic check; the upper bound must reach n because there are at least n operations