Étant donné un entier positif n écrit un code pour prendre sa factorisation première et remplacer tous ses facteurs de 2
avec 3
.
Par exemple
12 = 2 * 2 * 3 -> 3 * 3 * 3 = 27
C’est du code-golf, donc l’objectif est de minimiser le nombre d’octets de votre réponse.
Cas de test
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
la source
Python 2 , 28 octets
Essayez-le en ligne!
Divisez récursivement le nombre par 2 et multipliez le résultat par 3, tant que le nombre est pair. Les nombres impairs reviennent eux-mêmes.
32 octets alt:
Essayez-le en ligne . A une erreur de flottement. La constante est
log_2(3)-1
.Utilise
(n&-n)
pour trouver le plus grand facteur de puissance de 2n
, les convertis3**k
à2**k
en l' élevant à la puissancelog_2(3)-1
.la source
05AB1E , 4 octets
Essayez-le en ligne!
Comment ça marche
la source
Haskell,
2423 octetsLa division par deux et multiplier par 3 jusqu'à astuce étrange dans Haskell.
Essayez-le en ligne!
Alternative avec une fonction lambda au lieu d'une fonction sans points et avec le même nombre d'octets:
Edit: @ ais523 a enregistré un octet dans la version originale et @ Ørjan Johansen dans la version alternative, les deux versions ont donc toujours la même longueur. Merci!
la source
odd`until`\x->div(x*3)2
.$
pour remplacer une paire de parenthèses: Essayez-le en ligne!()
la version lambdaJavaScript (ES6), 19 octets
Alors que l’entrée est divisible par deux, multipliez-la par 1,5, ce qui équivaut à la division par 2 et à la multiplication par 3.
la source
x*3/2
has the same bytecountf=
is usally not needed for js.f(x*1.5)
it needs to have the namef
, hence why thef=
is included.Brain-Flak, 76 bytes
Try it online!
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...
la source
Mathematica,
2219 bytesThanks to lanlock4 for saving 3 bytes!
Pure function that does the replacement repeatedly, one factor of 2 at a time. Works on all positive integers less than 265537.
la source
x_?EvenQ
work instead ofx_/;EvenQ@x
?MATL,
7, 6 bytesTry it online!
1 byte saved thanks to Dennis's genius observation
The best way to explain this is to show the stack at various points.
Alternate solution:
la source
Yf3H$X>p
for 8 bytes05AB1E,
65 bytesSaved a byte thanks to Adnan.
Try it online!
Explanation
la source
ÒDÈ+P
should save a byteAlice, 9 bytes
Try it online!
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
la source
Jelly,
85 bytesTry it online!
-3 bytes thanks to a hint from @Dennis!
la source
Pyth -
14109 bytesCounts the number of 2s in the prime factorization (/PQ2). Multiplies the input by 1.5^(# of 2s)
Try it
la source
Java, 38 bytes
Try it online!
Previous 43-byte solution:
Try it online!
la source
Hexagony,
11291 bytesGrid size 6 (91 bytes)
Compact version
Grid size 7 (112 bytes)
Try it online!
Compact Version:
Ungolfed Version for better readability:
Approximate Memory Layout
Grey Path (Memory initialization)
Loop entry
Green Path (value is still divisible by 2)
Red Path (value is no longer divisible by 2)
la source
%2
and:2
both into the "modulo" edge? (So you can just get rid of the top two edges.) And then, could you attach the "multiplier" branch onto the "modulo" edge instead of the "divisor" edge so you need less movement after each branch? (You could possibly even rotate that section, so that "result" or "temp 2" touches "modulo" which would mean you only need to copy the final result once before being able to compute the product.)Retina, 23 bytes
Try it online!
la source
Brachylog, 7 bytes
Try it online!
How it works
la source
J, 11 bytes
Try it online!
[:
cap (placeholder to call the next verb monadically)*/
the product ofq:
the prime factors+
plus (i.e. with one added where)2
two=
is equal to (each of)q:
the prime factorsla source
[:
disgusting.J,
151210 bytesTry it online! Works similar to below, just has different logic concerning replacement of
2
with3
.15 bytes
Try it online!
Explanation
la source
roll
here. :)Pyth, 9 bytes
Integer output \o/
Test suite.
How it works
la source
Japt,
19 16 10 97 bytesTry it online!
Explanation
la source
×
is a shortcut forr@X*Y}1
(or justr*1
), which might come in handy. There's alsoXwY
which isMath.max(X,Y)
.k m_w3Ã×
to save a byte. Also,m_
can be shortened to®
.PHP, 36 Bytes
Try it online!
la source
for($a=$argn;!1&$a;)$a*=3/2;echo$a;
renaming$argn
saves a single byte.CJam,
109 bytesReally simple.
Explanation:
la source
Hexagony,
28 2726 bytesTry it online!
Laid out:
This basically runs:
At this point it's an exercise on how torturous I can get the loop path to minimise bytes.
la source
Japt, 7 bytes
Try it online!
Explanation
la source
APL (Dyalog Unicode), 26 bytes
Try it online!
This is too verbose, I must be doing something wrong...
la source
R, 42 bytes
The only right amount of bytes in an answer.
Pretty straightforward, uses the
gmp
package to factorizex
, replaces 2s by 3s, and returns the product.la source
Befunge-93, 20 bytes
Try it online!
la source
Perl 6, 14 bytes
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.
la source
Pyke, 5 bytes
Try it online!
la source
Actually, 9 bytes
Try it online!
Explanation:
la source