The title is slightly misleading: What I am trying to is a little bit more complicated, but I don't know how to describe it in a title.
I have two sequences (vectors, arrays, lists) p and q of numbers. I sum all of the numbers in p except the first element, then perform a test. If the fails, I replace the first element of p with the first element of q, and start over with the second element: Sum all elements of p except the second one (including the new element copied from q). If the test fails the second time, replace the second element in p with the second element in q, and do it again with the third element. The test always succeeds on some element. Pseudocode:
i = 0 if test(sum_except p i) then exit loop else p[i] := q[i], and run again with i+1 This means that if I have 1000 elements, I sum 999 of them, and then the next time through I sum 998 of those plus a new element, and so on. As the process goes on, I am also summing more and more elements from q rather than from the original p, but I will have summed most of them, previously, as well. So at each step, I'm summing a bunch of numbers that I had already added together. This seems inefficient, but I have not figured out a more sensible way to write this.
In case it matters, what the test does is check whether the sum of the values in p except the element at i, plus the value of q at i, is greater or equal to than 1 (or is less than or equal to 1--there are two variants). i.e.:
(sum_except p i) + q[i] > 1 This all happens inside an inner loop, and the sequence length will probably be between 500 and 5000, although larger numbers are possible.
(My code actually is in OCaml, fwiw.)