Sum of Primes Between Number and Its Reverse - Video Solutions
weekly contest 500 | leetcode 3917 | leetcode 3918 | leetcode 3919 | leetcode 3920 | Easy solutions
Sum of Primes Between Number and Its Reverse - Video Solution
Watch 8 video solutions for Sum of Primes Between Number and Its Reverse, a medium level problem involving Math, Number Theory. This walkthrough by Code With Vick has 567 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Statement
You are given an integer n.
Let r be the integer formed by reversing the digits of n.
Return the sum of all prime numbers between min(n, r) and max(n, r), inclusive.
Example 1:
Input: n = 13
Output: 132
Explanation:
- The reverse of 13 is 31. Thus, the range is
[13, 31]. - The prime numbers in this range are 13, 17, 19, 23, 29, and 31.
- The sum of these prime numbers is
13 + 17 + 19 + 23 + 29 + 31 = 132.
Example 2:
Input: n = 10
Output: 17
Explanation:
- The reverse of 10 is 1. Thus, the range is
[1, 10]. - The prime numbers in this range are 2, 3, 5, and 7.
- The sum of these prime numbers is
2 + 3 + 5 + 7 = 17.
Example 3:
Input: n = 8
Output: 0
Explanation:
- The reverse of 8 is 8. Thus, the range is
[8, 8]. - There are no prime numbers in this range, so the sum is 0.
Constraints:
1 <= n <= 1000
Approach Overview
Problem Overview: You are given an integer n. Reverse its digits to get another number rev. The task is to compute the sum of all prime numbers in the range between min(n, rev) and max(n, rev). The main challenge is efficiently identifying primes across that range.
Approach 1: Brute Force Primality Check (O(k * sqrt(k)) time, O(1) space)
Compute the reversed value of n, then determine the interval [L, R] where L = min(n, rev) and R = max(n, rev). Iterate through every number in this range and run a primality test for each value. A typical check tries dividing the number by all integers from 2 to sqrt(x). If no divisor is found, add it to the running sum. This method is straightforward and works well when the interval is small, but repeated square‑root checks make it slow for larger ranges.
Approach 2: Precompute Using Sieve of Eratosthenes (O(R log log R) time, O(R) space)
Instead of checking each number independently, generate all primes up to R using the Sieve of Eratosthenes. Create a boolean array where each index represents whether the number is prime. Start from 2, mark multiples as composite, and continue until sqrt(R). Once the sieve is built, iterate from L to R and sum values whose sieve entry is still marked prime. This avoids repeated factor checks and dramatically improves performance when the range is large. The sieve approach is a classic technique from number theory and math problems involving repeated prime queries.
Approach 3: Segmented Sieve for Large Ranges (O((R-L+1) log log R) time, O(R-L+1) space)
If R can be extremely large but the interval size (R-L) is manageable, a segmented sieve becomes more memory efficient. First compute primes up to sqrt(R). Then create a local boolean array representing only the interval [L, R]. Use the previously computed primes to mark multiples within this segment. Remaining unmarked numbers are prime and can be summed directly. This method avoids allocating a full sieve up to R and is commonly used in competitive programming when dealing with big numeric ranges.
Recommended for interviews: The Sieve of Eratosthenes approach is typically the expected solution. Starting with the brute force method shows you understand primality testing, but moving to the sieve demonstrates stronger algorithmic thinking and familiarity with efficient prime generation techniques. It reduces repeated work and keeps the runtime near linear relative to the range size.
Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Primality Check | O(k * sqrt(k)) | O(1) | Small ranges where direct prime checks are cheap |
| Sieve of Eratosthenes | O(R log log R) | O(R) | General case when R is moderate and multiple primes must be checked |
| Segmented Sieve | O((R-L+1) log log R) | O(R-L+1) | Very large upper bounds where allocating a full sieve up to R is impractical |