#Robert Dougherty-Bliss, hw3 # OK to post. read "C3.txt": # 1. NewUCG := proc(n) local w, res: w := [0$n]: res := []: while w <> FAIL do res := [op(res), w]: w := NEXG(w): od: res: end: # 2. BoxG := proc(L) local prev: local res: if nops(L) = 0 then return [[]]: fi: # `prev` is a walk along the first n - 1 dimensions. prev := BoxG(L[2..]): # Walk through normally with the last component 0, then reversed with the # last component 1, then normally with the last component 2, and so on # until you've used all components. res := []: for k from 0 to L[1] do newSegment := [seq([k, op(p)], p in prev)]: if k mod 2 = 1 then newSegment := ListTools[Reverse](newSegment): fi: res := [op(res), op(newSegment)]: od: res: end: # 3. # I would write this, but I would rather go outside and play in the snow! # 4. # My objection to this problem is similar to the last one... # Here's how we could start to work this out. # 1. Just go read what Wilf et al. say about this. # 2. Using empirical data, guess the index that changes at step n. # 3. Unroll the recursion in NEXG to find a nice description of what it's # doing. # Doing any of these suggests that you can't *literally* write a non-recursive # NEXG(w) in a way that is independent of w. That is, any non-recursive NEXG(w) # must change itself every time it is called to remember, at a minimum, the # parity of the index it was on. # Anyway, I'll just leave us with the following fun snippet. # Return the sequence of indices which change from step to step in the # n-dimensional Gray code. (Must read-in C3.txt) DeltaSeq := proc(n) local code, current, future, res: code := UCG(n): res := []: for k from 1 to nops(code) - 1 do current := code[k]: future := code[k + 1]: for j from 1 to n do if current[j] <> future[j] then res := [op(res), j]: break: fi: od: od: res: end: