[Question]
  [
Given a positive integer n write some code to take its prime factorization and replace all of its factors of `2` with `3`.
For example
```
12 = 2 * 2 * 3 -> 3 * 3 * 3 = 27
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the goal is to minimize the byte count of your answer.
# Test cases
```
1 -> 1
2 -> 3
3 -> 3
4 -> 9
5 -> 5
6 -> 9
7 -> 7
8 -> 27
9 -> 9
10 -> 15
11 -> 11
12 -> 27
13 -> 13
14 -> 21
15 -> 15
16 -> 81
17 -> 17
18 -> 27
19 -> 19
20 -> 45
21 -> 21
22 -> 33
23 -> 23
24 -> 81
25 -> 25
26 -> 39
27 -> 27
28 -> 63
29 -> 29
```
  
[Answer]
# [Fractran](https://esolangs.org/wiki/Fractran), 3 bytes
```
3/2
```
Fractran literally only has one builtin, but it happens to do exactly what this task is asking for. (It's also Turing-complete, by itself.)
The language doesn't really have a standardised syntax or interpreter. [This interpreter](http://scienceblogs.com/goodmath/2006/10/28/more-fractran-a-trivial-interp-1/#comment-3759) (in a comment to a blog post ‚Äì
 it's a very simple language) will accept the syntax shown here. (There are other Fractran interpreters with other syntaxes, e.g. some would write this program as `3 2`, or even using `3` and `2` as command line arguments, which would lead to a score of 0+3 bytes. I doubt it's possible to do better than 3 in a pre-existing interpreter, though.)
## Explanation
```
3/2
 /   Replace a factor of
  2  2
3    with 3
     {implicit: repeat until no more replacements are possible}
```
[Answer]
# [Python 2](https://docs.python.org/2/), 28 bytes
```
f=lambda n:n%2*n or 3*f(n/2)
```
[Try it online!](https://tio.run/nexus/python2#@59mm5OYm5SSqJBnladqpJWnkF@kYKyVppGnb6T5Pw3IyVPIzFMoSsxLT9Uw1DE20LTi4iwoyswrUQCq0fwPAA "Python 2 ‚Äì TIO Nexus")
Recursively divide the number by 2 and multiplies the result by 3, as long as the number is even. Odd numbers return themselves.
**32 byte alt:**
```
lambda n:n*(n&-n)**0.58496250072
```
[Try it online](https://tio.run/nexus/python2#S7ON@Z@TmJuUkqiQZ5WnpZGnppunqaVloGdqYWJpZmRqYGBu9D8tv0ghTyEzT6EoMS89VcNQx9hA04qLs6AoM69EIU0jT/M/AA "Python 2 ‚Äì TIO Nexus"). Has some float error. The constant is `log_2(3)-1`.
Uses `(n&-n)` to find the greatest power-of-2 factor of `n`, the converts `3**k` to `2**k` by raising it to the power of `log_2(3)-1`.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
```
√í1~P
```
[Try it online!](https://tio.run/nexus/05ab1e#M7L0Kav8f3iSYV3A/0qlw/utFA7vV9L5DwA "05AB1E ‚Äì TIO Nexus")
### How it works
```
√í     Compute the prime factorization of the input.
 1~   Perform bitwise OR with 1, making the only even prime (2) odd (3).
   P  Take the product of the result.
```
[Answer]
## Haskell, ~~24~~ 23 bytes
```
until odd$(*3).(`div`2)
```
The divide by two and multiply by 3 until odd trick in Haskell.
[Try it online!](https://tio.run/nexus/haskell#@5@bmJmnYKtQUJSZV6KgopCbWKCgoVCaV5KZo5CfkqKioWWsqaeRkJJZlmCkqaCpEG2op2dkGfv/PwA "Haskell ‚Äì TIO Nexus")
Alternative with a lambda instead of a pointfree function and with the same byte count:
```
odd`until`\x->div(x*3)2
```
Edit: @ais523 saved a byte in the original version and @√òrjan Johansen one in the alternative version, so both version have still the same length. Thanks!
[Answer]
## JavaScript (ES6), 19 bytes
```
f=x=>x%2?x:f(x*1.5)
```
While the input is divisible by two, multiplies it by 1.5, which is equivalent to dividing by 2 and multiplying by 3.
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 76 bytes
```
{{({}[()]<([({})]()<({}{})>)>)}{}([{}]()){{}((({})){}{})<>}<>}<>({}({}){}())
```
[Try it online!](https://tio.run/nexus/brain-flak#HYrBDQAwCALXgX@fxkVMJyHObrHRyOVkJKgLvIEy8YLhNKXHiVJbUkZsg/8b2X9tVvqSM@cB "Brain-Flak ‚Äì TIO Nexus")
## Explanation
This program works by dividing the number by two and tripling until it gets a remainder of one from the division. Then it stops looping and doubles and adds one to the final number.
### More detailed explanation eventually...
[Answer]
# Mathematica, ~~22~~ 19 bytes
*Thanks to lanlock4 for saving 3 bytes!*
```
#//.x_?EvenQ:>3x/2&
```
Pure function that does the replacement repeatedly, one factor of 2 at a time. Works on all positive integers less than 265537.
[Answer]
# [Perl 6](https://perl6.org), 14 bytes
```
{1.5**.lsb*$_}
```
[lsb](https://docs.perl6.org/routine/lsb) returns the position of the least-significant bit, counted from the right. That is, how many trailing zeroes in the binary representation, which is the same as the number of factors of 2. So raise 3/2 to that power and we're done.
```
say {$_*1.5**.lsb}(24);
> 81
```
[Answer]
# [Lean](https://leanprover.github.io/), ~~139~~ ~~82~~ 71 bytes
*11 bytes saved by ovs.*
```
import data.nat.prime
open list
Œªx,prod$map(Œªp,p+1-p%2)$nat.factors x
```
[Try it online!](https://tio.run/##y0lNzPv/PzO3IL@oRCElsSRRLy@xRK@gKDM3lSu/IDVPISezuIQrJTVNIc3K9tzuCp2CovwUldzEAo1zuwt0CrQNdQtUjTRVQJrSEpNL8ouKFSr@K6eWJeYoaKQpGGlywdkmSGwjAySOpbnmfwA "Lean ‚Äì Try It Online")
I'm really new to lean so, this answer is probably really inefficient in ways I don't yet know. But it works.
We get a list of factors with `nat.factors` replace `2`s with threes using a `map` and roll it back up with `list.prod`.
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~7~~, 6 bytes
```
Yf1Z|p
```
[Try it online!](https://tio.run/nexus/matl#@x@ZZhhVU/D/v5EJAA "MATL ‚Äì TIO Nexus")
*[1 byte saved thanks to Dennis's genius observation](https://codegolf.stackexchange.com/a/119003/31716)*
---
The best way to explain this is to show the stack at various points.
```
Yf  % Prime factors
[2 2 2 3]
1Z| % Bitwise OR with 1
[3 3 3 3]
p   % Product
81
```
Alternate solution:
```
Yfto~+p
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~6~~ 5 bytes
Saved a byte thanks to *Adnan*.
```
√íD√à+P
```
[Try it online!](https://tio.run/nexus/05ab1e#M7L0Kav8f3iSy@EO7YD/lUqH91spHN6vpPMfAA "05AB1E ‚Äì TIO Nexus")
**Explanation**
```
√í       # push list of prime factors of input
 D      # duplicate
  √à     # check each factor for evenness (1 if true, else 0)
   +    # add list of factors and list of comparison results
    P   # product
```
[Answer]
# [Java](http://openjdk.java.net/), 38 bytes
```
int f(int n){return n%2>0?n:f(n/2*3);}
```
[Try it online!](https://tio.run/nexus/java-openjdk#JY5BC4JAFITP@iseQrDbklne2rJf0MljdNhM5YE@ZfdZhPjbbTUGZmDgG6Yfng0WUDTGObgZpHFGYqjE4iRHW/JgCWhzzJIrnSpB@@M2lXqa@z/p2LCPd4cvaD0vcrZI9f1hbO3kGAZVZ9cxvBw0ntNEo1JLH@Rfx2UbdwPHvUe4IYEqgl0GkaLys74RMvZXpNRhMIVe8w8)
---
Previous 43-byte solution:
```
int f(int n){for(;n%2<1;)n=n/2*3;return n;}
```
[Try it online!](https://tio.run/nexus/java-openjdk#JY5BC4JAFITP@iseQrDbkmUeV/sHnTxGh81UHuhTdp9FiL/ddouBGRiYj5nmR4811L1xDq4GadmQGFoRnOTSjlZo2p2LTEsq6Xje59o2PFsC0us2/eeODft4jfiEwUNExRapu92N7Zxc4ihgAhHLTGORnzQqFfqo@jhuhnScOZ38hHsSqBI4XCBR1Lx/l4RM/R8pdRytsdf2BQ)
[Answer]
## [Alice](https://github.com/m-ender/alice), 9 bytes
```
2/S 3
o@i
```
[Try it online!](https://tio.run/nexus/alice#@2@kH6xgzJXvkPn/v6ERAA "Alice ‚Äì TIO Nexus")
Alice has a built-in to replace a divisor of a number with another. I didn't think I'd actually get to make use of it so soon...
Using the code points of characters for I/O, this becomes 6 bytes: `I23SO@`.
### Explanation
```
2   Push 2 (irrelevant).
/   Reflect to SE. Switch to Ordinal.
i   Read all input as a string.
    The IP bounces up and down, hits the bottom right corner and turns around,
    bounces down again.
i   Try to read more input, but we're at EOF so this pushes an empty string.
/   Reflect to W. Switch to Cardinal.
2   Push 2.
    The IP wraps around to the last column.
3   Push 3.
S   Implicitly discard the empty string and convert the input string to the integer
    value it contains. Then replace the divisor 2 with the divisor 3 in the input.
    This works by multiplying the value by 3/2 as long as it's divisible by 2.
/   Reflect to NW. Switch to Ordinal.
    Immediately bounce off the top boundary. Move SW.   
o   Implicitly convert the result to a string and print it.
    Bounce off the bottom left corner. Move NE.
/   Reflect to S. Switch to Cardinal.
@   Terminate the program.
```
[Answer]
# [Hexagony](https://github.com/m-ender/hexagony), ~~112~~ 91 bytes
Grid size 6 (91 bytes)
```
      ? { 2 . . <
     / = { * = \ "
    . & . . . { _ '
   . . { . . } ' * 2
  _ } { / . } & . ! "
 . > % . . < | . . @ |
  \ . . \ . | ~ . . 3
   . . " . } . " . "
    . & . \ = & / 1
     \ = { : = } .
      [ = { { { <
```
Compact version
```
?{2..</={*=\".&...{_'..{..}'*2_}{/.}&.!".>%..<|..@|\..\.|~..3..".}.".".&.\=&/1\={:=}.[={{{<
```
Grid size 7 (112 bytes)
```
       ? { 2 " ' 2 <
      / { * \ / : \ "
     . = . = . = | . 3
    / & / { . . } { . "
   . > $ } { { } / = . 1
  _ . . } . . _ . . & . {
 . > % < . . ~ & . . " . |
  | . . } - " . ' . @ | {
   . . . = & . . * ! . {
    . . . _ . . . _ . =
     > 1 . . . . . < [
      . . . . . . . .
       . . . . . . .
```
[Try it online!](https://tio.run/##TY/BDoIwEETv/YqRKCQkloAXYgr6H5IQDkZPeoW0@ut12G2IbXa2ebuzbZ/3eXq8X0uM0HWBR4MMBdUZZRVZiYH5TM2UWnRbBOrJaGsu7Zb7I1naLXrsBXhqJbaahTE1rqrnnOGNGA5wgr4COYkRaArJdBRSMK5kXu@x8iA1lNjptK0y/uVO/9GjTnDdDrcYm/YH "Hexagony ‚Äì Try It Online")
Compact Version:
```
?{2"'2</{*\/:\".=.=.=|.3/&/{..}{.".>$}{{}/=.1_..}.._..&.{.>%<..~&..".||..}-".'.@|{...=&..*!.{..._..._.=>1.....<[
```
Ungolfed Version for better readability:
[![Ungolfed](https://i.stack.imgur.com/p6Umkm.png)](https://i.stack.imgur.com/p6Umk.png)
Approximate Memory Layout
[![enter image description here](https://i.stack.imgur.com/zT808.png)](https://i.stack.imgur.com/zT808.png)
Grey Path (Memory initialization)
```
?     Read input as integer (Input)
{     Move to memory edge "Divisor left"
2     Set current memory edge to 2 
" '   Move to memory edge "Divisor right" 
2     Set current memory edge to 2
"     Move to memory edge "Multiplier" 
3     Set current memory edge to 3
"     Move to memory edge "Temp 2" 
1     Set current memory edge to 1 
{ { { Move to memory edge "Modulo"
=     Turn memory pointer around 
[     Continue with next instruction pointer
```
Loop entry
```
%     Set "Modulo" to Input mod Divisor
<     Branch depending on result
```
Green Path (value is still divisible by 2)
```
} } {     Move to memory edge "Result"
=         Turn memory pointer around 
*         Set "Result" to "Temp 2" times "Multiplier" (3) 
{ = &     Copy "Result" into "Temp2" 
{ { } } = Move to "Temp"
:         Set "Temp" to Input / Divisor (2)
{ = &     Copy "Temp" back to "Input"
"         Move back to "Modulo"
```
Red Path (value is no longer divisible by 2)
```
} = & " ~ & ' Drag what's left of "Input" along to "Multiplier"
*             Multiply "Multiplier" with "Temp 2"
! @           Output result, exit program
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~8~~ 5 bytes
```
√Üf3¬ªP
√Üf3¬ªP  Main Link, argument is z
√Üf     Prime factors
  3¬ª   Takes maximum of 3 and the value for each value in the array
    P  Takes the product of the whole thing
```
[Try it online!](https://tio.run/nexus/jelly#@3@4Lc340O6A////G1kAAA "Jelly ‚Äì TIO Nexus")
-3 bytes thanks to a hint from @Dennis!
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 7 bytes
```
~√ó‚ÇÇ√ó‚ÇÉ‚Ü∞|
```
[Try it online!](https://tio.run/nexus/brachylog2#@193ePqjpiYQ0fyobUPN//@GRv@jAA "Brachylog ‚Äì TIO Nexus")
## How it works
```
~√ó‚ÇÇ√ó‚ÇÉ‚Ü∞|      original program
?~√ó‚ÇÇ√ó‚ÇÉ‚Ü∞.|?.  with implicit input (?) and output (.) added
?~√ó‚ÇÇ         input "un-multiplied" by 2
    √ó‚ÇÉ       multiplied by 3
      ‚Ü∞      recursion
       .     is the output
        |    or (in case the above fails, meaning that the input
                 cannot be "un-multiplied" by 2)
         ?.  the input is the output
```
[Answer]
# [Hexagony](https://github.com/m-ender/hexagony), ~~28 27~~ 26 bytes
```
?'2{{(\}}>='!:@=$\%<..>)"*
```
[Try it online!](https://tio.run/##y0itSEzPz6v8/99e3ai6WiOmttbOVl3RysFWJUbVRk/PTlNJ6/9/IwsA "Hexagony ‚Äì Try It Online")
Laid out:
```
    ? ' 2 {
   { ( \ } }
  > = ' ! : @
 = $ \ % < . .
  > ) " * . .
   . . . . .
    . . . .
```
This basically runs:
```
num = input()
while num%2 == 0:
    num = (num/2)*3
print num
```
At this point it's an exercise on how torturous I can get the loop path to minimise bytes.
[Answer]
# [Retina](https://github.com/m-ender/retina), 23 bytes
```
.+
$*
+`^(1+)\1$
$&$1
1
```
[Try it online!](https://tio.run/nexus/retina#4zI2UNHi0uM6tE1FL4HLyDGBS1XDPeG/njYXUFg7IU7DUFszxlCFS0VNxZDL8P9/AA "Retina ‚Äì TIO Nexus") Link is to test suite that runs automatically runs all inputs from 1 to 29. Explanation:
```
.+
$*
```
Convert to unary.
```
+`^(1+)\1$
$&$1
```
While the value is even, multiply it by `3/2`.
```
1
```
Convert to decimal.
Note that any FRACTRAN program is trivially convertible to a Retina program with unary I/O. The fraction itself is converted into a match for a string that appears an exact number of times given by the denominator, and the substitution is then `$1` repeated a number of times given by the numerator, although if this is greater than the denominator then `$&` can be used to golf down the code length. It then remains to loop each fraction back to the start of the script, so that the earliest possible fraction is always the next to be substituted. In the case of a single fraction no group is required of course.
[Answer]
# Pyth - ~~14~~ ~~10~~ 9 bytes
```
*^1.5/PQ2
```
Counts the number of 2s in the prime factorization (/PQ2). Multiplies the input by 1.5^(# of 2s)
[Try it](http://pyth.herokuapp.com/?code=%2a%5E1.5%2FPQ2&input=12&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A10%0A11&debug=0)
[Answer]
# [Risky](https://github.com/Radvylf/risky), 9 bytes
```
*\?+0+0+0__?\?+?:2
```
[Try it online!](https://radvylf.github.io/risky?p=WyIqXFw_KzArMCswX18_XFw_Kz86MiIsIlsyOF0iLDBd)
[Answer]
# [BQN (CBQN)](https://mlochbaum.github.io/BQN/), 30 bytes
```
{0=2|ùï©?3√óùïäùï©√∑2;ùï©}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m704qTBvwfpH81ekPZq_UuFR2wQFIHtpaUmarsW-agNbo5oPc6eutDc-PB1Id4HYh7cbWYPoWoiibUBtXI8aFgVn5JcruB1aYaj9qG2qkSVEcsECCA0A)
[Answer]
# [J](http://jsoftware.com/), 11 bytes
```
[:*/q:+2=q:
```
[Try it online!](https://tio.run/nexus/j#@59maxVtpaVfaKVtZFto9f9/mpJBrKF2pp6RpYKfk55CYkFBTqVCmkJ@nkJqYnKGQn4aCJVkpCrkleYmpRYVK6QV5ecqGCqU5CsYWQIA "J ‚Äì TIO Nexus")
`[:` cap (placeholder to call the next verb monadically)
`*/` the product of
`q:` the prime factors
`+` plus (i.e. with one added where)
`2` two
`=` is equal to (each of)
`q:` the prime factors
[Answer]
# [J](http://jsoftware.com/), ~~15~~ ~~12~~ 10 bytes
```
(+2&=)&.q:
```
[Try it online!](https://tio.run/nexus/j#NYyxCoMwFEX3fMUlgzEkPq1bX4mL0KlT104SYo1DpE3/PxXE4cI5w7mSoGY4hoJFB97XEManfdxLbfrK6Yo@XLQQfsohwxEuMIjUX0Xwy4YBtUIz7P26xZR/35jeeDEkI0x@wa3VJ@C4sIRZmkPKHw "J ‚Äì TIO Nexus") Works similar to below, just has different logic concerning replacement of `2` with `3`.
# 15 bytes
```
(2&={,&3)"+&.q:
```
[Try it online!](https://tio.run/nexus/j#NYyxDoIwGAb3PsWXDi1Ny6/C5G/KYuLk5OpEmiJ1gEDdfPhKQhwuuVtOEvQAz9BwOII3asL14e63UjXKf51qjbSKFi5GiNDnmOEJJ1gkas4ihnFGh0qj7rbJe05T/qxpeuHJkIzYhxGXg/kL9oUjDNLuUX4)
## Explanation
```
(2&={,&3)"+&.q:
           &.    "under"; applies the verb on the right, then the left,
                 then the inverse of the right
             q:  prime factors
(       )"+      apply inside on each factor
     ,&3         pair with three: [factor, 3]
 2&=             equality with two: factor == 2
    {            list selection: [factor, 3][factor == 2]
                 gives us 3 for 2 and factor for anything else
           &.q:  under prime factor
```
[Answer]
# PHP, 36 Bytes
```
for($a=$argn;$a%2<1;)$a*=3/2;echo$a;
```
[Try it online!](https://tio.run/nexus/php#s7EvyCjgUkksSs@ztbD@n5ZfpKGSaAvmW6skqhrZGFprqiRq2RrrG1mnJmfkqyRa//8PAA "PHP ‚Äì TIO Nexus")
[Answer]
## CJam, ~~10~~ 9 bytes
```
rimf1f|:*
```
Really simple.
Explanation:
```
ri  e# Read integer:         | 28
mf  e# Prime factors:        | [2 2 7]
1   e# Push 1:               | [2 2 7] 1
f|  e# Bitwise OR with each: | [3 3 7]
:*  e# Product:              | 63
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 7 bytes
```
k mw3 √ó
```
[Try it online!](https://tio.run/nexus/japt#@5@tkFturHB4@v//hiYA "Japt ‚Äì TIO Nexus")
### Explanation
```
k mw3 √ó
k        // Factorize the input.
  mw3    // Map each item X by taking max(X, 3).
      √ó  // Take the product of the resulting array.
         // Implicit: output result of last expression
```
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 9 bytes
Integer output \o/
```
*F+1.|R1P
```
[Test suite](http://pyth.herokuapp.com/?code=%2aF%2B1.%7CR1P&input=12&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A10%0A11&debug=0).
## How it works
```
*F+1.|R1P
        P   prime factorization
    .|R1    bitwise OR each element with 1
*F+1        product
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 26 bytes
```
{‚çµ(‚ä£√ó(3*‚ä¢)√ó(√∑2*‚ä¢))2‚çü‚çµ‚à®2*‚çµ}
```
[Try it online!](https://tio.run/nexus/apl-dyalog#RY87DsJADERrOEXKhArP5lvmXIgWoUgr0VEhJGi4QJQD5Ca@SLAnyqZ7sj3z5KXX2@Oiccx1@M7PPJx0@BQG8wRiAY1v2@v9Z4M4Xpelz@Sg8SXHPoNDMAgblA6dQeVQGdTbpHFoDFoHOHXbTs6s9HtZ271ekC6FBnGF0AEeVHuMnpZTmoSx3SWUidtAW@kxSCrD@o0rQBuIZeoFbWCMtsCyJilAW80Ybej@ "APL (Dyalog Unicode) ‚Äì TIO Nexus")
This is too verbose, I must be doing something wrong...
[Answer]
# R, 42 bytes
The only right amount of bytes in an answer.
```
x=gmp::factorize(scan());x[x==2]=3;prod(x)
```
Pretty straightforward, uses the `gmp` package to factorize `x`, replaces 2s by 3s, and returns the product.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~19 16 10 9~~ 7 bytes
```
k ¬Æw3√É√ó
```
[Try it online!](https://tio.run/nexus/japt#@5@tcGhdufHh5sPT//83NAAA "Japt ‚Äì TIO Nexus")
# Explanation
```
 k ¬Æ   w3√É √ó
Uk mZ{Zw3} r*1
U              # (input)
 k m           # for every prime factor
    Z{Zw3}     # replace it by the maximum of itself and 3
           r*1 # output the product
```
]