Half, Half Half, and, Half
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
Consider the following number sequence:
$ 0, frac12, frac14, frac34, frac18, frac38, frac58, frac78, frac116, frac316, frac516, frac716, frac916, frac1116, frac1316, frac1516, frac132, frac332, frac532, dots $
It enumerates all binary fractions in the unit interval $ [0, 1) $.
(To make this challenge easier, the first element is optional: You may skip it and consider the sequence starts with 1/2.)
Task
Write a program (complete program or a function) which...
Choose one of these behaviors:
- Input n, output nth element of the sequence (0-indexed or 1-indexed);
- Input n, output first n elements of the sequence;
- Input nothing, output the infinite number sequence one by one;
Rule
- Your program should at least support first 1000 items;
- You may choose to output decimal, built-in fraction format, or, a pair of decimal integers as you like;
- Input / Output as binary numbers is not allowed in this question;
- This is code-golf, shortest codes win;
- Standard loopholes disallowed.
Testcases
input output
1 1/2 0.5
2 1/4 0.25
3 3/4 0.75
4 1/8 0.125
10 5/16 0.3125
100 73/128 0.5703125
511 511/512 0.998046875
512 1/1024 0.0009765625
These examples are based on 0-indexed sequence with the leading 0 included. You would need to adjust the input for fitting your solution.
Read More
OEIS A006257- Josephus problem: $ a_2n = 2a_n-1, a_2n+1 = 2a_n+1 $. (Formerly M2216)
- 0, 1, 1, 3, 1, 3, 5, 7, 1, 3, 5, 7, 9, 11, 13, 15, 1, 3, 5, ...
OEIS A062383- $ a_0 = 1 $: for $ n>0 $, $ a_n = 2^lfloor log_2n+1 rfloor $ or $ a_n = 2a_lfloor fracn2 rfloor $.
- 1, 2, 4, 4, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 32, 32, 32, ...
A006257(n)/A062383(n) = (0, 0.1, 0.01, 0.11, 0.001, ...) enumerates all binary fractions in the unit interval [0, 1). - Fredrik Johansson, Aug 14 2006
code-golf number sequence integer rational-numbers
add a comment |Â
up vote
8
down vote
favorite
Consider the following number sequence:
$ 0, frac12, frac14, frac34, frac18, frac38, frac58, frac78, frac116, frac316, frac516, frac716, frac916, frac1116, frac1316, frac1516, frac132, frac332, frac532, dots $
It enumerates all binary fractions in the unit interval $ [0, 1) $.
(To make this challenge easier, the first element is optional: You may skip it and consider the sequence starts with 1/2.)
Task
Write a program (complete program or a function) which...
Choose one of these behaviors:
- Input n, output nth element of the sequence (0-indexed or 1-indexed);
- Input n, output first n elements of the sequence;
- Input nothing, output the infinite number sequence one by one;
Rule
- Your program should at least support first 1000 items;
- You may choose to output decimal, built-in fraction format, or, a pair of decimal integers as you like;
- Input / Output as binary numbers is not allowed in this question;
- This is code-golf, shortest codes win;
- Standard loopholes disallowed.
Testcases
input output
1 1/2 0.5
2 1/4 0.25
3 3/4 0.75
4 1/8 0.125
10 5/16 0.3125
100 73/128 0.5703125
511 511/512 0.998046875
512 1/1024 0.0009765625
These examples are based on 0-indexed sequence with the leading 0 included. You would need to adjust the input for fitting your solution.
Read More
OEIS A006257- Josephus problem: $ a_2n = 2a_n-1, a_2n+1 = 2a_n+1 $. (Formerly M2216)
- 0, 1, 1, 3, 1, 3, 5, 7, 1, 3, 5, 7, 9, 11, 13, 15, 1, 3, 5, ...
OEIS A062383- $ a_0 = 1 $: for $ n>0 $, $ a_n = 2^lfloor log_2n+1 rfloor $ or $ a_n = 2a_lfloor fracn2 rfloor $.
- 1, 2, 4, 4, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 32, 32, 32, ...
A006257(n)/A062383(n) = (0, 0.1, 0.01, 0.11, 0.001, ...) enumerates all binary fractions in the unit interval [0, 1). - Fredrik Johansson, Aug 14 2006
code-golf number sequence integer rational-numbers
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
Consider the following number sequence:
$ 0, frac12, frac14, frac34, frac18, frac38, frac58, frac78, frac116, frac316, frac516, frac716, frac916, frac1116, frac1316, frac1516, frac132, frac332, frac532, dots $
It enumerates all binary fractions in the unit interval $ [0, 1) $.
(To make this challenge easier, the first element is optional: You may skip it and consider the sequence starts with 1/2.)
Task
Write a program (complete program or a function) which...
Choose one of these behaviors:
- Input n, output nth element of the sequence (0-indexed or 1-indexed);
- Input n, output first n elements of the sequence;
- Input nothing, output the infinite number sequence one by one;
Rule
- Your program should at least support first 1000 items;
- You may choose to output decimal, built-in fraction format, or, a pair of decimal integers as you like;
- Input / Output as binary numbers is not allowed in this question;
- This is code-golf, shortest codes win;
- Standard loopholes disallowed.
Testcases
input output
1 1/2 0.5
2 1/4 0.25
3 3/4 0.75
4 1/8 0.125
10 5/16 0.3125
100 73/128 0.5703125
511 511/512 0.998046875
512 1/1024 0.0009765625
These examples are based on 0-indexed sequence with the leading 0 included. You would need to adjust the input for fitting your solution.
Read More
OEIS A006257- Josephus problem: $ a_2n = 2a_n-1, a_2n+1 = 2a_n+1 $. (Formerly M2216)
- 0, 1, 1, 3, 1, 3, 5, 7, 1, 3, 5, 7, 9, 11, 13, 15, 1, 3, 5, ...
OEIS A062383- $ a_0 = 1 $: for $ n>0 $, $ a_n = 2^lfloor log_2n+1 rfloor $ or $ a_n = 2a_lfloor fracn2 rfloor $.
- 1, 2, 4, 4, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 32, 32, 32, ...
A006257(n)/A062383(n) = (0, 0.1, 0.01, 0.11, 0.001, ...) enumerates all binary fractions in the unit interval [0, 1). - Fredrik Johansson, Aug 14 2006
code-golf number sequence integer rational-numbers
Consider the following number sequence:
$ 0, frac12, frac14, frac34, frac18, frac38, frac58, frac78, frac116, frac316, frac516, frac716, frac916, frac1116, frac1316, frac1516, frac132, frac332, frac532, dots $
It enumerates all binary fractions in the unit interval $ [0, 1) $.
(To make this challenge easier, the first element is optional: You may skip it and consider the sequence starts with 1/2.)
Task
Write a program (complete program or a function) which...
Choose one of these behaviors:
- Input n, output nth element of the sequence (0-indexed or 1-indexed);
- Input n, output first n elements of the sequence;
- Input nothing, output the infinite number sequence one by one;
Rule
- Your program should at least support first 1000 items;
- You may choose to output decimal, built-in fraction format, or, a pair of decimal integers as you like;
- Input / Output as binary numbers is not allowed in this question;
- This is code-golf, shortest codes win;
- Standard loopholes disallowed.
Testcases
input output
1 1/2 0.5
2 1/4 0.25
3 3/4 0.75
4 1/8 0.125
10 5/16 0.3125
100 73/128 0.5703125
511 511/512 0.998046875
512 1/1024 0.0009765625
These examples are based on 0-indexed sequence with the leading 0 included. You would need to adjust the input for fitting your solution.
Read More
OEIS A006257- Josephus problem: $ a_2n = 2a_n-1, a_2n+1 = 2a_n+1 $. (Formerly M2216)
- 0, 1, 1, 3, 1, 3, 5, 7, 1, 3, 5, 7, 9, 11, 13, 15, 1, 3, 5, ...
OEIS A062383- $ a_0 = 1 $: for $ n>0 $, $ a_n = 2^lfloor log_2n+1 rfloor $ or $ a_n = 2a_lfloor fracn2 rfloor $.
- 1, 2, 4, 4, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 32, 32, 32, ...
A006257(n)/A062383(n) = (0, 0.1, 0.01, 0.11, 0.001, ...) enumerates all binary fractions in the unit interval [0, 1). - Fredrik Johansson, Aug 14 2006
code-golf number sequence integer rational-numbers
code-golf number sequence integer rational-numbers
asked 48 mins ago
tsh
7,1101941
7,1101941
add a comment |Â
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
4
down vote
MathGolf, 5 bytes
kâ«kâ¨]
Assuming implicit input (does not work on TIO currently)
_â«â¨] ("_" duplicates TOS, "" swaps the top two elements)
Try it online!
Explanation
k Read input as int
â« Left-rotate all bits in input
k Read input as int
⨠Round up to nearest power of 2
] Wrap in array (just for pretty printing)
I took my inspiration from this question to solve the problem, my "own" solution was around 10-12 bytes I think.
I had intended for the round up to closest power of 2 to return the number itself if it was a number of two, but due to a mistake it rounds to the next power of two (e.g. 4 -> 8 instead of 4 -> 4). This will have to be fixed later, but now it saves me one byte.
@user202729 Yes, thanks for noticing. I updated the explanation.
â maxb
8 mins ago
add a comment |Â
up vote
2
down vote
05AB1E, 11 bytes
[N>oDÃÂ
ÃÂs/û,
Try it online!
add a comment |Â
up vote
2
down vote
Jelly, 9 bytes
Bá¹Â1á¸Â,æÃÂ2$
Try it online!
add a comment |Â
up vote
2
down vote
PowerShell, 40 bytes
for($i=2;;$i*=2)?$_%2
Try it online!
Outputs the infinite sequence as decimal values. Given language limitations, will eventually run into precision problems, but easily handles the first 1000 entries.
Starts by setting $i=2
, then enters a for
loop. Each iteration, we construct a range from 1..$i
and pull out the odd values with |?$_%2
. Those are fed into their own inner loop, where we divide each to get the decimal |%$_/$i
. Those are left on the pipeline and output when the pipeline is flushed after every for
iteration. Each iteration we're simply incrementing $i
by $i*=2
to get the next go-round.
add a comment |Â
up vote
2
down vote
Haskell, 35 bytes
[(y,x)|x<-map(2^)[1..],y<-[1,3..x]]
This is an infinite list of integer pairs.
Try it online!
add a comment |Â
up vote
0
down vote
Java 10, 89 85 bytes
v->for(double i=1,j,t;;)for(t=Math.pow(2,i++),j=1;j<t;j+=2)System.out.println(j/t);
Port of @Emigma's 05AB1E answer, so outputs decimals indefinitely as well.
Try it online.
Explanation:
v-> // Method with empty unused parameter and no return-type
for(double i=1,j,t;;) // Loop `i` from 1 upwards indefinitely
for(t=Math.pow(2,i++), // Set `t` to 2 to the power `i`
j=1;j<t;j+=2) // Inner loop `j` in the range [1, `t`) in steps of 2
System.out.println( // Print with trailing new-line:
j/t); // `j` divided by `t`
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
MathGolf, 5 bytes
kâ«kâ¨]
Assuming implicit input (does not work on TIO currently)
_â«â¨] ("_" duplicates TOS, "" swaps the top two elements)
Try it online!
Explanation
k Read input as int
â« Left-rotate all bits in input
k Read input as int
⨠Round up to nearest power of 2
] Wrap in array (just for pretty printing)
I took my inspiration from this question to solve the problem, my "own" solution was around 10-12 bytes I think.
I had intended for the round up to closest power of 2 to return the number itself if it was a number of two, but due to a mistake it rounds to the next power of two (e.g. 4 -> 8 instead of 4 -> 4). This will have to be fixed later, but now it saves me one byte.
@user202729 Yes, thanks for noticing. I updated the explanation.
â maxb
8 mins ago
add a comment |Â
up vote
4
down vote
MathGolf, 5 bytes
kâ«kâ¨]
Assuming implicit input (does not work on TIO currently)
_â«â¨] ("_" duplicates TOS, "" swaps the top two elements)
Try it online!
Explanation
k Read input as int
â« Left-rotate all bits in input
k Read input as int
⨠Round up to nearest power of 2
] Wrap in array (just for pretty printing)
I took my inspiration from this question to solve the problem, my "own" solution was around 10-12 bytes I think.
I had intended for the round up to closest power of 2 to return the number itself if it was a number of two, but due to a mistake it rounds to the next power of two (e.g. 4 -> 8 instead of 4 -> 4). This will have to be fixed later, but now it saves me one byte.
@user202729 Yes, thanks for noticing. I updated the explanation.
â maxb
8 mins ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
MathGolf, 5 bytes
kâ«kâ¨]
Assuming implicit input (does not work on TIO currently)
_â«â¨] ("_" duplicates TOS, "" swaps the top two elements)
Try it online!
Explanation
k Read input as int
â« Left-rotate all bits in input
k Read input as int
⨠Round up to nearest power of 2
] Wrap in array (just for pretty printing)
I took my inspiration from this question to solve the problem, my "own" solution was around 10-12 bytes I think.
I had intended for the round up to closest power of 2 to return the number itself if it was a number of two, but due to a mistake it rounds to the next power of two (e.g. 4 -> 8 instead of 4 -> 4). This will have to be fixed later, but now it saves me one byte.
MathGolf, 5 bytes
kâ«kâ¨]
Assuming implicit input (does not work on TIO currently)
_â«â¨] ("_" duplicates TOS, "" swaps the top two elements)
Try it online!
Explanation
k Read input as int
â« Left-rotate all bits in input
k Read input as int
⨠Round up to nearest power of 2
] Wrap in array (just for pretty printing)
I took my inspiration from this question to solve the problem, my "own" solution was around 10-12 bytes I think.
I had intended for the round up to closest power of 2 to return the number itself if it was a number of two, but due to a mistake it rounds to the next power of two (e.g. 4 -> 8 instead of 4 -> 4). This will have to be fixed later, but now it saves me one byte.
edited 1 min ago
answered 36 mins ago
maxb
1,209519
1,209519
@user202729 Yes, thanks for noticing. I updated the explanation.
â maxb
8 mins ago
add a comment |Â
@user202729 Yes, thanks for noticing. I updated the explanation.
â maxb
8 mins ago
@user202729 Yes, thanks for noticing. I updated the explanation.
â maxb
8 mins ago
@user202729 Yes, thanks for noticing. I updated the explanation.
â maxb
8 mins ago
add a comment |Â
up vote
2
down vote
05AB1E, 11 bytes
[N>oDÃÂ
ÃÂs/û,
Try it online!
add a comment |Â
up vote
2
down vote
05AB1E, 11 bytes
[N>oDÃÂ
ÃÂs/û,
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
05AB1E, 11 bytes
[N>oDÃÂ
ÃÂs/û,
Try it online!
05AB1E, 11 bytes
[N>oDÃÂ
ÃÂs/û,
Try it online!
answered 43 mins ago
Emigna
43k431131
43k431131
add a comment |Â
add a comment |Â
up vote
2
down vote
Jelly, 9 bytes
Bá¹Â1á¸Â,æÃÂ2$
Try it online!
add a comment |Â
up vote
2
down vote
Jelly, 9 bytes
Bá¹Â1á¸Â,æÃÂ2$
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Jelly, 9 bytes
Bá¹Â1á¸Â,æÃÂ2$
Try it online!
Jelly, 9 bytes
Bá¹Â1á¸Â,æÃÂ2$
Try it online!
answered 42 mins ago
Dennisâ¦
182k32291722
182k32291722
add a comment |Â
add a comment |Â
up vote
2
down vote
PowerShell, 40 bytes
for($i=2;;$i*=2)?$_%2
Try it online!
Outputs the infinite sequence as decimal values. Given language limitations, will eventually run into precision problems, but easily handles the first 1000 entries.
Starts by setting $i=2
, then enters a for
loop. Each iteration, we construct a range from 1..$i
and pull out the odd values with |?$_%2
. Those are fed into their own inner loop, where we divide each to get the decimal |%$_/$i
. Those are left on the pipeline and output when the pipeline is flushed after every for
iteration. Each iteration we're simply incrementing $i
by $i*=2
to get the next go-round.
add a comment |Â
up vote
2
down vote
PowerShell, 40 bytes
for($i=2;;$i*=2)?$_%2
Try it online!
Outputs the infinite sequence as decimal values. Given language limitations, will eventually run into precision problems, but easily handles the first 1000 entries.
Starts by setting $i=2
, then enters a for
loop. Each iteration, we construct a range from 1..$i
and pull out the odd values with |?$_%2
. Those are fed into their own inner loop, where we divide each to get the decimal |%$_/$i
. Those are left on the pipeline and output when the pipeline is flushed after every for
iteration. Each iteration we're simply incrementing $i
by $i*=2
to get the next go-round.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
PowerShell, 40 bytes
for($i=2;;$i*=2)?$_%2
Try it online!
Outputs the infinite sequence as decimal values. Given language limitations, will eventually run into precision problems, but easily handles the first 1000 entries.
Starts by setting $i=2
, then enters a for
loop. Each iteration, we construct a range from 1..$i
and pull out the odd values with |?$_%2
. Those are fed into their own inner loop, where we divide each to get the decimal |%$_/$i
. Those are left on the pipeline and output when the pipeline is flushed after every for
iteration. Each iteration we're simply incrementing $i
by $i*=2
to get the next go-round.
PowerShell, 40 bytes
for($i=2;;$i*=2)?$_%2
Try it online!
Outputs the infinite sequence as decimal values. Given language limitations, will eventually run into precision problems, but easily handles the first 1000 entries.
Starts by setting $i=2
, then enters a for
loop. Each iteration, we construct a range from 1..$i
and pull out the odd values with |?$_%2
. Those are fed into their own inner loop, where we divide each to get the decimal |%$_/$i
. Those are left on the pipeline and output when the pipeline is flushed after every for
iteration. Each iteration we're simply incrementing $i
by $i*=2
to get the next go-round.
answered 22 mins ago
AdmBorkBork
24.4k359214
24.4k359214
add a comment |Â
add a comment |Â
up vote
2
down vote
Haskell, 35 bytes
[(y,x)|x<-map(2^)[1..],y<-[1,3..x]]
This is an infinite list of integer pairs.
Try it online!
add a comment |Â
up vote
2
down vote
Haskell, 35 bytes
[(y,x)|x<-map(2^)[1..],y<-[1,3..x]]
This is an infinite list of integer pairs.
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Haskell, 35 bytes
[(y,x)|x<-map(2^)[1..],y<-[1,3..x]]
This is an infinite list of integer pairs.
Try it online!
Haskell, 35 bytes
[(y,x)|x<-map(2^)[1..],y<-[1,3..x]]
This is an infinite list of integer pairs.
Try it online!
answered 20 mins ago
nimi
29.9k31881
29.9k31881
add a comment |Â
add a comment |Â
up vote
0
down vote
Java 10, 89 85 bytes
v->for(double i=1,j,t;;)for(t=Math.pow(2,i++),j=1;j<t;j+=2)System.out.println(j/t);
Port of @Emigma's 05AB1E answer, so outputs decimals indefinitely as well.
Try it online.
Explanation:
v-> // Method with empty unused parameter and no return-type
for(double i=1,j,t;;) // Loop `i` from 1 upwards indefinitely
for(t=Math.pow(2,i++), // Set `t` to 2 to the power `i`
j=1;j<t;j+=2) // Inner loop `j` in the range [1, `t`) in steps of 2
System.out.println( // Print with trailing new-line:
j/t); // `j` divided by `t`
add a comment |Â
up vote
0
down vote
Java 10, 89 85 bytes
v->for(double i=1,j,t;;)for(t=Math.pow(2,i++),j=1;j<t;j+=2)System.out.println(j/t);
Port of @Emigma's 05AB1E answer, so outputs decimals indefinitely as well.
Try it online.
Explanation:
v-> // Method with empty unused parameter and no return-type
for(double i=1,j,t;;) // Loop `i` from 1 upwards indefinitely
for(t=Math.pow(2,i++), // Set `t` to 2 to the power `i`
j=1;j<t;j+=2) // Inner loop `j` in the range [1, `t`) in steps of 2
System.out.println( // Print with trailing new-line:
j/t); // `j` divided by `t`
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Java 10, 89 85 bytes
v->for(double i=1,j,t;;)for(t=Math.pow(2,i++),j=1;j<t;j+=2)System.out.println(j/t);
Port of @Emigma's 05AB1E answer, so outputs decimals indefinitely as well.
Try it online.
Explanation:
v-> // Method with empty unused parameter and no return-type
for(double i=1,j,t;;) // Loop `i` from 1 upwards indefinitely
for(t=Math.pow(2,i++), // Set `t` to 2 to the power `i`
j=1;j<t;j+=2) // Inner loop `j` in the range [1, `t`) in steps of 2
System.out.println( // Print with trailing new-line:
j/t); // `j` divided by `t`
Java 10, 89 85 bytes
v->for(double i=1,j,t;;)for(t=Math.pow(2,i++),j=1;j<t;j+=2)System.out.println(j/t);
Port of @Emigma's 05AB1E answer, so outputs decimals indefinitely as well.
Try it online.
Explanation:
v-> // Method with empty unused parameter and no return-type
for(double i=1,j,t;;) // Loop `i` from 1 upwards indefinitely
for(t=Math.pow(2,i++), // Set `t` to 2 to the power `i`
j=1;j<t;j+=2) // Inner loop `j` in the range [1, `t`) in steps of 2
System.out.println( // Print with trailing new-line:
j/t); // `j` divided by `t`
answered 5 mins ago
Kevin Cruijssen
29.9k552165
29.9k552165
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172171%2fhalf-half-half-and-half%23new-answer', 'question_page');
);
Post as a guest
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
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
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