Generalised or n-ary Gray codes use more numbers, see http://en.wikipedia.org/wiki/Gray_code#n-ary_Gray_code
gGC=: 4 : 0 NB. (generalised) x-dim y-ary Graycode z=:i.y z ,/@:([,."_1 [EMAIL PROTECTED] $(,:|.)@])^:(<:x) [,.z ) 2 3 <@gGC"0 [4 3 +---+-----+ |0 0|0 0 0| |0 1|0 0 1| |0 2|0 0 2| |0 3|0 1 2| |1 3|0 1 1| |1 2|0 1 0| |1 1|0 2 0| |1 0|0 2 1| |2 0|0 2 2| |2 1|1 2 2| |2 2|1 2 1| |2 3|1 2 0| |3 3|1 1 0| |3 2|1 1 1| |3 1|1 1 2| |3 0|1 0 2| | |1 0 1| | |1 0 0| | |2 0 0| | |2 0 1| | |2 0 2| | |2 1 2| | |2 1 1| | |2 1 0| | |2 2 0| | |2 2 1| | |2 2 2| +---+-----+ R.E. Boss -----Oorspronkelijk bericht----- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Dan Bron Verzonden: dinsdag 5 december 2006 16:41 Aan: [email protected] Onderwerp: [Jprogramming] Gray codes Given positive integer n , generate the first 2^n Gray codes. Wikipedia defines Gray codes as a binary numeral system where two successive values differ in only one digit: G 3 0 0 0 0 0 1 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 1 0 0 The format of your result may either be a table of booleans or a list of integers (which formats can be readily interchanged with #: and #. ). #. G 3 0 1 3 2 6 7 5 4 Though the usual metrics of parisimony apply (brief, fast, lean), the larger goal here is to generate as many __different__ interesting solutions in J as possible. I chose this problem because I think it's particularly amenable to that goal. (Though I have to admit that while I've submitted 5 solutions, they only represent 3 different ways of attacking the problem; the last two were submitted only out of pride.) See http://www.jsoftware.com/jwiki/Puzzles/Gray_Code for the full rules (and spoilers down the page). -Dan ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
