You are given two integers n and m.
You have to select a multiset of prime numbers from the first m prime numbers such that the sum of the selected primes is exactly n. You may use each prime number multiple times.
Return the minimum number of prime numbers needed to sum up to n, or -1 if it is not possible.
Example 1:
Input: n = 10, m = 2
Output: 4
Explanation:
The first 2 primes are [2, 3]. The sum 10 can be formed as 2 + 2 + 3 + 3, requiring 4 primes.
Example 2:
Input: n = 15, m = 5
Output: 3
Explanation:
The first 5 primes are [2, 3, 5, 7, 11]. The sum 15 can be formed as 5 + 5 + 5, requiring 3 primes.
Example 3:
Input: n = 7, m = 6
Output: 1
Explanation:
The first 6 primes are [2, 3, 5, 7, 11, 13]. The sum 7 can be formed directly by prime 7, requiring only 1 prime.
Constraints:
1 <= n <= 10001 <= m <= 1000Problem Overview: You are given a target integer and need to determine the minimum number of prime numbers whose sum equals that target. Each prime can be used multiple times, and the goal is to minimize the count of primes used to reach the exact sum.
Approach 1: Brute Force Enumeration (Exponential Time, O(2^n) time, O(n) space)
The most direct idea is to try every combination of prime numbers that could sum to the target. First generate all primes up to the target using a simple primality test. Then recursively explore combinations where you add a prime and reduce the remaining target. Track the minimum number of primes used whenever the remaining value reaches zero. This approach behaves like exhaustive search and quickly becomes infeasible as the target grows because the recursion tree expands exponentially. It is useful conceptually because it reveals that the problem behaves like an unbounded combination problem similar to coin change.
Approach 2: Preprocessing + Dynamic Programming (O(n^2 / log n) time, O(n) space)
A practical solution treats each prime as a "coin" and the target as the amount to construct. Start by generating all prime numbers up to the target using the Sieve of Eratosthenes, a classic technique from number theory. This preprocessing step runs in roughly O(n log log n) time and produces the list of usable primes.
Next apply a bottom‑up dynamic programming strategy similar to the unbounded coin change problem. Create an array dp[i] representing the minimum number of primes required to form sum i. Initialize dp[0] = 0 and all other values to infinity. For every prime p, iterate through sums from p to the target and update dp[i] = min(dp[i], dp[i - p] + 1). Each update represents using prime p as the last element in the sum.
This method systematically builds answers for all smaller sums before computing the final target. The DP array ensures each state is computed once, turning the exponential brute force search into a polynomial-time solution. The algorithm mainly performs array updates and prime iterations, making it efficient even for relatively large targets. The logic relies heavily on patterns from dynamic programming and simple iteration over an array state table.
Recommended for interviews: The sieve + dynamic programming approach is what interviewers expect. Explaining the brute force recursion first demonstrates that you understand the combinational nature of the problem. Converting it into a coin-change style DP with precomputed primes shows the optimization step that interviewers look for. It also clearly communicates your understanding of state transitions and preprocessing techniques.
We can first preprocess to obtain the first 1000 prime numbers, and then use dynamic programming to solve the problem.
Define f[i] as the minimum number of primes needed to sum up to i. Initially, set f[0] = 0 and all other f[i] = infty. For each prime p, we can update f[i] from f[i - p] as follows:
$
f[i] = min(f[i], f[i - p] + 1)
If f[n] is still infty, it means it is impossible to obtain n as the sum of the first m primes, so return -1; otherwise, return f[n].
The time complexity is O(m times n), and the space complexity is O(n + M), where M is the number of preprocessed primes (here it is 1000$).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(2^n) | O(n) | Understanding the search space or validating small inputs |
| Sieve Preprocessing + Dynamic Programming | O(n^2 / log n) | O(n) | General solution for large targets where optimal performance is required |
Practice Minimum Number of Primes to Sum to Target with our built-in code editor and test cases.
Practice on FleetCode