cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

A105530 Ternary modular Gray code for n.

Original entry on oeis.org

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

Views

Author

Gary W. Adamson, Apr 11 2005

Keywords

Comments

Ternary number n is converted into ternary Gray code a(n) by using the following algorithm: Leftmost term (i.e., digit) is leftmost Gray code term. Then going to the right, if next term b is greater than current term a, then (b - a) is the next Gray code term. (Gray code terms do not enter into the algorithmic operation). If next term b < a, then add 3 to b and perform [(3+b) - a] which becomes the next Gray code term. If b = a, the Gray code term = 0.
Interpreting any N-Ary code for n as N-Ary Gray code or vice versa results in a permutation of the natural numbers. Any N-Ary term can be converted to the N-Ary Gray code by using a generalization of the algorithmic rules such that if b < a, then add N to b and perform [(N + b) - a]. The other rules remain the same.
Inverse permutation of A105529.

Crossrefs

Cf. A105529 (inverse), A128173 (ternary reflected), A003188 (binary), A098488 (decimal modular).

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