%I #24 Mar 04 2022 09:40:07
%S 1,1,-1,1,0,1,1,1,-1,-1,1,-1,0,1,-1,1,1,0,-1,1,0,0,1,0,1,1,1,-1,1,1,0,
%T 1,1,1,1,-1,-1,-1,1,-1,-1,0,1,-1,-1,1,1,-1,0,-1,1,-1,0,0,1,-1,0,1,1,
%U -1,1,-1,1,-1,1,0,1,-1,1,1,1,0,-1,-1,1,0,-1,0,1,0,-1,1,1,0,0,-1,1,0,0,0,1,0,0,1,1,0,1,-1,1,0,1,0,1,0,1,1,1,1,-1,-1
%N List consisting of the representation of n in base 3 using digits -1, 0, 1.
%C Every natural number n has a unique representation as n = Sum_{i=1..k} e(i)*(3^i) for some k where e(i) is one of -1,0,1. Example: 25 = 27-3+1 = 1*3^3+0*3^2+(-1)*3^1+1*3^0 so its representation is 1,0,-1,1. So by writing n in this base 3 representation and juxtaposing we get the sequence: (1), (1,-1), (1,0), (1,1), (1,-1,-1), ...
%D D. E. Knuth, The Art of Computer Programming, Addison-Wesley, Reading, MA, Vol 2, pp 173-175.
%H Michael De Vlieger, <a href="/A059095/b059095.txt">Table of n, a(n) for n = 1..4560</a> (rows 1 <= n <= 729 = 3^6, flattened)
%H Wikipedia, <a href="http://en.wikipedia.org/wiki/Balanced_ternary">Balanced Ternary</a>
%F n = Sum_{0 <= k < A134021(n)} a(A134421(n)-2-k)*3^k, for n>0. - _Reinhard Zumkeller_, Oct 25 2007
%e From _Michael De Vlieger_, Jun 27 2020: (Begin)
%e First 27 rows, with terms aligned with powers of 3:
%e 3^3 3^2 3^1 3^0
%e --------------------
%e 1: 1;
%e 2: 1, -1;
%e 3: 1, 0;
%e 4: 1, 1;
%e 5: 1, -1, -1;
%e 6: 1, -1, 0;
%e 7: 1, -1, 1;
%e 8: 1, 0, -1;
%e 9: 1, 0, 0;
%e 10: 1, 0, 1;
%e 11: 1, 1, -1;
%e 12: 1, 1, 0;
%e 13: 1, 1, 1;
%e 14: 1, -1, -1, -1;
%e 15: 1, -1, -1, 0;
%e 16: 1, -1, -1, 1;
%e 17: 1, -1, 0, -1;
%e 18: 1, -1, 0, 0;
%e 19: 1, -1, 0, 1;
%e 20: 1, -1, 1, -1;
%e 21: 1, -1, 1, 0;
%e 22: 1, -1, 1, 1;
%e 23: 1, 0, -1, -1;
%e 24: 1, 0, -1, 0;
%e 25: 1, 0, -1, 1;
%e 26: 1, 0, 0, -1;
%e 27: 1, 0, 0, 0;
%e ... (End)
%t Array[If[First@ # == 0, Rest@ #, #] &[Prepend[IntegerDigits[#, 3], 0] //. {a___, b_, 2, c___} :> {a, b + 1, -1, c}] &, 32] // Flatten (* _Michael De Vlieger_, Jun 27 2020 *)
%o (Python)
%o def b3(n):
%o if n == 0: return []
%o carry, trailing = [(0, 0), (0, 1), (1, -1)][n % 3]
%o return b3(n//3 + carry) + [trailing]
%o t = []
%o for n in range(50):
%o t += b3(n)
%o print(t)
%o # _Andrey Zabolotskiy_, Nov 10 2019
%o (PARI) row(n) = apply(d->d-1, digits(n + 3^(logint(n<<1,3)+1)>>1, 3)); \\ _Kevin Ryde_, Mar 04 2022
%Y Cf. A117966, A134021 (row lengths, starting from row 1), A102283 (last each row), A065363 (row sums).
%Y Cf. A003137 (ternary).
%K tabf,sign,easy
%O 1
%A Avi Peretz (njk(AT)netvision.net.il), Feb 13 2001
%E More terms from Larry Reeves (larryr(AT)acm.org), Jul 20 2001
%E Offset corrected by _Andrey Zabolotskiy_, Nov 10 2019