Bit floating sequenceTips for golfing in 05AB1EFloating point addition, without floats!The Floating HordeSylvester's sequenceKuznetsov's SequenceCalculate Exponents bit by bitThe Binary Square Diagonal SequenceAlternating bit smearingFloating over the integersNew Order #2: Turn My WayFloating Point XOR
Graph that visualizes subset relations
Ran out of space of newly installed HDD?
Is it possible to animate 2 folds on the same sheet?
Can the Protection fighting style be used in this way?
Did Ohio pass a law granting students the right to give scientifically wrong answers consistent with their religious beliefs?
Why are there so many binary systems?
Why does it not make sense to define addition on points in geometry?
Is there a preferred time in their presidency when US presidents pardon the most people?
How to left align the beginnings of two equations and right align the ends of the equations
How does the credit rating of a country affect its populace?
Uniqueness principle for functions types in the HoTT book
Is every conformal manifold equivalent to a flat one with cone singularities?
Can the treble clef be used instead of the bass clef in piano music?
Running code in a different tmux pane
Isn't any conversation with the US president quid-pro-quo?
How to explain to traditional people why they should upgrade their old Windows XP device?
The mystery of the digitally disadvantaged ancestors
How can I simplify this sum any further?
Grammar explanation for ~よし
Ethics: Is it ethical for a professor to conduct research using a student's ideas without giving them credit?
What was the motive for inventing Gröbner bases?
Is it a mistake to use a password that has previously been used (by anyone ever)?
Car imitates dead battery but comes back to life ~30 minutes later and lets me start it
How is a series resistor limiting the voltage for a diode?
Bit floating sequence
Tips for golfing in 05AB1EFloating point addition, without floats!The Floating HordeSylvester's sequenceKuznetsov's SequenceCalculate Exponents bit by bitThe Binary Square Diagonal SequenceAlternating bit smearingFloating over the integersNew Order #2: Turn My WayFloating Point XOR
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
$begingroup$
A bit floats from the LSB to the MSB moving one position each time until it floats to the top of the container:
0000
0001
0010
0100
1000
Once one bit floats to the top, another bit begins its journey and it stops when it meets other bit:
1001
1010
1100
This happens until the container is filled with bits:
1101
1110
1111
Challenge
Given an integer number, output the "Bit floating sequence" for a container of that number of bits.
- Each term of the sequence can be separated by any separator of your choice.
Edit: Sequence must be shown as decimal integer numbers, starting by the first therm:0.- The container size sould be greater than zero and up to the number of bits of the bigest integer suported by the language of your choice. You can assume that the input always match this requirement.
Examples
Only the numeric sequence is required, binary representation is shown as example:
For 1:
0 10 -> 0
1 -> 1For 3:
0 1 2 4 5 6 7000 -> 0
001 -> 1
010 -> 2
100 -> 4
101 -> 5
110 -> 6
111 -> 7For 4:
0 1 2 4 8 9 10 12 13 14 150000 -> 0
0001 -> 1
0010 -> 2
0100 -> 4
1000 -> 8
1001 -> 9
1010 -> 10
1100 -> 12
1101 -> 13
1110 -> 14
1111 -> 15For 8:
0 1 2 4 8 16 32 64 128 129 130 132 136 144 160 192 193 194 196 200 208 224 225 226 228 232 240 241 242 244 248 249 250 252 253 254 25500000000 -> 0
00000001 -> 1
00000010 -> 2
00000100 -> 4
00001000 -> 8
…
…
…
11111000 -> 248
11111001 -> 249
11111010 -> 250
11111100 -> 252
11111101 -> 253
11111110 -> 254
11111111 -> 255
code-golf sequence
$endgroup$
add a comment
|
$begingroup$
A bit floats from the LSB to the MSB moving one position each time until it floats to the top of the container:
0000
0001
0010
0100
1000
Once one bit floats to the top, another bit begins its journey and it stops when it meets other bit:
1001
1010
1100
This happens until the container is filled with bits:
1101
1110
1111
Challenge
Given an integer number, output the "Bit floating sequence" for a container of that number of bits.
- Each term of the sequence can be separated by any separator of your choice.
Edit: Sequence must be shown as decimal integer numbers, starting by the first therm:0.- The container size sould be greater than zero and up to the number of bits of the bigest integer suported by the language of your choice. You can assume that the input always match this requirement.
Examples
Only the numeric sequence is required, binary representation is shown as example:
For 1:
0 10 -> 0
1 -> 1For 3:
0 1 2 4 5 6 7000 -> 0
001 -> 1
010 -> 2
100 -> 4
101 -> 5
110 -> 6
111 -> 7For 4:
0 1 2 4 8 9 10 12 13 14 150000 -> 0
0001 -> 1
0010 -> 2
0100 -> 4
1000 -> 8
1001 -> 9
1010 -> 10
1100 -> 12
1101 -> 13
1110 -> 14
1111 -> 15For 8:
0 1 2 4 8 16 32 64 128 129 130 132 136 144 160 192 193 194 196 200 208 224 225 226 228 232 240 241 242 244 248 249 250 252 253 254 25500000000 -> 0
00000001 -> 1
00000010 -> 2
00000100 -> 4
00001000 -> 8
…
…
…
11111000 -> 248
11111001 -> 249
11111010 -> 250
11111100 -> 252
11111101 -> 253
11111110 -> 254
11111111 -> 255
code-golf sequence
$endgroup$
2
$begingroup$
May we output the sequence in any order (i.e. reversed), or do they have to be sorted from lowest to highest?
$endgroup$
– Kevin Cruijssen
Sep 6 at 12:00
1
$begingroup$
May we output as floats? E.g.[0.0, 1.0]
$endgroup$
– Grimmy
Sep 6 at 13:09
8
$begingroup$
May we output using the binary representation?
$endgroup$
– Neil
Sep 6 at 21:01
$begingroup$
May we output the zero-indexed sequence? i.e.0 -> [0, 1]
$endgroup$
– attinat
Sep 6 at 22:36
add a comment
|
$begingroup$
A bit floats from the LSB to the MSB moving one position each time until it floats to the top of the container:
0000
0001
0010
0100
1000
Once one bit floats to the top, another bit begins its journey and it stops when it meets other bit:
1001
1010
1100
This happens until the container is filled with bits:
1101
1110
1111
Challenge
Given an integer number, output the "Bit floating sequence" for a container of that number of bits.
- Each term of the sequence can be separated by any separator of your choice.
Edit: Sequence must be shown as decimal integer numbers, starting by the first therm:0.- The container size sould be greater than zero and up to the number of bits of the bigest integer suported by the language of your choice. You can assume that the input always match this requirement.
Examples
Only the numeric sequence is required, binary representation is shown as example:
For 1:
0 10 -> 0
1 -> 1For 3:
0 1 2 4 5 6 7000 -> 0
001 -> 1
010 -> 2
100 -> 4
101 -> 5
110 -> 6
111 -> 7For 4:
0 1 2 4 8 9 10 12 13 14 150000 -> 0
0001 -> 1
0010 -> 2
0100 -> 4
1000 -> 8
1001 -> 9
1010 -> 10
1100 -> 12
1101 -> 13
1110 -> 14
1111 -> 15For 8:
0 1 2 4 8 16 32 64 128 129 130 132 136 144 160 192 193 194 196 200 208 224 225 226 228 232 240 241 242 244 248 249 250 252 253 254 25500000000 -> 0
00000001 -> 1
00000010 -> 2
00000100 -> 4
00001000 -> 8
…
…
…
11111000 -> 248
11111001 -> 249
11111010 -> 250
11111100 -> 252
11111101 -> 253
11111110 -> 254
11111111 -> 255
code-golf sequence
$endgroup$
A bit floats from the LSB to the MSB moving one position each time until it floats to the top of the container:
0000
0001
0010
0100
1000
Once one bit floats to the top, another bit begins its journey and it stops when it meets other bit:
1001
1010
1100
This happens until the container is filled with bits:
1101
1110
1111
Challenge
Given an integer number, output the "Bit floating sequence" for a container of that number of bits.
- Each term of the sequence can be separated by any separator of your choice.
Edit: Sequence must be shown as decimal integer numbers, starting by the first therm:0.- The container size sould be greater than zero and up to the number of bits of the bigest integer suported by the language of your choice. You can assume that the input always match this requirement.
Examples
Only the numeric sequence is required, binary representation is shown as example:
For 1:
0 10 -> 0
1 -> 1For 3:
0 1 2 4 5 6 7000 -> 0
001 -> 1
010 -> 2
100 -> 4
101 -> 5
110 -> 6
111 -> 7For 4:
0 1 2 4 8 9 10 12 13 14 150000 -> 0
0001 -> 1
0010 -> 2
0100 -> 4
1000 -> 8
1001 -> 9
1010 -> 10
1100 -> 12
1101 -> 13
1110 -> 14
1111 -> 15For 8:
0 1 2 4 8 16 32 64 128 129 130 132 136 144 160 192 193 194 196 200 208 224 225 226 228 232 240 241 242 244 248 249 250 252 253 254 25500000000 -> 0
00000001 -> 1
00000010 -> 2
00000100 -> 4
00001000 -> 8
…
…
…
11111000 -> 248
11111001 -> 249
11111010 -> 250
11111100 -> 252
11111101 -> 253
11111110 -> 254
11111111 -> 255
code-golf sequence
code-golf sequence
edited Sep 9 at 10:11
PaperBirdMaster
asked Sep 6 at 9:48
PaperBirdMasterPaperBirdMaster
3811 silver badge6 bronze badges
3811 silver badge6 bronze badges
2
$begingroup$
May we output the sequence in any order (i.e. reversed), or do they have to be sorted from lowest to highest?
$endgroup$
– Kevin Cruijssen
Sep 6 at 12:00
1
$begingroup$
May we output as floats? E.g.[0.0, 1.0]
$endgroup$
– Grimmy
Sep 6 at 13:09
8
$begingroup$
May we output using the binary representation?
$endgroup$
– Neil
Sep 6 at 21:01
$begingroup$
May we output the zero-indexed sequence? i.e.0 -> [0, 1]
$endgroup$
– attinat
Sep 6 at 22:36
add a comment
|
2
$begingroup$
May we output the sequence in any order (i.e. reversed), or do they have to be sorted from lowest to highest?
$endgroup$
– Kevin Cruijssen
Sep 6 at 12:00
1
$begingroup$
May we output as floats? E.g.[0.0, 1.0]
$endgroup$
– Grimmy
Sep 6 at 13:09
8
$begingroup$
May we output using the binary representation?
$endgroup$
– Neil
Sep 6 at 21:01
$begingroup$
May we output the zero-indexed sequence? i.e.0 -> [0, 1]
$endgroup$
– attinat
Sep 6 at 22:36
2
2
$begingroup$
May we output the sequence in any order (i.e. reversed), or do they have to be sorted from lowest to highest?
$endgroup$
– Kevin Cruijssen
Sep 6 at 12:00
$begingroup$
May we output the sequence in any order (i.e. reversed), or do they have to be sorted from lowest to highest?
$endgroup$
– Kevin Cruijssen
Sep 6 at 12:00
1
1
$begingroup$
May we output as floats? E.g.
[0.0, 1.0]$endgroup$
– Grimmy
Sep 6 at 13:09
$begingroup$
May we output as floats? E.g.
[0.0, 1.0]$endgroup$
– Grimmy
Sep 6 at 13:09
8
8
$begingroup$
May we output using the binary representation?
$endgroup$
– Neil
Sep 6 at 21:01
$begingroup$
May we output using the binary representation?
$endgroup$
– Neil
Sep 6 at 21:01
$begingroup$
May we output the zero-indexed sequence? i.e.
0 -> [0, 1]$endgroup$
– attinat
Sep 6 at 22:36
$begingroup$
May we output the zero-indexed sequence? i.e.
0 -> [0, 1]$endgroup$
– attinat
Sep 6 at 22:36
add a comment
|
19 Answers
19
active
oldest
votes
$begingroup$
05AB1E, 10 bytes
LRL˜Íoî.¥ï
Try it online!
L # range [1..input]
R # reversed
L # convert each to a range: [[1..input], [1..input-1], ..., [1]]
˜ # flatten
Í # subtract 2 from each
o # 2**each
î # round up (returns a float)
ï # convert to integer
.¥ # undelta
$endgroup$
2
$begingroup$
I think there is a meta-post somewhere allowing floats with.0by default for integers, but not sure. I personally usually put theïin the footer to pretty-print and don't include it in the byte-count.
$endgroup$
– Kevin Cruijssen
Sep 6 at 13:38
add a comment
|
$begingroup$
Python 2, 45 bytes
y=n=2**input()
while y:print n-y;y=y&y-1or~-y
Try it online!
It turns out shorter to generate 2**n minus each term in the sequence for input n. If we look at their binary expansion, below for n=5, we see a nice pattern of triangles of 1's in the binary expansions.
100000 32
011111 31
011110 30
011100 28
011000 24
010000 16
001111 15
001110 14
001100 12
001000 8
000111 7
000110 6
000100 4
000011 3
000010 2
000001 1
Each number is obtained from the previous one by removing the rightmost one in the binary expansion, except if that would make the number 0, we subtract 1 instead, creating a new block of 1's that starts a new smaller triangle. This is implemented as y=y&y-1or~-y, where y&y-1 is a bit trick to remove the rightmost 1, and or~-y gives y-1 instead if that value was 0.
Python 2, 49 bytes
def f(n,x=0):1%n;print x;f(n-x%2,x+(x%2**n or 1))
Try it online!
A function that prints, terminating with error. The more nice program below turned out longer.
51 bytes
n=input()
x=0
while n:n-=x%2;print x;x+=x%2**n or 1
Try it online!
$endgroup$
add a comment
|
$begingroup$
Jelly, 11 10 bytes
RUḶ’F2*ĊÄŻ
Port of @Grimy's 05AB1E answer, so make sure to upvote him!
-1 byte thanks to @Grimy.
Try it online.
Explanation:
R # Create a list in the range [1, (implicit) argument]
U # Reverse it to [argument, 1]
Ḷ # Create an inner list in the range [0, N) for each value N in this list
’ # Decrease each by 1
F # Flatten the list of lists
2* # Take 2 to the power each
Ċ # Ceil
Ä # Undelta (cumulative sum) the list
Ż # And add a leading 0
# (after which the result is output implicitly)
$endgroup$
2
$begingroup$
R_2->Ḷ’for -1.Ḷis the only sensible range, I really wish 05AB1E had a single-byter for it.
$endgroup$
– Grimmy
Sep 6 at 15:29
$begingroup$
@Grimy Ah, how did I miss that one. I searched for range and must have skipped passed it somehow.. >.> Thanks!
$endgroup$
– Kevin Cruijssen
Sep 6 at 16:27
add a comment
|
$begingroup$
Perl 5 (-n), 41 40 bytes
-1 byte thanls to Xcali
mapglob"0b"."0,1"x$_
TIO
"0,1"x$_: the string"0,1"repeated n times"0b".: concatenate to"0b"glob: glob expansion (cartesian product)map...: for each element/01.*1/||: to skip when01followed by something then1say oct: to convert to decimal and say
$endgroup$
$begingroup$
40
$endgroup$
– Xcali
Sep 6 at 14:37
add a comment
|
$begingroup$
JavaScript (ES6), 43 bytes
When in doubt, use xnor's method.
n=>(g=x=>x?[n-x,...g(x&--x||x)]:[])(n=1<<n)
Try it online!
JavaScript (ES6), 59 57 55 52 bytes
f=(n,x=0)=>x>>n?[]:[x,...f(n,x+=x+(x&=-x)>>n|!x||x)]
Try it online!
How?
We define $p(x)$ as the highest power of $2$ dividing $x$, with $p(0)=0$ by convention.
This function can be implemented with a simple bitwise AND of $x$ and $-x$ to isolate the lowest bit set to $1$ in $x$. For instance:
$$p(52)=52 operatornameAND-52=4$$
Using $p$, the sequence $a_n$ can be defined as $a_n(0)=0$ and:
$$a_n(k+1)=cases
a_n(k)+p(a_n(k)), & textif $p(a_n(k))neq0$ and $a_n(k)+p(a_n(k))<2^n$\
a_n(k)+1, & textotherwise
$$
Commented
f = ( // f is a recursive function taking:
n, // n = input
x = 0 // x = current term of the sequence
) => //
x >> n ? // if x is greater than or equal to 2**n:
[] // stop recursion
: // else:
[ // update the sequence:
x, // append the current term to the sequence
...f( // do a recursive call:
n, // pass n unchanged
x += // update x:
x + (x &= -x) // given x' = lowest bit of x set to 1:
>> n // if x + x' is greater than or equal to 2**n
| !x // or x' is equal to 0: add 1 to x
|| x // otherwise, add x' to x
) // end of recursive call
] // end of sequence update
$endgroup$
add a comment
|
$begingroup$
Python 2, 95 76 bytes
n=input()
a=0
print 0
while n:
for j in range(n):print a+2**j
n-=1;a+=2**n
Try it online!
$endgroup$
add a comment
|
$begingroup$
Perl 6, 43 bytes
0 x$_,say :2($_);S/(0)1...1 x$_
Try it online!
Anonymous code block that takes a number and outputs the sequence separated by newlines. This works by starting with 0 repeated n times then replacing either 01 with 10 or the last 0 with a 1 until the number is just ones.
Or 40 bytes, using Nahuel Fouilleul's approach
say :2($_),[X~] ^2 xx$_
Try it online!
$endgroup$
$begingroup$
"then replacing either01with10or the last0with a1until the number is just ones" That's a genius move!
$endgroup$
– PaperBirdMaster
Sep 6 at 10:51
add a comment
|
$begingroup$
Python 2, 60 bytes
f=lambda i,n=0,b=1:[n][i:]or[n]+f(i-1/b,n^b+b/2,b>>i or 2*b)
Try it online!
Python 3, 76 bytes
f=lambda n:[0][n:]or[0]+[2**i for i in range(n-1)]+[x|1<<n-1for x in f(n-1)]
Try it online!
$endgroup$
add a comment
|
$begingroup$
Python 2, 67 bytes
n=0
i=2**input()-1
while n<=i:print n;d=n&(~-n^i)or 1;n+=n+d>i or d
Try it online!
$endgroup$
add a comment
|
$begingroup$
Python 3, 62 bytes
def f(n,c=0):
while c<2**n:yield c;r=c&-c;c+=c+r>>n or r or 1
Try it online!
The idea is more or less the same as @Arnauld's solution.
Another 65-byte solution:
lambda n:g(2**n-1)
g=lambda c:[0][c:]or g(c-((c&-c)//2 or 1))+[c]
Try it online!
$endgroup$
add a comment
|
$begingroup$
Jelly, 12 bytes
⁼þ‘ṚÄUo€ƊẎQḄ
Try it online!
$endgroup$
add a comment
|
$begingroup$
05AB1E, 13 12 bytes
Tsãʒ1ÛSO2‹}C
-1 byte thanks to @Grimy (also take a look at his shorter approach here).
Try it online or verify all test cases.
Explanation:
T # Push 10
sã # Swap to get the (implicit) input, and get the cartesian product with "10"
ʒ # Filter it by:
1Û # Remove leading 1s
SO # Get the sum of the remaining digits
! # Check that the sum is either 0 or 1 by taking the factorial
# (NOTE: Only 1 is truthy in 05AB1E)
C # After the filter: convert all remaining strings from binary to integer
!xC
-1 byte thanks to @Grimy (also take a look at his shorter approach here).
Try it online or verify all test cases.
Explanation:
T # Push 10
sã # Swap to get the (implicit) input, and get the cartesian product with "10"
ʒ # Filter it by:
1Û # Remove leading 1s
SO # Get the sum of the remaining digits
! # Check that the sum is either 0 or 1 by taking the factorial
# (NOTE: Only 1 is truthy in 05AB1E)
C # After the filter: convert all remaining strings from binary to integer
improve this answer
$endgroup$
$begingroup$
Alternate 13:oL<ʒbIj1Û1¢2‹. Doesn't look like I can get it lower.
$endgroup$
– Grimmy
Sep 6 at 13:03
1
$begingroup$
@Grimy I just hadoL<ʒbIj1ÛSO2‹and was trying to see where my error was. :) But I'm glad to see you aren't able to find a shorter version for one of my answers for a change. ;p (inb4 you find a shorter one after all xD)
$endgroup$
– Kevin Cruijssen
Sep 6 at 13:06
1
$begingroup$
@Grimy I have the feelingSO2‹can be 3 bytes somehow perhaps, but I'm not seeing it and also not entirely sure.. There are some alternatives, likeSO1~orSÆ>d, but I'm unable to find a 3-byter.
$endgroup$
– Kevin Cruijssen
Sep 6 at 13:13
1
$begingroup$
10 with a completely different approach
$endgroup$
– Grimmy
Sep 6 at 13:13
1
$begingroup$
Your feeling was right, I just found a 3-byter:SO!. Pretty sure I have some old answers using2‹that could benefit from this as well.
$endgroup$
– Grimmy
Sep 6 at 13:45
C
-1 byte thanks to @Grimy (also take a look at his shorter approach here).
Try it online or verify all test cases.
Explanation:
T # Push 10
sã # Swap to get the (implicit) input, and get the cartesian product with "10"
ʒ # Filter it by:
1Û # Remove leading 1s
SO # Get the sum of the remaining digits
! # Check that the sum is either 0 or 1 by taking the factorial
# (NOTE: Only 1 is truthy in 05AB1E)
C # After the filter: convert all remaining strings from binary to integer
show 2 more comments
$begingroup$
05AB1E, 13 12 bytes
Tsãʒ1ÛSO2‹C
-1 byte thanks to @Grimy (also take a look at his shorter approach here).
Try it online or verify all test cases.
Explanation:
T # Push 10
sã # Swap to get the (implicit) input, and get the cartesian product with "10"
ʒ # Filter it by:
1Û # Remove leading 1s
SO # Get the sum of the remaining digits
! # Check that the sum is either 0 or 1 by taking the factorial
# (NOTE: Only 1 is truthy in 05AB1E)
C # After the filter: convert all remaining strings from binary to integer
improve this answer
$endgroup$
05AB1E, 13 12 bytes
Tsãʒ1ÛSO2‹C
-1 byte thanks to @Grimy (also take a look at his shorter approach here).
Try it online or verify all test cases.
Explanation:
T # Push 10
sã # Swap to get the (implicit) input, and get the cartesian product with "10"
ʒ # Filter it by:
1Û # Remove leading 1s
SO # Get the sum of the remaining digits
! # Check that the sum is either 0 or 1 by taking the factorial
# (NOTE: Only 1 is truthy in 05AB1E)
C # After the filter: convert all remaining strings from binary to integer
{ # And sort (reverse) them
# (after which the result is output implicitly)
edited Sep 6 at 13:57
answered Sep 6 at 11:59
Kevin CruijssenKevin Cruijssen
62k7 gold badges89 silver badges255 bronze badges
62k7 gold badges89 silver badges255 bronze badges
$begingroup$
Alternate 13:oL<ʒbIj1Û1¢2‹. Doesn't look like I can get it lower.
$endgroup$
– Grimmy
Sep 6 at 13:03
1
$begingroup$
@Grimy I just hadoL<ʒbIj1ÛSO2‹and was trying to see where my error was. :) But I'm glad to see you aren't able to find a shorter version for one of my answers for a change. ;p (inb4 you find a shorter one after all xD)
$endgroup$
– Kevin Cruijssen
Sep 6 at 13:06
1
$begingroup$
@Grimy I have the feelingSO2‹can be 3 bytes somehow perhaps, but I'm not seeing it and also not entirely sure.. There are some alternatives, likeSO1~orSÆ>d, but I'm unable to find a 3-byter.
$endgroup$
– Kevin Cruijssen
Sep 6 at 13:13
1
$begingroup$
10 with a completely different approach
$endgroup$
– Grimmy
Sep 6 at 13:13
1
$begingroup$
Your feeling was right, I just found a 3-byter:SO!. Pretty sure I have some old answers using2‹that could benefit from this as well.
$endgroup$
– Grimmy
Sep 6 at 13:45
|
show 2 more comments
$begingroup$
Alternate 13:oL<ʒbIj1Û1¢2‹. Doesn't look like I can get it lower.
$endgroup$
– Grimmy
Sep 6 at 13:03
1
$begingroup$
@Grimy I just hadoL<ʒbIj1ÛSO2‹and was trying to see where my error was. :) But I'm glad to see you aren't able to find a shorter version for one of my answers for a change. ;p (inb4 you find a shorter one after all xD)
$endgroup$
– Kevin Cruijssen
Sep 6 at 13:06
1
$begingroup$
@Grimy I have the feelingSO2‹can be 3 bytes somehow perhaps, but I'm not seeing it and also not entirely sure.. There are some alternatives, likeSO1~orSÆ>d, but I'm unable to find a 3-byter.
$endgroup$
– Kevin Cruijssen
Sep 6 at 13:13
1
$begingroup$
10 with a completely different approach
$endgroup$
– Grimmy
Sep 6 at 13:13
1
$begingroup$
Your feeling was right, I just found a 3-byter:SO!. Pretty sure I have some old answers using2‹that could benefit from this as well.
$endgroup$
– Grimmy
Sep 6 at 13:45
$begingroup$
Alternate 13:
oL<ʒbIj1Û1¢2‹. Doesn't look like I can get it lower.$endgroup$
– Grimmy
Sep 6 at 13:03
$begingroup$
Alternate 13:
oL<ʒbIj1Û1¢2‹. Doesn't look like I can get it lower.$endgroup$
– Grimmy
Sep 6 at 13:03
1
1
$begingroup$
@Grimy I just had
oL<ʒbIj1ÛSO2‹ and was trying to see where my error was. :) But I'm glad to see you aren't able to find a shorter version for one of my answers for a change. ;p (inb4 you find a shorter one after all xD)$endgroup$
– Kevin Cruijssen
Sep 6 at 13:06
$begingroup$
@Grimy I just had
oL<ʒbIj1ÛSO2‹ and was trying to see where my error was. :) But I'm glad to see you aren't able to find a shorter version for one of my answers for a change. ;p (inb4 you find a shorter one after all xD)$endgroup$
– Kevin Cruijssen
Sep 6 at 13:06
1
1
$begingroup$
@Grimy I have the feeling
SO2‹ can be 3 bytes somehow perhaps, but I'm not seeing it and also not entirely sure.. There are some alternatives, like SO1~ or SÆ>d, but I'm unable to find a 3-byter.$endgroup$
– Kevin Cruijssen
Sep 6 at 13:13
$begingroup$
@Grimy I have the feeling
SO2‹ can be 3 bytes somehow perhaps, but I'm not seeing it and also not entirely sure.. There are some alternatives, like SO1~ or SÆ>d, but I'm unable to find a 3-byter.$endgroup$
– Kevin Cruijssen
Sep 6 at 13:13
1
1
$begingroup$
10 with a completely different approach
$endgroup$
– Grimmy
Sep 6 at 13:13
$begingroup$
10 with a completely different approach
$endgroup$
– Grimmy
Sep 6 at 13:13
1
1
$begingroup$
Your feeling was right, I just found a 3-byter:
SO!. Pretty sure I have some old answers using 2‹ that could benefit from this as well.$endgroup$
– Grimmy
Sep 6 at 13:45
$begingroup$
Your feeling was right, I just found a 3-byter:
SO!. Pretty sure I have some old answers using 2‹ that could benefit from this as well.$endgroup$
– Grimmy
Sep 6 at 13:45
|
show 2 more comments
$begingroup$
Retina, 26 bytes
.+
*0
L$w`.(.*)
$.`*1$'1$1
Try it online! Outputs in binary. If that's not acceptable, then for 39 bytes:
.+
*0
L$w`.(.*)
$.`*1$'1$1
+`10
011
%`1
Try it online! Explanation:
.+
*0
Convert the input into a string of n zeros.
L$w`.(.*)
Match all possible non-empty substrings.
$.`*1$'1$1
For each substring, output: the prefix with 0s changed to 1s; the suffix; the match with the initial 0 changed to 1.
+`10
011
%`1
Convert from binary to decimal.
$endgroup$
add a comment
|
$begingroup$
Retina, 26 bytes
.+
*0
L$w`.(.*)
$.`*1$'1$1
Try it online! Outputs in binary. If that's not acceptable, then for 39 bytes:
.+
*0
L$w`.(.*)
$.`*1$'1$1
+`10
011
%`1
Try it online! Explanation:
.+
*0
Convert the input into a string of n zeros.
L$w`.(.*)
Match all possible non-empty substrings.
$.`*1$'1$1
For each substring, output: the prefix with 0s changed to 1s; the suffix; the match with the initial 0 changed to 1.
+`10
011
%`1
Convert from binary to decimal.
$endgroup$
add a comment
|
$begingroup$
Retina, 26 bytes
.+
*0
L$w`.(.*)
$.`*1$'1$1
Try it online! Outputs in binary. If that's not acceptable, then for 39 bytes:
.+
*0
L$w`.(.*)
$.`*1$'1$1
+`10
011
%`1
Try it online! Explanation:
.+
*0
Convert the input into a string of n zeros.
L$w`.(.*)
Match all possible non-empty substrings.
$.`*1$'1$1
For each substring, output: the prefix with 0s changed to 1s; the suffix; the match with the initial 0 changed to 1.
+`10
011
%`1
Convert from binary to decimal.
$endgroup$
Retina, 26 bytes
.+
*0
L$w`.(.*)
$.`*1$'1$1
Try it online! Outputs in binary. If that's not acceptable, then for 39 bytes:
.+
*0
L$w`.(.*)
$.`*1$'1$1
+`10
011
%`1
Try it online! Explanation:
.+
*0
Convert the input into a string of n zeros.
L$w`.(.*)
Match all possible non-empty substrings.
$.`*1$'1$1
For each substring, output: the prefix with 0s changed to 1s; the suffix; the match with the initial 0 changed to 1.
+`10
011
%`1
Convert from binary to decimal.
answered Sep 6 at 22:21
NeilNeil
91.6k8 gold badges47 silver badges190 bronze badges
91.6k8 gold badges47 silver badges190 bronze badges
add a comment
|
add a comment
|
$begingroup$
Brachylog, 27 bytes
1;0|⟦₅;2z^₍ᵐLtT&-₁↰+ᵐ↙T,L,0
Try it online!
Outputs out of order and with duplicates. If that's not okay, tack do onto the end.
$endgroup$
add a comment
|
$begingroup$
Brachylog, 27 bytes
1;0|⟦₅;2z^₍ᵐLtT&-₁↰+ᵐ↙T,L,0
Try it online!
Outputs out of order and with duplicates. If that's not okay, tack do onto the end.
$endgroup$
add a comment
|
$begingroup$
Brachylog, 27 bytes
1;0|⟦₅;2z^₍ᵐLtT&-₁↰+ᵐ↙T,L,0
Try it online!
Outputs out of order and with duplicates. If that's not okay, tack do onto the end.
$endgroup$
Brachylog, 27 bytes
1;0|⟦₅;2z^₍ᵐLtT&-₁↰+ᵐ↙T,L,0
Try it online!
Outputs out of order and with duplicates. If that's not okay, tack do onto the end.
answered Sep 8 at 5:11
Unrelated StringUnrelated String
5,0532 gold badges6 silver badges24 bronze badges
5,0532 gold badges6 silver badges24 bronze badges
add a comment
|
add a comment
|
$begingroup$
Charcoal, 19 bytes
I⮌E⊕θEι⁺⁻X²IθX²ιX²λ
Try it online! Link is to verbose version of code. Explanation:
θ Input
⊕ Incremented
E Map over implicit range
ι Outer index
E Map over implicit range
Iθ Input cast to integer
ι Outer index
λ Inner index
X² X² X² Power of 2
⁺⁻ Subtract and add
⮌ Reverse outer list
I Cast to string
Implicitly print
$endgroup$
add a comment
|
$begingroup$
Charcoal, 19 bytes
I⮌E⊕θEι⁺⁻X²IθX²ιX²λ
Try it online! Link is to verbose version of code. Explanation:
θ Input
⊕ Incremented
E Map over implicit range
ι Outer index
E Map over implicit range
Iθ Input cast to integer
ι Outer index
λ Inner index
X² X² X² Power of 2
⁺⁻ Subtract and add
⮌ Reverse outer list
I Cast to string
Implicitly print
$endgroup$
add a comment
|
$begingroup$
Charcoal, 19 bytes
I⮌E⊕θEι⁺⁻X²IθX²ιX²λ
Try it online! Link is to verbose version of code. Explanation:
θ Input
⊕ Incremented
E Map over implicit range
ι Outer index
E Map over implicit range
Iθ Input cast to integer
ι Outer index
λ Inner index
X² X² X² Power of 2
⁺⁻ Subtract and add
⮌ Reverse outer list
I Cast to string
Implicitly print
$endgroup$
Charcoal, 19 bytes
I⮌E⊕θEι⁺⁻X²IθX²ιX²λ
Try it online! Link is to verbose version of code. Explanation:
θ Input
⊕ Incremented
E Map over implicit range
ι Outer index
E Map over implicit range
Iθ Input cast to integer
ι Outer index
λ Inner index
X² X² X² Power of 2
⁺⁻ Subtract and add
⮌ Reverse outer list
I Cast to string
Implicitly print
answered Sep 6 at 12:34
NeilNeil
91.6k8 gold badges47 silver badges190 bronze badges
91.6k8 gold badges47 silver badges190 bronze badges
add a comment
|
add a comment
|
$begingroup$
Perl 5, 40 bytes
mapsay$-;$-+=2**$_0,0..$_-2;$_--&&redo
Try it online!
$endgroup$
add a comment
|
$begingroup$
Perl 5, 40 bytes
mapsay$-;$-+=2**$_0,0..$_-2;$_--&&redo
Try it online!
$endgroup$
add a comment
|
$begingroup$
Perl 5, 40 bytes
mapsay$-;$-+=2**$_0,0..$_-2;$_--&&redo
Try it online!
$endgroup$
Perl 5, 40 bytes
mapsay$-;$-+=2**$_0,0..$_-2;$_--&&redo
Try it online!
answered Sep 6 at 13:36
GrimmyGrimmy
9,53319 silver badges42 bronze badges
9,53319 silver badges42 bronze badges
add a comment
|
add a comment
|
$begingroup$
Retina, 24 bytes
.+
*0
/0/+<0`(0)1|0$
1$1
Outputs in binary. Input should have a trailing newline.
Attempt at explanation:
.+ #match the entire input
*0 #replace it with that many zeroes
/0/+<0`(0)1|0$ #while the string has a 0, substitute the first match and output
1$1 #if 01 is present in the string, replace it with 10, else replace the last character with $
I tried to avoid the 3 bytes long /0/ regex option by rearranging the options, but couldn't.
Try it online!
$endgroup$
$begingroup$
I don't think outputting in binary is allowed. There's a comment asking if it is allowed, but it's better to assume that you can't until the asker replies
$endgroup$
– Jo King
Sep 7 at 12:00
add a comment
|
$begingroup$
Retina, 24 bytes
.+
*0
/0/+<0`(0)1|0$
1$1
Outputs in binary. Input should have a trailing newline.
Attempt at explanation:
.+ #match the entire input
*0 #replace it with that many zeroes
/0/+<0`(0)1|0$ #while the string has a 0, substitute the first match and output
1$1 #if 01 is present in the string, replace it with 10, else replace the last character with $
I tried to avoid the 3 bytes long /0/ regex option by rearranging the options, but couldn't.
Try it online!
$endgroup$
$begingroup$
I don't think outputting in binary is allowed. There's a comment asking if it is allowed, but it's better to assume that you can't until the asker replies
$endgroup$
– Jo King
Sep 7 at 12:00
add a comment
|
$begingroup$
Retina, 24 bytes
.+
*0
/0/+<0`(0)1|0$
1$1
Outputs in binary. Input should have a trailing newline.
Attempt at explanation:
.+ #match the entire input
*0 #replace it with that many zeroes
/0/+<0`(0)1|0$ #while the string has a 0, substitute the first match and output
1$1 #if 01 is present in the string, replace it with 10, else replace the last character with $
I tried to avoid the 3 bytes long /0/ regex option by rearranging the options, but couldn't.
Try it online!
$endgroup$
Retina, 24 bytes
.+
*0
/0/+<0`(0)1|0$
1$1
Outputs in binary. Input should have a trailing newline.
Attempt at explanation:
.+ #match the entire input
*0 #replace it with that many zeroes
/0/+<0`(0)1|0$ #while the string has a 0, substitute the first match and output
1$1 #if 01 is present in the string, replace it with 10, else replace the last character with $
I tried to avoid the 3 bytes long /0/ regex option by rearranging the options, but couldn't.
Try it online!
answered Sep 7 at 6:10
someonesomeone
2,90614 silver badges35 bronze badges
2,90614 silver badges35 bronze badges
$begingroup$
I don't think outputting in binary is allowed. There's a comment asking if it is allowed, but it's better to assume that you can't until the asker replies
$endgroup$
– Jo King
Sep 7 at 12:00
add a comment
|
$begingroup$
I don't think outputting in binary is allowed. There's a comment asking if it is allowed, but it's better to assume that you can't until the asker replies
$endgroup$
– Jo King
Sep 7 at 12:00
$begingroup$
I don't think outputting in binary is allowed. There's a comment asking if it is allowed, but it's better to assume that you can't until the asker replies
$endgroup$
– Jo King
Sep 7 at 12:00
$begingroup$
I don't think outputting in binary is allowed. There's a comment asking if it is allowed, but it's better to assume that you can't until the asker replies
$endgroup$
– Jo King
Sep 7 at 12:00
add a comment
|
$begingroup$
C (clang), 73 bytes
o,j,y;f(x)for(o=j=0;printf("%d ",o),x;o+=y+!y,y+=y+!y)j=!j?y=0,--x:--j;
Try it online!
for(o=j=0;printf("%d ",o),x; o+=y+!y, y+=y+!y)
// adds 1, 1+1=>2 , 2+2=> 4 .... sequence
j=!j?y=0,--x:--j;
// uses ternary instead of nested loop to decrement 'x' when 'j' go to 0
$endgroup$
add a comment
|
$begingroup$
C (clang), 73 bytes
o,j,y;f(x)for(o=j=0;printf("%d ",o),x;o+=y+!y,y+=y+!y)j=!j?y=0,--x:--j;
Try it online!
for(o=j=0;printf("%d ",o),x; o+=y+!y, y+=y+!y)
// adds 1, 1+1=>2 , 2+2=> 4 .... sequence
j=!j?y=0,--x:--j;
// uses ternary instead of nested loop to decrement 'x' when 'j' go to 0
$endgroup$
add a comment
|
$begingroup$
C (clang), 73 bytes
o,j,y;f(x)for(o=j=0;printf("%d ",o),x;o+=y+!y,y+=y+!y)j=!j?y=0,--x:--j;
Try it online!
for(o=j=0;printf("%d ",o),x; o+=y+!y, y+=y+!y)
// adds 1, 1+1=>2 , 2+2=> 4 .... sequence
j=!j?y=0,--x:--j;
// uses ternary instead of nested loop to decrement 'x' when 'j' go to 0
$endgroup$
C (clang), 73 bytes
o,j,y;f(x)for(o=j=0;printf("%d ",o),x;o+=y+!y,y+=y+!y)j=!j?y=0,--x:--j;
Try it online!
for(o=j=0;printf("%d ",o),x; o+=y+!y, y+=y+!y)
// adds 1, 1+1=>2 , 2+2=> 4 .... sequence
j=!j?y=0,--x:--j;
// uses ternary instead of nested loop to decrement 'x' when 'j' go to 0
answered Sep 13 at 14:26
AZTECCOAZTECCO
1,5151 silver badge19 bronze badges
1,5151 silver badge19 bronze badges
add a comment
|
add a comment
|
$begingroup$
k4, 28 24 bytes
0,+"j"$2 xexp,/-1+|,!:
@Grimy's approach ported to k4
edit: -4 thanks to ngn!
$endgroup$
1
$begingroup$
!:'1+|!:->|,!:
$endgroup$
– ngn
Sep 16 at 16:19
$begingroup$
you can remove the space afterxexp
$endgroup$
– ngn
Sep 16 at 16:21
$begingroup$
@ngn, agh|,!:seems so obvious now that i see it!
$endgroup$
– scrawl
Sep 17 at 10:34
add a comment
|
$begingroup$
k4, 28 24 bytes
0,+"j"$2 xexp,/-1+|,!:
@Grimy's approach ported to k4
edit: -4 thanks to ngn!
$endgroup$
1
$begingroup$
!:'1+|!:->|,!:
$endgroup$
– ngn
Sep 16 at 16:19
$begingroup$
you can remove the space afterxexp
$endgroup$
– ngn
Sep 16 at 16:21
$begingroup$
@ngn, agh|,!:seems so obvious now that i see it!
$endgroup$
– scrawl
Sep 17 at 10:34
add a comment
|
$begingroup$
k4, 28 24 bytes
0,+"j"$2 xexp,/-1+|,!:
@Grimy's approach ported to k4
edit: -4 thanks to ngn!
$endgroup$
k4, 28 24 bytes
0,+"j"$2 xexp,/-1+|,!:
@Grimy's approach ported to k4
edit: -4 thanks to ngn!
edited Sep 17 at 10:33
answered Sep 13 at 15:11
scrawlscrawl
7111 silver badge6 bronze badges
7111 silver badge6 bronze badges
1
$begingroup$
!:'1+|!:->|,!:
$endgroup$
– ngn
Sep 16 at 16:19
$begingroup$
you can remove the space afterxexp
$endgroup$
– ngn
Sep 16 at 16:21
$begingroup$
@ngn, agh|,!:seems so obvious now that i see it!
$endgroup$
– scrawl
Sep 17 at 10:34
add a comment
|
1
$begingroup$
!:'1+|!:->|,!:
$endgroup$
– ngn
Sep 16 at 16:19
$begingroup$
you can remove the space afterxexp
$endgroup$
– ngn
Sep 16 at 16:21
$begingroup$
@ngn, agh|,!:seems so obvious now that i see it!
$endgroup$
– scrawl
Sep 17 at 10:34
1
1
$begingroup$
!:'1+|!: -> |,!:$endgroup$
– ngn
Sep 16 at 16:19
$begingroup$
!:'1+|!: -> |,!:$endgroup$
– ngn
Sep 16 at 16:19
$begingroup$
you can remove the space after
xexp$endgroup$
– ngn
Sep 16 at 16:21
$begingroup$
you can remove the space after
xexp$endgroup$
– ngn
Sep 16 at 16:21
$begingroup$
@ngn, agh
|,!: seems so obvious now that i see it!$endgroup$
– scrawl
Sep 17 at 10:34
$begingroup$
@ngn, agh
|,!: seems so obvious now that i see it!$endgroup$
– scrawl
Sep 17 at 10:34
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%2f191381%2fbit-floating-sequence%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
2
$begingroup$
May we output the sequence in any order (i.e. reversed), or do they have to be sorted from lowest to highest?
$endgroup$
– Kevin Cruijssen
Sep 6 at 12:00
1
$begingroup$
May we output as floats? E.g.
[0.0, 1.0]$endgroup$
– Grimmy
Sep 6 at 13:09
8
$begingroup$
May we output using the binary representation?
$endgroup$
– Neil
Sep 6 at 21:01
$begingroup$
May we output the zero-indexed sequence? i.e.
0 -> [0, 1]$endgroup$
– attinat
Sep 6 at 22:36