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.

This page as a plain text file.
%I A105530 #29 Jun 26 2023 08:48:05
%S A105530 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,
%T A105530 18,45,46,47,50,48,49,52,53,51,33,34,35,29,27,28,31,32,30,39,40,41,44,
%U A105530 42,43
%N A105530 Ternary modular Gray code for n.
%C A105530 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.
%C A105530 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.
%C A105530 Inverse permutation of A105529.
%H A105530 Martin Cohn, <a href="https://doi.org/10.1016/S0019-9958(63)90119-0">Affine m-ary Gray Codes</a>, Information and Control, volume 6, 1963, pages 70-78.  (For the case m=3, U = P = identity matrix, v = 0 vector.)
%H A105530 Donald E. Knuth, <a href="http://www-cs-faculty.stanford.edu/~knuth/fasc2a.ps.gz">The Art of Computer Programming, Pre-Fascicle 2A</a>, 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).
%H A105530 Joseph Rosenbaum, <a href="https://doi.org/10.2307/2302451">Elementary Problem E319</a>, American Mathematical Monthly, volume 45, number 10, December 1938, pages 694-696.  (For p=3, stepping though switch position combinations by single changes.)
%H A105530 <a href="/index/Per#IntegerPermutation">Index entries for sequences that are permutations of the natural numbers</a>
%p A105530 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
%t A105530 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]];
%t A105530 Table[a[n], {n, 0, 50}] (* _Jean-François Alcover_, Jun 26 2023, after _Kevin Ryde_ *)
%o A105530 (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
%Y A105530 Cf. A105529 (inverse), A128173 (ternary reflected), A003188 (binary), A098488 (decimal modular).
%K A105530 nonn
%O A105530 0,3
%A A105530 _Gary W. Adamson_, Apr 11 2005
%E A105530 More terms from _R. J. Mathar_, Mar 28 2006