In article <fab6nc$ni2$(E-Mail Removed)>

Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> wrote:

>*One* algorithm is:
[snipped]

>(As the technique is not immediately obvious, you should

>be crediting -someone- for the algorithm if you use it.)
I like to think of it as an odometer.

An ordinary odometer of N decimal digits increments thus:

/*

* Increment an "ndigits"-digit decimal odometer whose values

* are in odo[0] through odo[ndigits-1]. Return 0 on

* success, nonzero if the odometer has rolled over to

* all-zeros again.

*/

int odo_increment(int *odo, size_t ndigits) {

size_t i;

for (i = ndigits - 1;; i--) {

if (++odo[i] <= 9)

break;

odo[i] = 0;

if (i == 0)

return -1; /* overflow -- odometer is all 0s again */

}

return 0; /* success */

}

It is not too difficult to adapt this to "odometers" using something

other than "decimal digits". The key observation, of course, is

that we simply increment the least-significant "digit" until it

overflows, then reset it to zero and increment next-most significant

digit, and so on -- exactly like an old-style mechanical odometer.

--

In-Real-Life: Chris Torek, Wind River Systems

Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603

email: forget about it

http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.