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.
%I A117967 #28 Jan 29 2025 19:36:58 %S A117967 0,1,5,3,4,17,15,16,11,9,10,14,12,13,53,51,52,47,45,46,50,48,49,35,33, %T A117967 34,29,27,28,32,30,31,44,42,43,38,36,37,41,39,40,161,159,160,155,153, %U A117967 154,158,156,157,143,141,142,137,135,136,140,138,139,152,150,151,146 %N A117967 Positive part of inverse of A117966; write n in balanced ternary and then replace (-1)'s with 2's. %D A117967 D. E. Knuth, The Art of Computer Programming. Addison-Wesley, Reading, MA, Vol. 2, pp. 173-175 %H A117967 Alois P. Heinz, <a href="/A117967/b117967.txt">Table of n, a(n) for n = 0..9841</a> %H A117967 Ken Levasseur, <a href="http://discretemath.org/ternary_number_system.html">The Balanced Ternary Number System</a> %F A117967 a(0) = 0, a(3n) = 3a(n), a(3n+1) = 3a(n)+1, a(3n-1) = 3a(n)+2. %F A117967 If one adds this clause, then the function is defined on the whole Z: If n<0, then a(n) = A004488(a(-n)) (or equivalently: a(n) = A117968(-n)) and then it holds that a(A117966(n)) = n. - _Antti Karttunen_, May 19 2008 %e A117967 7 in balanced ternary is 1(-1)1, changing to 121 ternary is 16, so a(7)=16. %p A117967 a:= proc(n) local d, i, m, r; m:=n; r:=0; %p A117967 for i from 0 while m>0 do %p A117967 d:= irem(m, 3, 'm'); %p A117967 if d=2 then m:=m+1 fi; %p A117967 r:= r+d*3^i %p A117967 od; r %p A117967 end: %p A117967 seq(a(n), n=0..100); # _Alois P. Heinz_, May 11 2015 %t A117967 a[n_] := Module[{d, i, m = n, r = 0}, For[i = 0, m > 0, i++, {m, d} = QuotientRemainder[m, 3]; If[d == 2, m++]; r = r + d*3^i]; r]; %t A117967 a /@ Range[0, 100] (* _Jean-François Alcover_, Jan 05 2021, after _Alois P. Heinz_ *) %o A117967 (Scheme) %o A117967 ;; Two alternative definitions in MIT/GNU Scheme, defined for whole Z: %o A117967 (define (A117967 z) (cond ((zero? z) 0) ((negative? z) (A004488 (A117967 (- z)))) (else (let* ((lp3 (expt 3 (A062153 z))) (np3 (* 3 lp3))) (if (< (* 2 z) np3) (+ lp3 (A117967 (- z lp3))) (+ np3 (A117967 (- z np3)))))))) %o A117967 (define (A117967v2 z) (cond ((zero? z) 0) ((negative? z) (A004488 (A117967v2 (- z)))) ((zero? (modulo z 3)) (* 3 (A117967v2 (/ z 3)))) ((= 1 (modulo z 3)) (+ (* 3 (A117967v2 (/ (- z 1) 3))) 1)) (else (+ (* 3 (A117967v2 (/ (+ z 1) 3))) 2)))) %o A117967 ;; _Antti Karttunen_, May 19 2008 %o A117967 (Python) %o A117967 from sympy.ntheory.factor_ import digits %o A117967 def a004488(n): return int("".join([str((3 - i)%3) for i in digits(n, 3)[1:]]), 3) %o A117967 def a117968(n): %o A117967 if n==1: return 2 %o A117967 if n%3==0: return 3*a117968(n/3) %o A117967 elif n%3==1: return 3*a117968((n - 1)/3) + 2 %o A117967 else: return 3*a117968((n + 1)/3) + 1 %o A117967 def a(n): return 0 if n==0 else a004488(a117968(n)) # _Indranil Ghosh_, Jun 06 2017 %Y A117967 Cf. A117966. a(n) = A004488(A117968(n)). Bisection of A140263. A140267 gives the same sequence in ternary. %K A117967 base,nonn,look %O A117967 0,3 %A A117967 _Franklin T. Adams-Watters_, Apr 05 2006