g | x | w | all
Bytes Lang Time Link
004Thunno 2230721T065717ZThe Thon
004Vyxal210915T101035Zemanresu
026x86180410T185800Zqwr
074BrainFlak180409T202210ZMegaTom
054Go180411T114413ZKristoff
052Java 10180409T124514ZKevin Cr
117Whitespace180410T133756ZKevin Cr
037Zsh180411T060159Zmuru
034Julia 0.6180410T191938Zgggg
007Jstx180410T160436ZQuantum6
00505AB1E180409T124820ZUriel
043R180409T131043ZGiuseppe
028Ruby180410T083339Zanna328p
050brainfuck180410T002511ZJo King
054Excel180409T232857ZSophia L
025Pari/GP180409T182750Zalephalp
066dc180409T174700Zbrhfl
007Stax180409T135551Zrecursiv
008Pyth180409T151404ZDigital
036Perl 5180409T160453ZXcali
128C# Visual C# Compiler180409T155754ZHyarus
006MATL180409T150007ZStewie G
008CJam180409T152657ZLuis Men
010APL Dyalog180409T124646ZUriel
028Bash + GNU utilities180409T151659ZDigital
064PHP180409T132919ZFrancisc
043AWK180409T145328ZRobert B
032Octave with the communication toolbox180409T134100ZStewie G
005Husk180409T143622ZFyr
006Japt180409T142950ZEtheryte
032Haskell180409T142420ZAngs
036Retina 0.8.2180409T141116ZNeil
007J180409T124856ZDELETE_M
027C180409T133834ZO.O.Bala
007Japt180409T134439ZShaggy
019Attache180409T133351ZConor O&
023JavaScript Node.js180409T124532Zl4m2
027Ruby180409T132426ZKirill L
054Python 2180409T130207ZElPedro
031Python 2180409T130603ZDennis
055Python 2180409T130328ZTFeld
044Java JDK 10180409T125622ZDELETE_M
014Add++180409T125329Zcaird co
007Brachylog180409T125114ZFatalize
004Jelly180409T124447ZErik the

Thunno 2, 4 bytes

ḃ⁺3b

Try it online!

Explanation

ḃ⁺3b  # Implicit input
ḃ     # Convert to binary
 ⁺    # Increment each
  3b  # Convert from base-3
      # Implicit output

Vyxal, 4 bytes

b›3β

Try it Online!

b    # Binary
 ›   # Increment
  3β # From base 3

x86, 26 bytes

Rewrite of the recursive formula as a tail-recursive stack function, since function calls are expensive. Uses __fastcall convention (input in ecx, output in eax).

The cmp %esp,%ebp/je sum1 branch is done to prevent an extra multiplication of 3 from occurring. It might save bytes to avoid this branch. Reordering the multiplication makes this unnecessary and saves 4 bytes.

.section .text
.globl main
main:
        mov     $10, %ecx

start:
        mov     %esp, %ebp          # Save sp 
build:
        mov     %ecx, %eax
        and     $1, %eax
        inc     %eax                # n%2 + 1
        push    %eax                # push n%2 + 1
        sar     %ecx                # n >>= 1
        jnz     build               # do while (n)
        
        xor     %eax, %eax          # sum = 0
sum0:
        lea     (%eax,%eax,2),%eax  # sum *= 3
        pop     %ebx
        add     %ebx, %eax          # pop stack, add to sum
        cmp     %esp, %ebp
        jnz     sum0                # do while stack non-empty


        ret

Hexdump:

