Die Herausforderung
Bei einer Ganzzahleingabe von x
where 1 <= x <= 255
werden die Ergebnisse von Zweierpotenzen zurückgegeben, die bei Summierung ergeben x
.
Beispiele
Angesichts der Eingabe:
86
Ihr Programm sollte folgendes ausgeben:
64 16 4 2
Eingang:
240
Ausgabe:
128 64 32 16
Eingang:
1
Ausgabe:
1
Eingang:
64
Ausgabe:
64
Der Ausgang kann Nullen enthalten, wenn die bestimmte Zweierpotenz nicht in der Summe vorhanden ist.
Zum Beispiel Eingabe 65
ausgegeben werden 0 64 0 0 0 0 0 1
.
Wertung
Das ist Code-Golf , also gewinnt die kürzeste Antwort in jeder Sprache.
code-golf
binary
code-golf
sequence
integer
chess
code-golf
number
arithmetic
matrix
code-golf
code-golf
combinatorics
grid
set-partitions
code-golf
array-manipulation
graph-theory
code-golf
number
code-golf
string
decision-problem
code-golf
matrix
cellular-automata
3d
code-challenge
restricted-source
printable-ascii
code-golf
board-game
code-golf
geometry
grid
code-golf
word-puzzle
code-golf
matrix
sorting
code-golf
code-golf
string
decision-problem
code-golf
matrix
cellular-automata
code-golf
decision-problem
code-golf
math
number
arithmetic
restricted-source
code-golf
code-golf
number
integer
matrix
code-golf
date
code-golf
matrix
code-golf
sequence
combinatorics
chemistry
code-golf
array-manipulation
popularity-contest
code-golf
code-golf
natural-language
code-golf
number
integer
sorting
substitution
code-golf
string
number
date
encode
code-golf
decision-problem
code-golf
string
subsequence
code-golf
string
alphabet
code-golf
SpookyGengar
quelle
quelle
Antworten:
JavaScript (ES6), 28 Byte
Probieren Sie es online!
quelle
f=n=>n&&f(n&~-n)+[,n&-n]
.Pure Bash, 20
Try it online!
Erläuterung
quelle
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Try it online!
How?
quelle
Jelly, 6 bytes
Try it online!
Explanation
BUT here is an explanation (note: I had assumed that we may only output the powers of 2 themselves and nothing else):
UT
. Now that we've found the correct indices all we have to do is raisequelle
’
there...BUT2*H
would work though.Python, 35 bytes
Little-endian with zeros at unused powers of 2.
Try it online!
quelle
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (
⎕IO←0
).Try it online!
2
two*
raised to the power of⍸
the ɩndices where true⍢
while⌽
reversed⍤
of⊤
the binary representationquelle
Sledgehammer 0.2, 3 bytes
Decompresses into
{intLiteral[2],call[NumberExpand,2]}
.Sledgehammer is a compressor for Wolfram Language code using Braille as a code page. The actual size of the above is 2.75 bytes, but due to current rules on meta, padding to the nearest byte is counted in code size.
quelle
05AB1E, 3 bytes
Port of @JonathanAllan's Jelly answer, so make sure to upvote him!
Contains zeros (including -loads of- trailing zeros).
Try it online or verify all test cases.
Explanation:
quelle
bitwise and
used in osabie. Nice one.&
in. xD I have used Bitwise-XOR a couple of times, like here or here and Bitwise-NOT once here (which I later removed again after golfing further..). I do use Bitwise-AND, XOR, OR, NOT, SHIFT, etc. pretty often in Java, but in 05AB1E not so much. :)Catholicon, 3 bytes
Try it online!
Explanation:
quelle
Wolfram Language (Mathematica), 17 bytes
Try it online!
Mathematica strikes again.
quelle
R,
2723 bytesTry it online!
Unrolled code and explanation :
quelle
C# (Visual C# Interactive Compiler), 29 bytes
Contains 5 unprintable characters.
Explanation
Try it online!
quelle
n=>new int[8].Select((j,i)=>1<<i&n).Where(i=>i!=0)
The part beforeWhere
is five bytes shorter btwThe output may contain zeros
n=>new int[8].Select((j,i)=>1<<i&n)
is 35 bytes long and we won't need additional flags and text encodings.n=>"INSERT ASCII HERE".Select(a=>1<<a&n)
But I'm on a mobile device that can't display or type unprintables, so I'll have to wait till I get home to update the answerC# (Visual C# Interactive Compiler), 38 bytes
Try it online!
quelle
1
,2
,4
,8
,16
, etc. (thex>y
should bex>=y
instead)./u:System.Linq.Enumerable
and try this for 31 bytes/u:System.Linq.Enumerable
" :PC (gcc), 39 bytes
Try it online!
quelle
05AB1E, 7 bytes
explanation:
Try it online!
quelle
Haskell, 29 bytes
Try it online!
quelle
Ruby, 25 bytes
Try it online!
quelle
C (clang),
1331106358 bytes58-byte solution thanks to @ceilingcat.
Try it online!
quelle
main(){}
and the return type defaults to int. Same for variables at global scope. Also, at least on normal implementations like clang, printf and scanf work without prototypes. You get warnings of course, but it's still valid C89 (maybe) or at least K&R C for them to be implicitly declared. The types of the C objects you pass as args defines how they're passed, so achar*
andint*
will Just Work without truncating pointers to 32-bit on x86-64 or anything. (Default argument promotions happen, same as for variadic functions which they are anyway.)&
to check if a bit is set. Likey&(1<<x)&&printf("%d ",1<<x);
. Or to not skip zeros, justprintf("%d ", y&(1<<x))
. Or instead of counting bit positions, usex=256
andx>>=1
to shift the mask.main(y){int x=256;for(scanf("%d",&y);x>>=1;)printf("%d ",y&x);}
63 bytes Try it online! clang will even compile that with-std=c11
MATL, 5 bytes
Try it online!
Explanation
Consider input
86
as an example.quelle
Perl 6,
1612 bytes-4 bytes thanks to Jonathan Allan
Try it online!
Returns an All Junction with 8 elements. This is a rather non-standard way of returning, but generally, Junctions can act as ordered (at least until autothreading is implemented) lists and it is possible to extract the values from one.
Explanation:
quelle
Japt,
85 bytesTry it
Alternative
Suggested by Oliver to avoid the
0
s in the output using the-mf
flag.Try it
quelle
N&2pU
with-mf
to avoid the0
s05AB1E, 9 bytes
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
Try it online!
quelle
15
, instead of[1,2,4,8]
2**0
, nice catch.Ý
overL
.L
instead ofÝ
at first in my answer.Julia 0.6, 13 bytes
Try it online!
quelle
CJam, 12 bytes
Try it online!
quelle
K (oK),
1916 bytes-3 bytes thanks to ngn!
Try it online!
oK does not have
power
operator, that's why I need a helper function{*/x#2}
(copy 2x
times and reduce the resulting list by multiplication)quelle
{
x}
Alchemist, 125 bytes
Try it online! or Test every input!
Explanation
quelle
PHP,
4139 bytesTry it online!
Or 38 with no fun
>>=
operator and PHP 5.6+:Or 36 with little-endian ("0 2 4 0 16 0 64 0") output:
Really I just wanted to use the
>>=
operator, so I'm sticking with the 39.Tests:
quelle
TSQL,
4339 bytesCan't find a shorter fancy solution, so here is a standard loop. -4 bytes thanks to MickyT and KirillL
Try it out
quelle
,@ int=128s:print @y&@ set @/=2IF @>0GOTO s
. This is hinted by @KirillL for the R answerPython 2,
4340 bytesTry it online!
quelle
C# (Visual C# Interactive Compiler), 33 bytes
Port of @Arnauld's JavaScript (ES6) answer, so make sure to upvote him!
Try it online.
Explanation:
quelle