Math question about my alarm system

Heh, I got curious and wrote some code to do it too. I use a pretty straightforward (read: not clever) algorithm, and it works. You don’t even have to try to order the numbers in a clever way. My algorithm is:

For each number in question:

  1. is the number already in the sequence? if so, skip it
  2. if the number in question “overlaps” with the beginning or end of the sequence, prepend or append the part that isn’t overlapping and move on to the next number
  3. otherwise, just append the number to the sequence

My code is at http://www.codsquad.com/~galt/codes.c and the output is at http://www.codsquad.com/~galt/codesout.txt (with linebreaks so you can read the number)

To see if I can see a pattern (and I definitely can, but it’s very complex) in how to construct this number (as opposed to the brute-force method my C program does), I had the brute force method output the action it was taking for each number in turn. The output is at http://www.codsquad.com/~galt/codesout2.html
So for example, the output:

8998: 0
8999: +2
9000: -1
9001: 0

means that for “8988” it did nothing because that number was already in the sequence. “8999” had its last 2 digits stuck on the end of the sequence, “9000” had its first 1 digit stuck on the beginning of the sequence, and “9001” was already in the sequence, so nothing was added.

As you scroll through, you can see some definite patterns (like, I can abstract out what happens from 0 to 1000 pretty easily, but after that it gets vague). Anyone out there know how one goes about analyzing this and coming up with a general formula?