How to use a 5 or 6 bit integer in Python?

Glen Wheeler adsl5lcq at tpg.com.au
Sat Dec 20 18:15:15 EST 2003


On 20 Dec 2003 18:35:31 GMT, bokr at oz.net (Bengt Richter) wrote:

>>
>Just to see if I understand your problem statement, does the following (though it's slow)
>do what you are trying to do? 

  Not exactly, although if you wc -l the result and then wrote down
the number, then it would.  I am only interested in the final result.

>If so, what would be the point of generating
>all those gray code sequences? IOW, how are you going to use them? Assuming my
>prog generated all the 6-bit grey code seqences to a monster file, all the 64-number
>sequences packed into 64-byte strings end to end, then you would either have to
>read that file sequentially, or seek to some point and read 64 bytes to get a
>given gray code sequence. If the latter, how about an algorithm that would
>do a virtual seek into the whole data as a virtual file, and then generate
>what you would read? I.e., there would be no point in storing all that data
>if you could easily generate an arbitrary segment of it. 

  Generating a single code of arbitrary digits is a relatively simple
task; I am interested in generating the total *number*, not every
code.  Each of the codes I am generating also has (as one of the
integers in it's data tuple) a `multiplicity' which is how many other
Gray codes of that length are symmetrically equivalent.
  I am merely generating a very small subset which I can recombine (as
a polynomial, say) and then work out how many there will be in total.

>Of course, maybe that's
>not so easy ;-) But I'm wonder what the use is going to be, and the access pattern,
>if you assumed availability in a monster file, virtual or not.
>

  Incidentally, I did try the method you have below.  Almost identical
I might add!  It ran for around 3 weeks, and generated just over a
million 5-digit Gray codes.  For some interest, if G(n) represents the
number of n-digit Gray codes then :

G(1) = 2
G(2) = 8 (as you show below)
G(3) = 144
G(4) = 91 392
G(5) = 187 499 658 240 (I can calculate this in under 8 hours)
G(6) = ?? (but it's around 10^24)

  So if you allocate each code a minimal number of bits, storing every
5 or 6 digit BGC is a very different and much more impossible task
than that which I am trying to achieve.

>If you really do want to generate all that data, I suppose the loop and recursions
>could be fanned out to parallel processing. But first, do I understand the problem?
>
>====< gray.py >=========================================================

  If instead of printing the successful code, or storing it, you
simply incremented an internal counter, then yes that is exactly the
problem I need to solve.  For 6 digits.
  My representations and algorithm come from alot of theory that me
and other associates have proven, although I can post it here or to
your e-mail if you would like.  I haven't included any of these
tuples-as-strings speedups yet, as I'm working on integrating some new
theory into the program, but comments would always be appreciated.
  It would be ironic if you did take me up on this, as I recently
deleted all the comments from my code because I felt they were
cluttering the code itself :).

-- 
Glen




More information about the Python-list mailing list