I found this problem on codeforces in an ACM archive.
Given a number $n$, find the number possible of ways of expressing that number as a sum of consecutive primes. Example :
Given $n = 41$:
$41 = 41 = 2 +5+3 +7 + 11 + 13 = 11 + 13 + 17$
As we can see, there are three ways of writing $41$ as a sum of consecutive primes, keeping in mind that since $41$ is a prime, a sum that consists of only $41$ is a valid sum as well.
Knowing that $n \leq 10000$, list the number of ways you can express the input numbers as sums of consecutive primes.
My effort regarding this problem consists of the following :
Given that the max $n $ is $10000 $, I populated a vector of primes using the sieve of eratosthenes (its size is precisely $1229$).
Following, I computed a vector of prefix sums, such that at index $p$ of that particular vector, we have the sum of all consecutive primes up to inclusively $p$ (non prime indexed entries are equal to $0$).
For example: $\text{prime_sums[2] = 2 }, \text{ prime_sums[3] =5 },\text{ prime_sums[5] = 10}$, etc.
Now, using this particular vector we could simply do traversals of the sum array for each of the input numbers and upon finding a way to write it as a sum (by comparing its value with that of each sum we encounter) , we would do multiple subtractions between elements of the sum array in order to find other possible ways to express our number as a sum of consecutive primes.
But this kills the speed of the program and feels really inelegant and awkward. Can you suggest alternative ways?