// gray code if a is word #i in the gray code, then a^(((i^(i+1))+1)>>1) is #(i+1), i>=0 // subset iteration (from lukas); k is an int for(int i = k; ; i = (i-1)&k) { //A(i) if(!i) break; } //here, A(i) will be run once for each subset of k //given a prime int p, and an allocated uninitialized int array i[p] i[1] = 1; for(int x = 2; x < p; ++x) i[x] = (p - i[p%x] * (p/x)) % p; /* This computes the inverse of x mod p for _all_ x without any scary divisions, in roughly the same time it would take to compute any particular inverse. So, after the computation we have (x * i[x]) % p == 1 for each x in {1, 2, ..., p-1}. This code uses both the p%x and p/x at the same time, so could be slightly optimized by using some (which I forget) function deep in the C library which returns both for the cost of one. */