00000039  89 e5 89 c8 83 e0 01 40  50 d1 f9 75 f5 31 c0 8d  |.......@P..u.1..|
00000049  04 40 5b 01 d8 39 e5 75  f6 c3                    |.@[..9.u..|

Assembly-friendly python:

stack = []
while n:
    stack.append(n%2 + 1)
    n //= 2

while stack:
    s *= 3
    s += stack.pop()

Brain-Flak, 74 bytes

({<>(())<>({<({}[()])><>([{}]())<>})}(<>)){{}((({})()){}{}[{}])([][()])}{}

Try it online!

"Readable" version

({<>(())<>
  ({
    <({}[()])>
    <>
    ([{}]())
    <>
  })
}
# At this point we have a inverted binary string on the stack
(<>)
)
{
  {}
  (
    (({})()){}{}[{}]
  )
  ([][()])
}{}

Go, 54 bytes

func f(n int)(o int){if n>0{o=n%2+1+3*f(n>>1)};return}

Base solution is the same as the one that everyone else uses.

Try it online!

Java 10, 81 52 bytes (Base conversion)

n->n.toString(n,2).chars().reduce(0,(r,c)->r*3+c-47)

Try it online.

-29 bytes thanks to @Holger.

Explanation:

n->{                         // Method with Long as both parameter and return-type
  n.toString(n,2)            //  Convert the input to a Base-2 String
  .chars().reduce(0,(r,c)->  //  Loop over its digits as bytes
    r*3+c-47)                //  Multiply the current result by 3, and add the digit + 1
                             //  (which is equal to increasing each digit by 1,
                             //  and then converting from Base-3 to Base-10)

Java 10, 171 167 151 150 149 bytes (Sequence)

n->{int t=31-n.numberOfLeadingZeros(n);return a(t+1)+b(n-(1<<t));};int a(int n){return--n<1?n+2:3*a(n)+1;}int b(int n){return n<1?0:n+3*b(n/=2)+n*2;}

-16 bytes thanks to @musicman523, changing (int)Math.pow(2,t) to (1<<t).
-1 byte thanks to @Holger, changing (int)(Math.log(n)/Math.log(2)) to 31-n.numberOfLeadingZeros(n).

Try it online.

Explanation:

n->{                         // Method with Integer as both parameter and return-type
  int t=31-n.numberOfLeadingZeros(n);
                             //  2_log(n)
  return a(t+1)              //  Return A060816(2_log(n)+1)
         +b(n-(1<<t));}      //   + A005836(n-2^2_log(n))

// A060816: a(n) = 3*a(n-1) + 1; a(0)=1, a(1)=2
int a(int n){return--n<1?n+2:3*a(n)+1;}

// A005836: a(n+1) = 3*a(floor(n/2)) + n - 2*floor(n/2).
int b(int n){return n<1?0:n+3*b(n/=2)+n*2;}

When we look at the sequence:

2,  7,8,  22,23,25,26,  67,68,70,71,76,77,79,80,  202,203,205,206,211,212,214,215,229,230,232,233,238,239,241,242, ...

We can see multiple subsequences:

A053645(n):
0,  0,1,  0,1,2,3,  0,1,2,3,4,5,6,7,  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,  ...

A060816(A053645(n)):
2,  7,7,  22,22,22,22,  67,67,67,67,67,67,67,67,  202,202,202,202,202,202,202,202,202,202,202,202,202,202,202,  ...

A005836(A053645(n)+1)
0,  0,1,  0,1,3,4,  0,1,3,4,9,10,12,13,  0,1,3,4,9,10,12,13,27,28,30,31,36,37,39,40,  ...

So the sequence being asked is:

A060816(A053645(n)) + A005836(A053645(n)+1)

I suck at finding patterns, so I'm proud of what I found above.. Having said that, @user202729 found a better and shorter approach in Java within a few minutes.. :'(

Whitespace, 117 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_number][T    T   T   _Retrieve][N
S S S N
_Create_Label_OUTER_LOOP][S N
S _Duplicate][S S S T   S N
_Push_2][T  S T T   _Modulo][S S S T    N
_Push_1][T  S S S _Add][S N
T   _Swap][S S S T  S N
_Push_2][T  S T S _Integer_division][S N
S _Duplicate][N
T   S N
_If_0_jump_to_Label_INNER_LOOP][N
S N
S N
_Jump_to_Label_OUTER_LOOP][N
S S N
_Create_Label_INNER_LOOP][S S S T   T   N
_Push_3][T  S S N
_Multiply][T    S S S _Add][S N
T   _Swap][S N
S _Duplicate][N
T   S T N
_If_0_jump_to_Label_PRINT_AND_EXIT][S N
T   _Swap][N
S N
N
_Jump_to_Label_INNER_LOOP][N
S S T   N
_Create_Label_PRINT_AND_EXIT][S N
T   _Swap][T    N
S T _Output_integer_to_STDOUT]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

I first converted the recursive function int f(int n){return n<1?0:n%2+1+3*f(n/2);} to its iterative form (in pseudo-code):

Integer n = STDIN as integer
Add starting_value 0 to the stack
function OUTER_LOOP:
  while(true){
    Add n%2+1 to the stack
    n = n/2
    if(n == 0):
      Jump to INNER_LOOP
    Else:
      Jump to next iteration OUTER_LOOP

function INNER_LOOP:
  while(true){
    n = 3*n
    n = n + Value at the top of the stack (the ones we calculated with n%2+1)
    Swap top two items
    Check if the top is now 0 (starting value):
      Jump to PRINT_AND_EXIT
    Else:
      Swap top two items back
      Jump to next iteration INNER_LOOP

function PRINT_AND_EXIT:
  Swap top two items back
  Print top to STDOUT as integer
  Exit program with error: Exit not defined

And I then implemented this iterative approach in the stack-based language Whitespace, using it's default stack.

Example runs:

Input: 1

Command    Explanation                   Stack           Heap    STDIN   STDOUT   STDERR

SSSN       Push 0                        [0]
SNS        Duplicate top (0)             [0,0]
SNS        Duplicate top (0)             [0,0,0]
TNTT       Read STDIN as integer         [0,0]           {0:1}   1
TTT        Retrieve                      [0,1]           {0:1}
NSSSN      Create Label OUTER_LOOP       [0,1]           {0:1}
 SNS       Duplicate top (1)             [0,1,1]         {0:1}
 SSSTSN    Push 2                        [0,1,1,2]       {0:1}
 TSTT      Modulo top two (1%2)          [0,1,1]         {0:1}
 SSSTN     Push 1                        [0,1,1,1]       {0:1}
 TSSS      Add top two (1+1)             [0,1,2]         {0:1}
 SNT       Swap top two                  [0,2,1]         {0:1}
 SSSTSN    Push 2                        [0,2,1,2]       {0:1}
 TSTS      Int-divide top two (1/2)      [0,2,0]         {0:1}
 SNS       Duplicate top (0)             [0,2,0,0]       {0:1}
 NTSN      If 0: Go to Label INNER_LOOP  [0,2,0]         {0:1}
 NSSN      Create Label INNER_LOOP       [0,2,0]         {0:1}
  SSSTTN   Push 3                        [0,2,0,3]       {0:1}
  TSSN     Multiply top two (0*3)        [0,2,0]         {0:1}
  TSSS     Add top two (2+0)             [0,2]           {0:1}
  SNT      Swap top two                  [2,0]           {0:1}
  SNS      Duplicate top (0)             [2,0,0]         {0:1}
  NTSTN    If 0: Jump to Label PRINT     [2,0]           {0:1}
  NSSTN    Create Label PRINT            [2,0]           {0:1}
   SNT     Swap top two                  [0,2]           {0:1}
   TNST    Print top to STDOUT           [0]             {0:1}           2
                                                                                  error

Try it online (with raw spaces, tabs and new-lines only).
Stops with error: Exit not defined.

Input: 4

Command    Explanation                   Stack           Heap    STDIN   STDOUT   STDERR

SSSN       Push 0                        [0]
SNS        Duplicate top (0)             [0,0]
SNS        Duplicate top (0)             [0,0,0]
TNTT       Read STDIN as integer         [0,0]           {0:4}   4
TTT        Retrieve                      [0,4]           {0:4}
NSSSN      Create Label OUTER_LOOP       [0,4]           {0:4}
 SNS       Duplicate top (4)             [0,4,4]         {0:4}
 SSSTSN    Push 2                        [0,4,4,2]       {0:4}
 TSTT      Modulo top two (4%2)          [0,4,0]         {0:4}
 SSSTN     Push 1                        [0,4,0,1]       {0:4}
 TSSS      Add top two (0+1)             [0,4,1]         {0:4}
 SNT       Swap top two                  [0,1,4]         {0:4}
 SSSTSN    Push 2                        [0,1,4,2]       {0:4}
 TSTS      Int-divide top two (4/2)      [0,1,2]         {0:4}
 SNS       Duplicate top (2)             [0,1,2,2]       {0:4}
 NTSN      If 0: Go to Label INNER_LOOP  [0,1,2]         {0:4}
 NSNSN     Jump to Label OUTER_LOOP      [0,1,2]         {0:4}
 SNS       Duplicate top (2)             [0,1,2,2]       {0:4}
 SSSTSN    Push 2                        [0,1,2,2,2]     {0:4}
 TSTT      Modulo top two (2%2)          [0,1,2,0]       {0:4}
 SSSTN     Push 1                        [0,1,2,0,1]     {0:4}
 TSSS      Add top two (0+1)             [0,1,2,1]       {0:4}
 SNT       Swap top two                  [0,1,1,2]       {0:4}
 SSSTSN    Push 2                        [0,1,1,2,2]     {0:4}
 TSTS      Int-divide top two (2/2)      [0,1,1,1]       {0:4}
 SNS       Duplicate top (1)             [0,1,1,1,1]     {0:4}
 NTSN      If 0: Go to Label INNER_LOOP  [0,1,1,1]       {0:4}
 NSNSN     Jump to Label OUTER_LOOP      [0,1,1,1]       {0:4}
 SNS       Duplicate top (1)             [0,1,1,1,1]     {0:4}
 SSSTSN    Push 2                        [0,1,1,1,1,2]   {0:4}
 TSTT      Modulo top two (1%2)          [0,1,1,1,1]     {0:4}
 SSSTN     Push 1                        [0,1,1,1,1,1]   {0:4}
 TSSS      Add top two (1+1)             [0,1,1,1,2]     {0:4}
 SNT       Swap top two                  [0,1,1,2,1]     {0:4}
 SSSTSN    Push 2                        [0,1,1,2,1,2]   {0:4}
 TSTS      Int-divide top two (1/2)      [0,1,1,2,0]     {0:4}
 SNS       Duplicate top (0)             [0,1,1,2,0,0]   {0:4}
 NTSN      If 0: Go to Label INNER_LOOP  [0,1,1,2,0]     {0:4}
 NSSN      Create Label INNER_LOOP       [0,1,1,2,0]     {0:4}
  SSSTTN   Push 3                        [0,1,1,2,0,3]   {0:4}
  TSSN     Multiply top two (0*3)        [0,1,1,2,0]     {0:4}
  TSSS     Add top two (2+0)             [0,1,1,2]       {0:4}
  SNT      Swap top two                  [0,1,2,1]       {0:4}
  SNS      Duplicate top (1)             [0,1,2,1,1]     {0:4}
  NTSTN    If 0: Jump to Label PRINT     [0,1,2,1]       {0:4}
  SNT      Swap top two                  [0,1,1,2]       {0:4}
  NSNN     Jump to Label INNER_LOOP      [0,1,1,2]       {0:4}
  SSSTTN   Push 3                        [0,1,1,2,3]     {0:4}
  TSSN     Multiply top two (2*3)        [0,1,1,6]       {0:4}
  TSSS     Add top two (1+6)             [0,1,7]         {0:4}
  SNT      Swap top two                  [0,7,1]         {0:4}
  SNS      Duplicate top (1)             [0,7,1,1]       {0:4}
  NTSTN    If 0: Jump to Label PRINT     [0,7,1]         {0:4}
  SNT      Swap top two                  [0,1,7]         {0:4}
  NSNN     Jump to Label INNER_LOOP      [0,1,7]         {0:4}
  SSSTTN   Push 3                        [0,1,7,3]       {0:4}
  TSSN     Multiply top two (7*3)        [0,1,21]        {0:4}
  TSSS     Add top two (1+21)            [0,22]          {0:4}
  SNT      Swap top two                  [22,0]          {0:4}
  SNS      Duplicate top (0)             [22,0,0]        {0:4}
  NTSTN    If 0: Jump to Label PRINT     [22,0]          {0:4}
  NSSTN    Create Label PRINT            [22,0]          {0:4}
   SNT     Swap top two                  [0,22]          {0:4}
   TNST    Print top to STDOUT           [0]             {0:4}           22
                                                                                  error

Try it online (with raw spaces, tabs and new-lines only).
Stops with error: Exit not defined.

Zsh, 37 bytes

a=3#$[[##2]$1];echo $[a+${a//[01]/1}]

Try it online!

Julia 0.6, 34 bytes

x->parse(Int,map(x->x+1,bin(x)),3)

Try it online!

Tho it seems like this is the cool way

Julia 0.6, 25 bytes

f(n)=n<1?0:n%2+1+3f(n÷2)

Try it online!

Jstx, 7 bytes

£?☺å♥£F

Try it online!

Explanation

£? # Push the first stack value in binary.
☺  # Push literal 1
å  # Push the second stack value with all characters arithmetically shifted by the first stack value.
♥  # Push literal 3
£F # Push the second stack value in base first stack value converted to decimal.

05AB1E, 5 bytes

b€>3β

Try it online!

b       binary
 €>     increment each
   3β   base 3

05AB1E, 5 bytes

2в>3β

Try it online!

R, 55 43 bytes

function(n)(n%/%2^(x=0:log2(n))%%2+1)%*%3^x

Try it online!

Uses the standard base conversion trick in R, increments, and then uses a dot product with powers of 3 to convert back to an integer.

Thanks to @user2390246 for dropping 12 bytes!

Ruby, 28 bytes

f=->x{x>0?x%2+3*f[x>>1]+1:0}

black magic, no idea how it works

Try it online!

brainfuck, 50 bytes

+<,[[>-]++>[<]<[>+[<]<]>>-]>[>]<-<<[>[-<+++>]<<]>.

Try it online!

Input and output as code points, and assumes arbitrary size cells otherwise it can't get past n=31.

Excel, 54 bytes

=DECIMAL(SUBSTITUTE(SUBSTITUTE(BASE(A1,2),1,2),0,1),3)

A fairly straightforward translation of the problem. Takes input from A1. I tried some cuter things with bit math like

=SUM(IF(ROW(1:31)<LOG(A1,2)+1,POWER(3,ROW(1:31)-1)*(1+MOD(BITRSHIFT(A1, ROW(1:31)-1),2))))

because nesting SUBSTITUTE always feels wasteful, but couldn't get nearly as short.

Pari/GP, 25 bytes

f(n)=if(n,n%2+1+3*f(n\2))

Try it online!

dc, 66 bytes

[2~rd0<B]dsBxz1-si[1+z:tz0<T]dsTx0dsj[ljd1+dsj;tr3r^*+ljli>M]dsMxp

Try it online!

[2~rd0<B]dsBx is a macro that uses dc's quotient/remainder integer division, ~ to continuously break divide by two, breaking n down to individual binary bits on the stack. It will always leave one extra zero, so we subtract one from the stack depth and store this total length in i with z1-si.

[1+z:tz0<T]dsTx is a macro that adds one to whatever is on the stack, and then pops that value, storing it at index(stack depth) in array t. This basically means that if we started with the binary 1101, t now holds 2, 1, 2, 2, assuming 1-indexing. 0dsj puts a zero on the stack so we don't get a stack empty error when we do our first addition, and it stores a zero in register j as well.

[ljd1+dsj;tr3r^*+ljli>M]dsMx is, unfortunately, a lot of counter nonsense. We need register j for two things - pull element (j+1) from array t, and multiply it by 3^j. We start macro M by putting j on the stack and duplicating it. We increment the new copy, duplicate it, and store it back into j. With the copy of (j+1) we left behind, we pull from array t. Swap so that our original j is at the top of the stack, do the necessary base-three math with 3r^*, add this 'bit' to our total, and then compare j with i to see if we still have more 'bits' to do. At the very end, we print.

Stax, 7 bytes

É¥ê4¼⌐○

Run and debug it

This one is pretty straightforward. After unpacking, and commenting, the program looks like this.

:B  Convert to bits
{^m Increment each
3|b Base-3 convert

Pyth, 8

ihMjQ2 3

How to eliminate the space and make the Q implicit?

Pyth online.

Perl 5, 36 bytes

$\+=(1+$_%2)*3**$b++,$_>>=1while$_}{

Try it online!

C# (Visual C# Compiler), 128 bytes

using System;using System.Linq;i=>{int z=0;return Convert.ToString(i,2).Reverse().Select(a=>(a-47)*(int)Math.Pow(3,z++)).Sum();}

Try it online!

I am counting System because i use Convert and Math.

MATL, 12 7 6 bytes

BQ3_ZA

Try it online!

Saved 5 bytes thanks to Giuseppe and another one thanks to Luis Mendo.

Old 7 byte answer:

YBQc3ZA

Try it online!

Explanation:

YB        % Convert to binary string
  Q       % Increment each element
   c      % Convert ASCII values to characters
    3     % Push 3
     ZA   % Convert from base 3 to decimal.

Old one for 12 bytes:

BQtz:q3w^!Y*

Try it online!

Oh my, that was messy... So is this: `BQ3GBn:q^!Y*.

Explanation:

               % Implicit input
B              % Convert to binary vector
 Q             % Increment all numbers
  t            % Duplicate
   z           % Number of element in vector
    :          % Range from 1 to that number
     q         % Decrement to get the range from 0 instead of 1
      3        % Push 3
       w       % Swap order of stack
        ^      % Raise 3 to the power of 0, 1, ...
         !     % Transpose
          Y*   % Matrix multiplication
               % Implicit output

CJam, 8 bytes

ri2b:)3b

Try it online!

Explanation

ri   e# Read input as an integer
2b   e# Convert to base 2. Gives a list containing 0 and 1
:)   e# Add 1 to each number in that list
3b   e# Convert list from base 3 to decimal. Implicitly display

APL (Dyalog), 10 bytes

3⊥1+2⊥⍣¯1⊢

Try it online!

    2⊥⍣¯1  binary
  1+       go guess
3⊥         base 3

Bash + GNU utilities, 28

dc -e?2op|tr 01 12|dc -e3i?p

Input read from STDIN.

Try it online!

PHP, 84 64 Bytes

Try it online!!

ORIGINAL Code

function f($n){$b=decbin($n);echo base_convert($b+str_repeat('1',strlen($b)),3,10);}

Try it online!!

Thanks to Cristoph, less bytes if ran with php -R

function f($n){echo base_convert(strtr(decbin($n),10,21),3,10);}

Explanation

function f($n){
$b=decbin($n);                    #Convert the iteger to base 2
echo base_convert(                  #base conversion PHP function
    $b+str_repeat('1',strlen($b)),  #It adds to our base 2 number
    3,                              #a number of the same digits length
    10);                            #with purely '1's
}

AWK, 43 bytes

func n(x){return x<1?0:x%2+1+3*n(int(x/2))}

Try it online!

I discovered that AWK has rshift and lshift functions. They use more bytes than just doing a divide and cast, but might be useful sometime. :)

Octave with the communication toolbox, 33 32 bytes

@(x)(de2bi(x)+1)*3.^(0:log2(x))'

Try it online!

Converts the input to a binary vector using de2bi, and incrementing all numbers. Does matrix multiplication with a vertical vector of 3 raised to the appropriate powers: 1, 3, 9, ..., thus getting the sum without an explicit call to sum.

Husk, 5 bytes

B3m→ḋ

Try it online!

Explanation

B3m→ḋ
    ḋ  Convert to base 2
  m→   Map increment
B3     Convert from base 3

Japt, 6 bytes

¤cÄ n3
¤      // Convert the input to a base-2 string,
 c     // then map over it as charcodes.
  Ä    // For each item, add one to its charcode
       // and when that's done,
    n3 // parse the string as a base 3 number.
 

Takes input as a number, outputs a number.

Try it online!

Haskell, 32 bytes

f 0=0
f a=1+mod a 2+3*f(div a 2)

Try it online!

Retina 0.8.2, 36 bytes

.+
$*
+`^(1+)\1
$1;1
^
1
+`1;
;111
1

Try it online! Explanation:

.+
$*

Convert from decimal to unary.

+`^(1+)\1
$1;1

Repeatedly divmod by 2, and add 1 to the result of the modulo.

^
1

Add 1 to the first digit too.

+`1;
;111

Convert from unary-encoded base 3 to unary.

1

Convert to decimal.

J, 7 bytes

3#.1+#:

Try it online!

Thanks Galen Ivanov for -4 bytes! I really need to improve my J golfing skill...

C, 32 27 bytes

n(x){x=x?x%2+1+3*n(x/2):0;}

Based on user202729's Java answer. Try it online here. Thanks to Kevin Cruijssen for golfing 5 bytes.

Ungolfed version:

n(x) { // recursive function; both argument and return type are implicitly int
    x = // implicit return
    x ? x % 2 + 1 + 3*n(x/2) // if x != 0 return x % 2 + 1 + 3*n(x/2) (recursive call)
    : 0; // else return 0
}

Japt, 7 bytes

¤£°XÃn3

Try it here

Attache, 19 bytes

FromBase&3@1&`+@Bin

Try it online!

This is a composition of three functions:

This first converts to binary (Bin), increments it (1&`+), then converts to ternary (FromBase&3).

Alternatives

Non-pointfree, 21 bytes: {FromBase[Bin!_+1,3]}

Without builtins, 57 bytes: Sum@{_*3^(#_-Iota!_-1)}@{If[_>0,$[_/2|Floor]'(1+_%2),[]]}

JavaScript (Node.js), 23 bytes

f=x=>x&&x%2+1+3*f(x>>1)

Try it online!

Ruby, 27 bytes

f=->x{x>0?x%2+1+3*f[x/2]:0}

Try it online!

Python 2, 56 54 bytes

lambda i:int(''.join(`int(x)+1`for x in bin(i)[2:]),3)

Try it online!

Python 2, 31 bytes

f=lambda n:n and 3*f(n/2)+n%2+1

Try it online!

Python 2, 56 55 bytes

lambda n:int(''.join('12'[c>'0']for c in bin(n)[2:]),3)

Try it online!

Java (JDK 10), 44 bytes

long n(long x){return x<1?0:x%2+1+3*n(x/2);}

Try it online!

Add++, 14 bytes

L,BBu1€+B]3$Bb

Try it online!

Brachylog, 7 bytes

ḃ+₁ᵐ~ḃ₃

Try it online!

Explanation

Not that you really need one, but…

ḃ            To binary
 +₁ᵐ         Map increment
    ~ḃ₃      From ternary

Jelly, 4 bytes

B‘ḅ3

Try it online!