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.

Showing 1-4 of 4 results.

A128173 Numbers in ternary reflected Gray code order.

Original entry on oeis.org

0, 1, 2, 5, 4, 3, 6, 7, 8, 17, 16, 15, 12, 13, 14, 11, 10, 9, 18, 19, 20, 23, 22, 21, 24, 25, 26, 53, 52, 51, 48, 49, 50, 47, 46, 45, 36, 37, 38, 41, 40, 39, 42, 43, 44, 35, 34, 33, 30, 31, 32, 29, 28, 27, 54, 55, 56, 59, 58, 57, 60, 61, 62, 71, 70, 69, 66, 67, 68, 65, 64, 63, 72
Offset: 0

Views

Author

Ralf Stephan, May 09 2007

Keywords

Crossrefs

Programs

  • Maple
    A128173 := proc(nmax) local K,tmp,n3,n,r,c,t,a ; n3 := 3 ; n := 1 ; K := linalg[matrix](n3,1,[[0],[1],[2]]) ; while n3 < nmax do n3 := n3*3 ; n := n+1 ; tmp := K ; K := linalg[extend](K,2*n3/3,1,0) ; K := linalg[copyinto](tmp,K,1+n3/3,1) ; K := linalg[copyinto](tmp,K,1+2*n3/3,1) ; for r from 1 to n3 do K[r,n] := floor((r-1)/(n3/3)) ; od ; for r from n3/3+1 to n3/2 do for c from 1 to n do t := K[r,c] ; K[r,c] := K[n3+1-r,c] ; K[n3+1-r,c] := t ; od ; od ; od ; a := [] ; for r from 1 to n3 do a := [op(a), add( K[r,c]*3^(c-1),c=1..n) ] ; od ; a ; end: A128173(30) ; # R. J. Mathar, Jun 17 2007
  • Mathematica
    a[n_] := Module[{v, r, i}, v = IntegerDigits[n, 3]; r = 0; For[i = 1, i <= Length[v], i++, If[r == 1, v[[i]] = 2 - v[[i]]]; r = Mod[r + v[[i]], 2]]; FromDigits[v, 3]];
    a /@ Range[0, 100] (* Jean-François Alcover, Jul 18 2020, after Kevin Ryde *)
  • PARI
    a(n) = my(v=digits(n,3),r=Mod(0,2)); for(i=1,#v, if(r,v[i]=2-v[i]); r+=v[i]); fromdigits(v,3); \\ Kevin Ryde, May 21 2020

Extensions

More terms from R. J. Mathar, Jun 17 2007
Offset changed to 0 by Alois P. Heinz, Feb 23 2018

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

A370932 For any number n >= 0 with ternary expansion Sum_{i >= 0} t_i * 3^i, a(n) = Sum_{i >= 0} ((Sum_{j >= 0} (-1)^j * t_{i+j}) mod 3) * 3^i.

Original entry on oeis.org

0, 1, 2, 5, 3, 4, 7, 8, 6, 16, 17, 15, 9, 10, 11, 14, 12, 13, 23, 21, 22, 25, 26, 24, 18, 19, 20, 50, 48, 49, 52, 53, 51, 45, 46, 47, 27, 28, 29, 32, 30, 31, 34, 35, 33, 43, 44, 42, 36, 37, 38, 41, 39, 40, 70, 71, 69, 63, 64, 65, 68, 66, 67, 77, 75, 76, 79, 80
Offset: 0

Views

Author

Rémy Sigrist, Mar 06 2024

Keywords

Comments

In other words, the k-th ternary digit of a(n) is congruent (modulo 3) to the alternate sum of the digits to the left of (and including) the k-th ternary digit of n.
This sequence is a permutation of the nonnegative integers with inverse A071770 that preserves the number of ternary digits (A081604) and the leading ternary digit (A122586).

Examples

			For n = 42: the ternary expansion of 42 is "1120"; also:
     + 1             = 1 (mod 3)
     - 1 + 1         = 0 (mod 3)
     + 1 - 1 + 2     = 2 (mod 3)
     - 1 + 1 - 2 + 0 = 1 (mod 3)
- so the ternary expansion of a(42) is "1021", and a(42) = 34.
		

Crossrefs

Cf. A006068 (base-2 analog), A081604, A105529, A122586, A071770 (inverse).

Programs

  • PARI
    a(n, base = 3) = { my (d = digits(n, base), s = 0); for (i = 1, #d, d[i] = (s = d[i]-s) % base;); fromdigits(d, base); }
    
  • Python
    from itertools import accumulate
    from sympy.ntheory import digits
    def A370932(n):
        t = accumulate(((-j if i&1 else j) for i, j in enumerate(digits(n,3)[1:])),func=lambda x,y: (x+y)%3)
        return int(''.join(str(-d%3 if i&1 else d) for i,d in enumerate(t)),3) # Chai Wah Wu, Mar 08 2024

Formula

A081604(a(n)) = A081604(n).
A122586(a(n)) = A122586(n).

A247891 a(n) is Gray-coded in base 4 into n.

Original entry on oeis.org

0, 1, 2, 3, 5, 6, 7, 4, 10, 11, 8, 9, 15, 12, 13, 14, 21, 22, 23, 20, 26, 27, 24, 25, 31, 28, 29, 30, 16, 17, 18, 19, 42, 43, 40, 41, 47, 44, 45, 46, 32, 33, 34, 35, 37, 38, 39, 36, 63, 60, 61, 62, 48, 49, 50, 51, 53, 54, 55, 52, 58, 59, 56, 57, 85, 86, 87, 84, 90
Offset: 0

Views

Author

Alex Ratushnyak, Sep 26 2014

Keywords

Comments

Permutation of nonnegative numbers.

Crossrefs

Cf. A006068 (base 2), A105529 (base 3), A226134 (base 10).

Programs

  • Python
    def basexor(a, b, base):
      result = 0
      digit = 1
      while a or b:
        da = a % base
        db = b % base
        a //= base
        b //= base
        sum = (da+db) % base
        result += sum * digit
        digit *= base
      return result
    base = 4  #  2 => A006068, 3 => A105529, 10 => A226134
    for n in range(129):
      a = n
      b = n//base
      while b:
        a = basexor(a,b, base)
        b //= base
      print(a, end=', ')
    
  • Python
    def xor4(x, y): return 4*xor4(x//4, y//4) + (x+y)%4 if x else y
    def a(n): return xor4(n, a(n//4)) if n else 0 # David Radcliffe, Jun 22 2025

Formula

a(n) = n XOR4 [n/4] XOR4 [n/16] XOR4 [n/64] ... XOR4 [n/4^m] where m = [log(n)/log(4)], [x] is integer floor of x, and XOR4 is a base 4 analog of binary exclusive-OR operator.
In other words, a(n) = n + [n/4] + [n/16] + ... where addition is performed in base 4 without carries. - David Radcliffe, Jun 22 2025
Showing 1-4 of 4 results.