Sum of Powers of 2
$begingroup$
The Challenge
Given an integer input x where 1 <= x <= 255, return the results of powers of two that when summed give x.
Examples
Given the input:
86
Your program should output:
64 16 4 2
Input:
240
Output:
128 64 32 16
Input:
1
Output:
1
Input:
64
Output:
64
The output may contain zeros if the certain power of two is not present in the sum.
For example, input 65 may output 0 64 0 0 0 0 0 1.
Scoring
This is code-golf, so the shortest answer in each language wins.
code-golf binary
$endgroup$
|
show 3 more comments
$begingroup$
The Challenge
Given an integer input x where 1 <= x <= 255, return the results of powers of two that when summed give x.
Examples
Given the input:
86
Your program should output:
64 16 4 2
Input:
240
Output:
128 64 32 16
Input:
1
Output:
1
Input:
64
Output:
64
The output may contain zeros if the certain power of two is not present in the sum.
For example, input 65 may output 0 64 0 0 0 0 0 1.
Scoring
This is code-golf, so the shortest answer in each language wins.
code-golf binary
$endgroup$
3
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
4 hours ago
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
4 hours ago
$begingroup$
@Adám going to say yes this time. I want the output to be somewhat similar to the binary representation of the number. ie 65 = 0 + 2^6 +0 +0 +0 +0 + 0 +2^0
$endgroup$
– SpookyGengar
4 hours ago
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
4 hours ago
2
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
3 hours ago
|
show 3 more comments
$begingroup$
The Challenge
Given an integer input x where 1 <= x <= 255, return the results of powers of two that when summed give x.
Examples
Given the input:
86
Your program should output:
64 16 4 2
Input:
240
Output:
128 64 32 16
Input:
1
Output:
1
Input:
64
Output:
64
The output may contain zeros if the certain power of two is not present in the sum.
For example, input 65 may output 0 64 0 0 0 0 0 1.
Scoring
This is code-golf, so the shortest answer in each language wins.
code-golf binary
$endgroup$
The Challenge
Given an integer input x where 1 <= x <= 255, return the results of powers of two that when summed give x.
Examples
Given the input:
86
Your program should output:
64 16 4 2
Input:
240
Output:
128 64 32 16
Input:
1
Output:
1
Input:
64
Output:
64
The output may contain zeros if the certain power of two is not present in the sum.
For example, input 65 may output 0 64 0 0 0 0 0 1.
Scoring
This is code-golf, so the shortest answer in each language wins.
code-golf binary
code-golf binary
edited 3 hours ago
SpookyGengar
asked 5 hours ago
SpookyGengarSpookyGengar
722617
722617
3
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
4 hours ago
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
4 hours ago
$begingroup$
@Adám going to say yes this time. I want the output to be somewhat similar to the binary representation of the number. ie 65 = 0 + 2^6 +0 +0 +0 +0 + 0 +2^0
$endgroup$
– SpookyGengar
4 hours ago
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
4 hours ago
2
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
3 hours ago
|
show 3 more comments
3
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
4 hours ago
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
4 hours ago
$begingroup$
@Adám going to say yes this time. I want the output to be somewhat similar to the binary representation of the number. ie 65 = 0 + 2^6 +0 +0 +0 +0 + 0 +2^0
$endgroup$
– SpookyGengar
4 hours ago
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
4 hours ago
2
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
3 hours ago
3
3
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
4 hours ago
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
4 hours ago
2
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
4 hours ago
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
4 hours ago
$begingroup$
@Adám going to say yes this time. I want the output to be somewhat similar to the binary representation of the number. ie 65 = 0 + 2^6 +0 +0 +0 +0 + 0 +2^0
$endgroup$
– SpookyGengar
4 hours ago
$begingroup$
@Adám going to say yes this time. I want the output to be somewhat similar to the binary representation of the number. ie 65 = 0 + 2^6 +0 +0 +0 +0 + 0 +2^0
$endgroup$
– SpookyGengar
4 hours ago
4
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
4 hours ago
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
4 hours ago
2
2
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
3 hours ago
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
3 hours ago
|
show 3 more comments
19 Answers
19
active
oldest
votes
$begingroup$
Jelly, 6 bytes
BUT’2*
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):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
$endgroup$
1
$begingroup$
"ASCII-only" Sneaky’there...
$endgroup$
– Erik the Outgolfer
4 hours ago
$begingroup$
@EriktheOutgolfer I guessBUT2*Hwould work though.
$endgroup$
– Mr. Xcoder
4 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
$endgroup$
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
1 hour ago
add a comment |
$begingroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
$endgroup$
add a comment |
$begingroup$
PowerShell, 45 bytes
param($a)7..0|%{'2*'*$_+'1'|iex}|?{$_-band$a}
Try it online!
Takes input $a, loops from 7 to 0, each iteration performing a string-multiplication of 2*, so for example for 3 this would be 2*2*2*, plus a 1 tacked on the end, so 2*2*2*1. That's piped to iex (short for Invoke-Expression and similar to eval) to formulate the powers-of-two. We then pull out those ? (where) the number $_ shares a value -binaryand with the input number.
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0).
2*⍸⍢⌽⍤⊤
Try it online!
2 two* raised to the power of⍸ the ɩndices where true⍢ while⌽ reversed⍤ of⊤ the binary representation
$endgroup$
add a comment |
$begingroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
$endgroup$
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
3 hours ago
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Catholicon, 3 bytes
ṫĊŻ
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
$endgroup$
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Wolfram Language 58 bytes
Flatten[2^#&/@(Position[Reverse@IntegerDigits[#,2],1]-1)]&
IntegerDigits[#,2] returns the binary representation of the input, #, as a list of 1's and 0's.
Reverse@ reverses that list.
(Position[...,1]-1) lists the positions of the 1's in the reversed list and decrements each position by 1.
2^#&/@ 2 is raised to the power of (position -1) for each integer in the list.
Flatten removes nested braces.
$endgroup$
$begingroup$
The sort-order restriction has been lifted (also you may have zeros in place of unused powers of 2 now, if that helps)
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
@JonathanAllan, Thanks, it saves 6 bytes. There are no zeros generated by this approach.
$endgroup$
– DavidC
2 hours ago
add a comment |
$begingroup$
Retina 0.8.2, 25 bytes
.+
$*
M!`(G1|11)*1
%`1
Try it online! Explanation:
.+
$*
Convert to unary.
M!`(G1|11)*1
Match as many powers of 2 as possible, and then an extra 1. This gives a total sum of the next power of 2. The regular expression is greedy (default), so tries to consume the largest possible power of 2 at each match. The M! then causes the matches themselves to be listed on separate lines.
%`1
Convert each line back to decimal.
$endgroup$
add a comment |
$begingroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
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 lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
$endgroup$
1
$begingroup$
@JonathanAllan Thanks! I didn't realise the input was limited to a specific range. It even saves me a few more bytes since now the input only appears once and I can switch to a Whatever lambda
$endgroup$
– Jo King
2 hours ago
add a comment |
$begingroup$
Charcoal, 12 bytes
I⮌E⮌↨N²×ιX²κ
Try it online! Link is to verbose version of code. Includes zero values (+2 bytes to remove). Explanation:
N Input number
↨ Converted to base (MSB first)
² Literal 2
⮌ Reversed (i.e. LSB first)
E Map over bits
ι Current bit
× Multiplied by
² Literal 2
X Raised to power
κ Current index
⮌ Reversed (back to MSB first)
I Cast to string
Implicitly print on separate lines
$endgroup$
add a comment |
$begingroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86 as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
$endgroup$
add a comment |
$begingroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Original
¤¬ÔðÍm!²
Try it
¢¬ÔË*2pE
Try it
$endgroup$
add a comment |
$begingroup$
05AB1E, 9 bytes
Loʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
LoæʒOQ
Try it online!
$endgroup$
add a comment |
$begingroup$
Alchemist, 274 bytes
_->9a+In_x+s
s+a->s+b+c
s+x->s+y+z
s+0a+0x+b->b+d+m
s+0a+0x+0b->n+e
d+m->d+2n
d+0m->e
e+n->e+m
e+0n+b->d
e+0n+0b->h
h+m->h+u+v
h+0m->t
t+y+u->t
t+0y+u->f
t+y+0u->g+p
t+0y+0u->p
g+0p+z+v->g
g+0v->f
f+c->f+a
f+z->f+x
f+u->f
f+v->f
f+y->f
f+0v+0u+0z+0c+0y+a->s
p->Out_v+Out_" "
Try it online!
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179174%2fsum-of-powers-of-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
19 Answers
19
active
oldest
votes
19 Answers
19
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Jelly, 6 bytes
BUT’2*
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):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
$endgroup$
1
$begingroup$
"ASCII-only" Sneaky’there...
$endgroup$
– Erik the Outgolfer
4 hours ago
$begingroup$
@EriktheOutgolfer I guessBUT2*Hwould work though.
$endgroup$
– Mr. Xcoder
4 hours ago
add a comment |
$begingroup$
Jelly, 6 bytes
BUT’2*
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):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
$endgroup$
1
$begingroup$
"ASCII-only" Sneaky’there...
$endgroup$
– Erik the Outgolfer
4 hours ago
$begingroup$
@EriktheOutgolfer I guessBUT2*Hwould work though.
$endgroup$
– Mr. Xcoder
4 hours ago
add a comment |
$begingroup$
Jelly, 6 bytes
BUT’2*
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):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
$endgroup$
Jelly, 6 bytes
BUT’2*
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):
BUT’2* – Monadic link. Takes a number N as input. Example: 86
B – Convert N to binary. [1, 0, 1, 0, 1, 1, 0]
U – Reverse. [0, 1, 1, 0, 1, 0, 1]
T – Truthy indices. [2, 3, 5, 7]
’ – Decrement. [1, 2, 4, 6]
2* – Raise 2 to that power. [2, 4, 16, 64]
"Proof" that it works correctly. The standard representation of an integer $
X$ in base 2 is a list ${x_1, x_2, x_3,cdots, x_n}$, where $x_iin{0,1},:forall:: iinoverline{1,n}$, such that:
$$X=sum_{i=1}^n x_icdot 2^{n-i}$$
The indices $i$ such that $x_i=0$ obviously have no contribution so we're only interested in finding those such that $x_i=1$. Since subtracting $i$ from $n$ is not convenient (the powers of two all have exponents of the form $n-i$, where $i$ is any index of a $1$), instead of finding the truthy indices in this list we reverse it and then find them "backwards" with UT. Now that we've found the correct indices all we have to do is raise $2$ to those powers.
edited 3 hours ago
answered 5 hours ago
Mr. XcoderMr. Xcoder
31.8k759198
31.8k759198
1
$begingroup$
"ASCII-only" Sneaky’there...
$endgroup$
– Erik the Outgolfer
4 hours ago
$begingroup$
@EriktheOutgolfer I guessBUT2*Hwould work though.
$endgroup$
– Mr. Xcoder
4 hours ago
add a comment |
1
$begingroup$
"ASCII-only" Sneaky’there...
$endgroup$
– Erik the Outgolfer
4 hours ago
$begingroup$
@EriktheOutgolfer I guessBUT2*Hwould work though.
$endgroup$
– Mr. Xcoder
4 hours ago
1
1
$begingroup$
"ASCII-only" Sneaky
’ there...$endgroup$
– Erik the Outgolfer
4 hours ago
$begingroup$
"ASCII-only" Sneaky
’ there...$endgroup$
– Erik the Outgolfer
4 hours ago
$begingroup$
@EriktheOutgolfer I guess
BUT2*H would work though.$endgroup$
– Mr. Xcoder
4 hours ago
$begingroup$
@EriktheOutgolfer I guess
BUT2*H would work though.$endgroup$
– Mr. Xcoder
4 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
$endgroup$
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
1 hour ago
add a comment |
$begingroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
$endgroup$
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
1 hour ago
add a comment |
$begingroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
$endgroup$
JavaScript (ES6), 28 bytes
f=n=>n?[...f(n&~-n),n&-n]:
Try it online!
edited 3 hours ago
answered 4 hours ago
ArnauldArnauld
74.3k690311
74.3k690311
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
1 hour ago
add a comment |
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
1 hour ago
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
1 hour ago
$begingroup$
You're the only person in the whole world who can make me upvote JavaScript answers!
$endgroup$
– sergiol
1 hour ago
add a comment |
$begingroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
$endgroup$
Jelly, 4 bytes
-2 since we may output zeros in place of unused powers of 2 :)
Ḷ2*&
Try it online!
edited 3 hours ago
answered 4 hours ago
Jonathan AllanJonathan Allan
51.5k535168
51.5k535168
add a comment |
add a comment |
$begingroup$
PowerShell, 45 bytes
param($a)7..0|%{'2*'*$_+'1'|iex}|?{$_-band$a}
Try it online!
Takes input $a, loops from 7 to 0, each iteration performing a string-multiplication of 2*, so for example for 3 this would be 2*2*2*, plus a 1 tacked on the end, so 2*2*2*1. That's piped to iex (short for Invoke-Expression and similar to eval) to formulate the powers-of-two. We then pull out those ? (where) the number $_ shares a value -binaryand with the input number.
$endgroup$
add a comment |
$begingroup$
PowerShell, 45 bytes
param($a)7..0|%{'2*'*$_+'1'|iex}|?{$_-band$a}
Try it online!
Takes input $a, loops from 7 to 0, each iteration performing a string-multiplication of 2*, so for example for 3 this would be 2*2*2*, plus a 1 tacked on the end, so 2*2*2*1. That's piped to iex (short for Invoke-Expression and similar to eval) to formulate the powers-of-two. We then pull out those ? (where) the number $_ shares a value -binaryand with the input number.
$endgroup$
add a comment |
$begingroup$
PowerShell, 45 bytes
param($a)7..0|%{'2*'*$_+'1'|iex}|?{$_-band$a}
Try it online!
Takes input $a, loops from 7 to 0, each iteration performing a string-multiplication of 2*, so for example for 3 this would be 2*2*2*, plus a 1 tacked on the end, so 2*2*2*1. That's piped to iex (short for Invoke-Expression and similar to eval) to formulate the powers-of-two. We then pull out those ? (where) the number $_ shares a value -binaryand with the input number.
$endgroup$
PowerShell, 45 bytes
param($a)7..0|%{'2*'*$_+'1'|iex}|?{$_-band$a}
Try it online!
Takes input $a, loops from 7 to 0, each iteration performing a string-multiplication of 2*, so for example for 3 this would be 2*2*2*, plus a 1 tacked on the end, so 2*2*2*1. That's piped to iex (short for Invoke-Expression and similar to eval) to formulate the powers-of-two. We then pull out those ? (where) the number $_ shares a value -binaryand with the input number.
answered 4 hours ago
AdmBorkBorkAdmBorkBork
26.6k364229
26.6k364229
add a comment |
add a comment |
$begingroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0).
2*⍸⍢⌽⍤⊤
Try it online!
2 two* raised to the power of⍸ the ɩndices where true⍢ while⌽ reversed⍤ of⊤ the binary representation
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0).
2*⍸⍢⌽⍤⊤
Try it online!
2 two* raised to the power of⍸ the ɩndices where true⍢ while⌽ reversed⍤ of⊤ the binary representation
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0).
2*⍸⍢⌽⍤⊤
Try it online!
2 two* raised to the power of⍸ the ɩndices where true⍢ while⌽ reversed⍤ of⊤ the binary representation
$endgroup$
APL (Dyalog Extended), 7 bytesSBCS
Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0).
2*⍸⍢⌽⍤⊤
Try it online!
2 two* raised to the power of⍸ the ɩndices where true⍢ while⌽ reversed⍤ of⊤ the binary representation
answered 4 hours ago
AdámAdám
29.7k271195
29.7k271195
add a comment |
add a comment |
$begingroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
$endgroup$
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
3 hours ago
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
$endgroup$
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
3 hours ago
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
$endgroup$
Python 2, 43 40 bytes
f=lambda n,p=1:n/p*[1]and f(n,p*2)+[p&n]
Try it online!
edited 3 hours ago
answered 5 hours ago
ovsovs
18.8k21159
18.8k21159
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
3 hours ago
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
3 hours ago
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
3 hours ago
1
1
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
3 hours ago
$begingroup$
@JonathanAllan this definitely helped. Thanks for notifying me.
$endgroup$
– ovs
3 hours ago
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
...and the restriction has been lifted, so -1 byte :)
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Catholicon, 3 bytes
ṫĊŻ
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
$endgroup$
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Catholicon, 3 bytes
ṫĊŻ
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
$endgroup$
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Catholicon, 3 bytes
ṫĊŻ
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
$endgroup$
Catholicon, 3 bytes
ṫĊŻ
Explanation:
ṫ Decompose into the largest values where:
Ċ the input
Ż the bit count is truthy (equal to one)
edited 3 hours ago
answered 3 hours ago
OkxOkx
12.7k128102
12.7k128102
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
Interesting! Get TIO'd :D
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Wolfram Language 58 bytes
Flatten[2^#&/@(Position[Reverse@IntegerDigits[#,2],1]-1)]&
IntegerDigits[#,2] returns the binary representation of the input, #, as a list of 1's and 0's.
Reverse@ reverses that list.
(Position[...,1]-1) lists the positions of the 1's in the reversed list and decrements each position by 1.
2^#&/@ 2 is raised to the power of (position -1) for each integer in the list.
Flatten removes nested braces.
$endgroup$
$begingroup$
The sort-order restriction has been lifted (also you may have zeros in place of unused powers of 2 now, if that helps)
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
@JonathanAllan, Thanks, it saves 6 bytes. There are no zeros generated by this approach.
$endgroup$
– DavidC
2 hours ago
add a comment |
$begingroup$
Wolfram Language 58 bytes
Flatten[2^#&/@(Position[Reverse@IntegerDigits[#,2],1]-1)]&
IntegerDigits[#,2] returns the binary representation of the input, #, as a list of 1's and 0's.
Reverse@ reverses that list.
(Position[...,1]-1) lists the positions of the 1's in the reversed list and decrements each position by 1.
2^#&/@ 2 is raised to the power of (position -1) for each integer in the list.
Flatten removes nested braces.
$endgroup$
$begingroup$
The sort-order restriction has been lifted (also you may have zeros in place of unused powers of 2 now, if that helps)
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
@JonathanAllan, Thanks, it saves 6 bytes. There are no zeros generated by this approach.
$endgroup$
– DavidC
2 hours ago
add a comment |
$begingroup$
Wolfram Language 58 bytes
Flatten[2^#&/@(Position[Reverse@IntegerDigits[#,2],1]-1)]&
IntegerDigits[#,2] returns the binary representation of the input, #, as a list of 1's and 0's.
Reverse@ reverses that list.
(Position[...,1]-1) lists the positions of the 1's in the reversed list and decrements each position by 1.
2^#&/@ 2 is raised to the power of (position -1) for each integer in the list.
Flatten removes nested braces.
$endgroup$
Wolfram Language 58 bytes
Flatten[2^#&/@(Position[Reverse@IntegerDigits[#,2],1]-1)]&
IntegerDigits[#,2] returns the binary representation of the input, #, as a list of 1's and 0's.
Reverse@ reverses that list.
(Position[...,1]-1) lists the positions of the 1's in the reversed list and decrements each position by 1.
2^#&/@ 2 is raised to the power of (position -1) for each integer in the list.
Flatten removes nested braces.
edited 2 hours ago
answered 4 hours ago
DavidCDavidC
23.9k243102
23.9k243102
$begingroup$
The sort-order restriction has been lifted (also you may have zeros in place of unused powers of 2 now, if that helps)
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
@JonathanAllan, Thanks, it saves 6 bytes. There are no zeros generated by this approach.
$endgroup$
– DavidC
2 hours ago
add a comment |
$begingroup$
The sort-order restriction has been lifted (also you may have zeros in place of unused powers of 2 now, if that helps)
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
@JonathanAllan, Thanks, it saves 6 bytes. There are no zeros generated by this approach.
$endgroup$
– DavidC
2 hours ago
$begingroup$
The sort-order restriction has been lifted (also you may have zeros in place of unused powers of 2 now, if that helps)
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
The sort-order restriction has been lifted (also you may have zeros in place of unused powers of 2 now, if that helps)
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
@JonathanAllan, Thanks, it saves 6 bytes. There are no zeros generated by this approach.
$endgroup$
– DavidC
2 hours ago
$begingroup$
@JonathanAllan, Thanks, it saves 6 bytes. There are no zeros generated by this approach.
$endgroup$
– DavidC
2 hours ago
add a comment |
$begingroup$
Retina 0.8.2, 25 bytes
.+
$*
M!`(G1|11)*1
%`1
Try it online! Explanation:
.+
$*
Convert to unary.
M!`(G1|11)*1
Match as many powers of 2 as possible, and then an extra 1. This gives a total sum of the next power of 2. The regular expression is greedy (default), so tries to consume the largest possible power of 2 at each match. The M! then causes the matches themselves to be listed on separate lines.
%`1
Convert each line back to decimal.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 25 bytes
.+
$*
M!`(G1|11)*1
%`1
Try it online! Explanation:
.+
$*
Convert to unary.
M!`(G1|11)*1
Match as many powers of 2 as possible, and then an extra 1. This gives a total sum of the next power of 2. The regular expression is greedy (default), so tries to consume the largest possible power of 2 at each match. The M! then causes the matches themselves to be listed on separate lines.
%`1
Convert each line back to decimal.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 25 bytes
.+
$*
M!`(G1|11)*1
%`1
Try it online! Explanation:
.+
$*
Convert to unary.
M!`(G1|11)*1
Match as many powers of 2 as possible, and then an extra 1. This gives a total sum of the next power of 2. The regular expression is greedy (default), so tries to consume the largest possible power of 2 at each match. The M! then causes the matches themselves to be listed on separate lines.
%`1
Convert each line back to decimal.
$endgroup$
Retina 0.8.2, 25 bytes
.+
$*
M!`(G1|11)*1
%`1
Try it online! Explanation:
.+
$*
Convert to unary.
M!`(G1|11)*1
Match as many powers of 2 as possible, and then an extra 1. This gives a total sum of the next power of 2. The regular expression is greedy (default), so tries to consume the largest possible power of 2 at each match. The M! then causes the matches themselves to be listed on separate lines.
%`1
Convert each line back to decimal.
answered 4 hours ago
NeilNeil
80k744178
80k744178
add a comment |
add a comment |
$begingroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
$endgroup$
add a comment |
$begingroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
$endgroup$
add a comment |
$begingroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
$endgroup$
Python, 35 bytes
lambda n:[n&2**i for i in range(8)]
Little-endian with zeros at unused powers of 2.
Try it online!
edited 3 hours ago
answered 4 hours ago
Jonathan AllanJonathan Allan
51.5k535168
51.5k535168
add a comment |
add a comment |
$begingroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
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 lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
$endgroup$
1
$begingroup$
@JonathanAllan Thanks! I didn't realise the input was limited to a specific range. It even saves me a few more bytes since now the input only appears once and I can switch to a Whatever lambda
$endgroup$
– Jo King
2 hours ago
add a comment |
$begingroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
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 lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
$endgroup$
1
$begingroup$
@JonathanAllan Thanks! I didn't realise the input was limited to a specific range. It even saves me a few more bytes since now the input only appears once and I can switch to a Whatever lambda
$endgroup$
– Jo King
2 hours ago
add a comment |
$begingroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
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 lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
$endgroup$
Perl 6, 16 12 bytes
-4 bytes thanks to Jonathan Allan
*+&2**all ^8
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 lists and it is possible to extract the values from one.
Explanation:
*+& # Bitwise AND the input with
2** # 2 raised to the power of
all ^8 # All of the range 0 to 7
edited 2 hours ago
answered 3 hours ago
Jo KingJo King
21.5k248110
21.5k248110
1
$begingroup$
@JonathanAllan Thanks! I didn't realise the input was limited to a specific range. It even saves me a few more bytes since now the input only appears once and I can switch to a Whatever lambda
$endgroup$
– Jo King
2 hours ago
add a comment |
1
$begingroup$
@JonathanAllan Thanks! I didn't realise the input was limited to a specific range. It even saves me a few more bytes since now the input only appears once and I can switch to a Whatever lambda
$endgroup$
– Jo King
2 hours ago
1
1
$begingroup$
@JonathanAllan Thanks! I didn't realise the input was limited to a specific range. It even saves me a few more bytes since now the input only appears once and I can switch to a Whatever lambda
$endgroup$
– Jo King
2 hours ago
$begingroup$
@JonathanAllan Thanks! I didn't realise the input was limited to a specific range. It even saves me a few more bytes since now the input only appears once and I can switch to a Whatever lambda
$endgroup$
– Jo King
2 hours ago
add a comment |
$begingroup$
Charcoal, 12 bytes
I⮌E⮌↨N²×ιX²κ
Try it online! Link is to verbose version of code. Includes zero values (+2 bytes to remove). Explanation:
N Input number
↨ Converted to base (MSB first)
² Literal 2
⮌ Reversed (i.e. LSB first)
E Map over bits
ι Current bit
× Multiplied by
² Literal 2
X Raised to power
κ Current index
⮌ Reversed (back to MSB first)
I Cast to string
Implicitly print on separate lines
$endgroup$
add a comment |
$begingroup$
Charcoal, 12 bytes
I⮌E⮌↨N²×ιX²κ
Try it online! Link is to verbose version of code. Includes zero values (+2 bytes to remove). Explanation:
N Input number
↨ Converted to base (MSB first)
² Literal 2
⮌ Reversed (i.e. LSB first)
E Map over bits
ι Current bit
× Multiplied by
² Literal 2
X Raised to power
κ Current index
⮌ Reversed (back to MSB first)
I Cast to string
Implicitly print on separate lines
$endgroup$
add a comment |
$begingroup$
Charcoal, 12 bytes
I⮌E⮌↨N²×ιX²κ
Try it online! Link is to verbose version of code. Includes zero values (+2 bytes to remove). Explanation:
N Input number
↨ Converted to base (MSB first)
² Literal 2
⮌ Reversed (i.e. LSB first)
E Map over bits
ι Current bit
× Multiplied by
² Literal 2
X Raised to power
κ Current index
⮌ Reversed (back to MSB first)
I Cast to string
Implicitly print on separate lines
$endgroup$
Charcoal, 12 bytes
I⮌E⮌↨N²×ιX²κ
Try it online! Link is to verbose version of code. Includes zero values (+2 bytes to remove). Explanation:
N Input number
↨ Converted to base (MSB first)
² Literal 2
⮌ Reversed (i.e. LSB first)
E Map over bits
ι Current bit
× Multiplied by
² Literal 2
X Raised to power
κ Current index
⮌ Reversed (back to MSB first)
I Cast to string
Implicitly print on separate lines
answered 3 hours ago
NeilNeil
80k744178
80k744178
add a comment |
add a comment |
$begingroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86 as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
$endgroup$
add a comment |
$begingroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86 as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
$endgroup$
add a comment |
$begingroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86 as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
$endgroup$
MATL, 5 bytes
BPfqW
Try it online!
Explanation
Consider input 86 as an example.
B % Implicit input. Convert to binary (highest to lowest digits)
% STACK: [1 0 1 0 1 1 0]
P % Flip
% STACK: [0 1 1 0 1 0 1]
f % Find: indices of nonzeros (1-based)
% STACK: [2 3 5 7]
q % Subtract 1, element-wise
% STACK: [1 2 4 6]
W % Exponential with base 2, element-wise. Implicit display
% STACK: [2 4 16 64]
answered 3 hours ago
Luis MendoLuis Mendo
74.2k887291
74.2k887291
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
$endgroup$
Wolfram Language (Mathematica), 17 bytes
#~NumberExpand~2&
Try it online!
Mathematica strikes again.
answered 2 hours ago
lirtosiastlirtosiast
16.2k437108
16.2k437108
add a comment |
add a comment |
$begingroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
$endgroup$
add a comment |
$begingroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
$endgroup$
add a comment |
$begingroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
$endgroup$
Pure Bash, 20
echo $[2**{7..0}&$1]
Try it online!
answered 2 hours ago
Digital TraumaDigital Trauma
58.9k787223
58.9k787223
add a comment |
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
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.
answered 2 hours ago
lirtosiastlirtosiast
16.2k437108
16.2k437108
add a comment |
add a comment |
$begingroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Original
¤¬ÔðÍm!²
Try it
¢¬ÔË*2pE
Try it
$endgroup$
add a comment |
$begingroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Original
¤¬ÔðÍm!²
Try it
¢¬ÔË*2pE
Try it
$endgroup$
add a comment |
$begingroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Original
¤¬ÔðÍm!²
Try it
¢¬ÔË*2pE
Try it
$endgroup$
Japt, 8 5 bytes
Æ&2pX
Try it
Original
¤¬ÔðÍm!²
Try it
¢¬ÔË*2pE
Try it
edited 2 hours ago
answered 3 hours ago
ShaggyShaggy
19.5k21666
19.5k21666
add a comment |
add a comment |
$begingroup$
05AB1E, 9 bytes
Loʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
LoæʒOQ
Try it online!
$endgroup$
add a comment |
$begingroup$
05AB1E, 9 bytes
Loʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
LoæʒOQ
Try it online!
$endgroup$
add a comment |
$begingroup$
05AB1E, 9 bytes
Loʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
LoæʒOQ
Try it online!
$endgroup$
05AB1E, 9 bytes
Loʒ›}æʒOQ
Try it online!
This is also correct for 6-bytes, but it doesn't complete in time on TIO for 86:
05AB1E, 6 bytes
LoæʒOQ
Try it online!
answered 6 mins ago
Magic Octopus UrnMagic Octopus Urn
12.5k444125
12.5k444125
add a comment |
add a comment |
$begingroup$
Alchemist, 274 bytes
_->9a+In_x+s
s+a->s+b+c
s+x->s+y+z
s+0a+0x+b->b+d+m
s+0a+0x+0b->n+e
d+m->d+2n
d+0m->e
e+n->e+m
e+0n+b->d
e+0n+0b->h
h+m->h+u+v
h+0m->t
t+y+u->t
t+0y+u->f
t+y+0u->g+p
t+0y+0u->p
g+0p+z+v->g
g+0v->f
f+c->f+a
f+z->f+x
f+u->f
f+v->f
f+y->f
f+0v+0u+0z+0c+0y+a->s
p->Out_v+Out_" "
Try it online!
$endgroup$
add a comment |
$begingroup$
Alchemist, 274 bytes
_->9a+In_x+s
s+a->s+b+c
s+x->s+y+z
s+0a+0x+b->b+d+m
s+0a+0x+0b->n+e
d+m->d+2n
d+0m->e
e+n->e+m
e+0n+b->d
e+0n+0b->h
h+m->h+u+v
h+0m->t
t+y+u->t
t+0y+u->f
t+y+0u->g+p
t+0y+0u->p
g+0p+z+v->g
g+0v->f
f+c->f+a
f+z->f+x
f+u->f
f+v->f
f+y->f
f+0v+0u+0z+0c+0y+a->s
p->Out_v+Out_" "
Try it online!
$endgroup$
add a comment |
$begingroup$
Alchemist, 274 bytes
_->9a+In_x+s
s+a->s+b+c
s+x->s+y+z
s+0a+0x+b->b+d+m
s+0a+0x+0b->n+e
d+m->d+2n
d+0m->e
e+n->e+m
e+0n+b->d
e+0n+0b->h
h+m->h+u+v
h+0m->t
t+y+u->t
t+0y+u->f
t+y+0u->g+p
t+0y+0u->p
g+0p+z+v->g
g+0v->f
f+c->f+a
f+z->f+x
f+u->f
f+v->f
f+y->f
f+0v+0u+0z+0c+0y+a->s
p->Out_v+Out_" "
Try it online!
$endgroup$
Alchemist, 274 bytes
_->9a+In_x+s
s+a->s+b+c
s+x->s+y+z
s+0a+0x+b->b+d+m
s+0a+0x+0b->n+e
d+m->d+2n
d+0m->e
e+n->e+m
e+0n+b->d
e+0n+0b->h
h+m->h+u+v
h+0m->t
t+y+u->t
t+0y+u->f
t+y+0u->g+p
t+0y+0u->p
g+0p+z+v->g
g+0v->f
f+c->f+a
f+z->f+x
f+u->f
f+v->f
f+y->f
f+0v+0u+0z+0c+0y+a->s
p->Out_v+Out_" "
Try it online!
answered 3 mins ago
BMOBMO
12.1k22290
12.1k22290
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179174%2fsum-of-powers-of-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
$begingroup$
Does the list have to be sorted highest to lowest?
$endgroup$
– Adám
4 hours ago
2
$begingroup$
May we output some redundant zeros?
$endgroup$
– Jonathan Allan
4 hours ago
$begingroup$
@Adám going to say yes this time. I want the output to be somewhat similar to the binary representation of the number. ie 65 = 0 + 2^6 +0 +0 +0 +0 + 0 +2^0
$endgroup$
– SpookyGengar
4 hours ago
4
$begingroup$
RE: "sorted highest to lowest" why add a restriction that was not part of the challenge and invalidates most existing answers? (Also what about little-endian?!) + it invalidates my Python answer since sets do not have any order.
$endgroup$
– Jonathan Allan
4 hours ago
2
$begingroup$
@JonathanAllan I've removed the restriction. I'll keep that in mind next time I post another question - I'm still fairly new to this. :)
$endgroup$
– SpookyGengar
3 hours ago