OFFSET
0,4
COMMENTS
As the graph demonstrates, there are large discontinuities in the sequence between terms 3^i-1 and 3^i, and between terms 2*3^i-1 and 2*3^i. - N. J. A. Sloane, Jul 03 2016
REFERENCES
D. E. Knuth, The Art of Computer Programming. Addison-Wesley, Reading, MA, Vol. 2, pp. 173-175; 2nd. ed. pp. 190-193.
LINKS
Gheorghe Coserea, Table of n, a(n) for n = 0..59048 (first 729 terms from A. Karttunen)
Ken Levasseur, The Balanced Ternary Number System
Tilman Piesk, First 27 numbers with their ternary representation
N. J. A. Sloane and Brady Haran, Amazing Graphs II (including Star Wars), Numberphile video (2019)
Wikipedia, Balanced ternary
FORMULA
EXAMPLE
7 in base 3 is 21; changing the 2 to a (-1) gives (-1)*3+1 = -2, so a(7) = -2. I.e., the number of -2 according to the balanced ternary enumeration is 7, which can be obtained by replacing every -1 by 2 in the balanced ternary representation (or expansion) of -2, which is -1,1.
MAPLE
f:= proc(n) local L, i;
L:= subs(2=-1, convert(n, base, 3));
add(L[i]*3^(i-1), i=1..nops(L))
end proc:
map(f, [$0..100]);
# alternate:
N:= 100: # to get a(0) to a(N)
g:= 0:
for n from 1 to ceil(log[3](N+1)) do
g:= convert(series(3*subs(x=x^3, g)*(1+x+x^2)+x/(1+x+x^2), x, 3^n+1), polynom);
od:
seq(coeff(g, x, j), j=0..N); # Robert Israel, Nov 17 2015
# third Maple program:
a:= proc(n) option remember; `if`(n=0, 0,
3*a(iquo(n, 3, 'r'))+`if`(r=2, -1, r))
end:
seq(a(n), n=0..3^4-1); # Alois P. Heinz, Aug 14 2019
MATHEMATICA
Map[FromDigits[#, 3] &, IntegerDigits[#, 3] /. 2 -> -1 & /@ Range@ 80] (* Michael De Vlieger, Nov 17 2015 *)
PROG
(MIT/GNU Scheme:) (define (A117966 n) (let loop ((z 0) (i 0) (n n)) (if (zero? n) z (loop (+ z (* (expt 3 i) (if (= 2 (modulo n 3)) -1 (modulo n 3)))) (1+ i) (floor->exact (/ n 3)))))) -- Antti Karttunen, May 19 2008
(PARI) a(n) = subst(Pol(apply(x->if(x == 2, -1, x), digits(n, 3)), 'x), 'x, 3)
vector(73, i, a(i-1)) \\ Gheorghe Coserea, Nov 17 2015
(Python)
def a(n):
if n==0: return 0
if n%3==0: return 3*a(n//3)
elif n%3==1: return 3*a((n - 1)//3) + 1
else: return 3*a((n - 2)//3) - 1
print([a(n) for n in range(101)]) # Indranil Ghosh, Jun 06 2017
CROSSREFS
KEYWORD
AUTHOR
Franklin T. Adams-Watters, Apr 05 2006
EXTENSIONS
Name corrected by Andrey Zabolotskiy, Nov 10 2019
STATUS
approved