A105530 Ternary modular Gray code for n.
0, 1, 2, 5, 3, 4, 7, 8, 6, 15, 16, 17, 11, 9, 10, 13, 14, 12, 21, 22, 23, 26, 24, 25, 19, 20, 18, 45, 46, 47, 50, 48, 49, 52, 53, 51, 33, 34, 35, 29, 27, 28, 31, 32, 30, 39, 40, 41, 44, 42, 43
Offset: 0
Keywords
Links
- Martin Cohn, Affine m-ary Gray Codes, Information and Control, volume 6, 1963, pages 70-78. (For the case m=3, U = P = identity matrix, v = 0 vector.)
- Donald E. Knuth, The Art of Computer Programming, Pre-Fascicle 2A, Draft of Section 7.2.1.1. See subsection "Nonbinary Gray codes" page 18, and exercise 78 page 35 and answer page 54 (modular Gray g overline for the case all m_j=3).
- Joseph Rosenbaum, Elementary Problem E319, American Mathematical Monthly, volume 45, number 10, December 1938, pages 694-696. (For p=3, stepping though switch position combinations by single changes.)
- Index entries for sequences that are permutations of the natural numbers
Crossrefs
Programs
-
Maple
gray := proc(inp::integer,bas::integer) local resul, digs, convdigs, compl,d ; digs := [op(convert(inp,base,bas)),0] ; convdigs := [] ; for d from 1 to nops(digs)-1 do compl := op(d,digs)-op(d+1,digs) : if compl >= 0 then convdigs := [op(convdigs),compl] ; else convdigs := [op(convdigs),bas+compl] ; fi : od : convdigs := [op(convdigs),op(-1,digs)] : resul := 0 : for d from nops(convdigs) to 1 by -1 do resul := resul*bas + op(d,convdigs) : od : RETURN(resul) ; end: for n from 0 to 50 do printf("%a,",gray(n,3)) ; od : # R. J. Mathar, Mar 28 2006
-
Mathematica
a[n_] := Module[{v = IntegerDigits[n, 3]}, Do[v[[i]] = Mod[v[[i]] - v[[i-1]], 3], {i, Length[v], 2, -1}]; FromDigits[v, 3]]; Table[a[n], {n, 0, 50}] (* Jean-François Alcover, Jun 26 2023, after Kevin Ryde *)
-
PARI
a(n) = my(v=digits(n,3)); forstep(i=#v,2,-1, v[i]=(v[i]-v[i-1])%3); fromdigits(v,3); \\ Kevin Ryde, May 23 2020
Extensions
More terms from R. J. Mathar, Mar 28 2006
Comments