Will this generated sequence go backwards?

37 Views Asked by At

This is a followup to my previous query. If you look at the Wikipedia page for the Sieve of Sundaram, the general formula is:

$$i, j \in \mathbb{N}; 1 \le i \le j$$ $$\{\forall x | x = i + j + 2ij\}$$

(Don't know if I got that formula right.) I decided to use a priority queue to store each result. I insert the first result, 4, along with its generators, i = 1 and j = 1. Then I iterate by:

$$i \to i + 1 :: i+j+2ij \to (i+j+2ij) + (2j + 1)$$

with a similar transformation for j since the formula is symmetric for i and j. I do two iterations per cycle, one for i and one for j.

So for the first result, I return 4 and generate 7 with |1, 2|. The next result I return 7 and generate 10 with |1, 3| and 12 with |2, 2|. The queue has two entries, so I pick the lowest, 10, for the third cycle, then generate 13 with |1, 4| and 17 with |2, 3|. Next is 12 with generation of 17 with |2, 3|. And so on. Note that 17 got inserted twice with the same |i, j| pair, so when I get to it, I'll remove the duplicate and iterate only once. Also note that some numbers, like 22, are duplicated with distinct |i, j| pairs, |2, 4| and |1, 7| for 22; for these I remove all duplicates but iterate per distinct pair. (Here, I iterate once for 17, to 24 and 22, and twice for 22, to 31 and 27 then 37 and 25.)

If you read the previous query, and looked at the doubly-infinite array of values in the article, you notice that later rows/columns move faster than the earlier ones. I'm worried that slower row will generate a number I already went past from a faster row, which would mess my generation scheme. I don't feel it would happen, and it didn't happen when I did the first few rows by hand, nor did it happen when I ran my program to 500 entries. But my intuition isn't enough, so I'm asking if it can be proven that a slower row will never encounter a number I already covered from a faster one (or provide a counter example). I'm guessing some sort of induction, since we know it does work for lower values.