19
\$\begingroup\$

This challenge is to output to your terminal, window, canvas or screen the numbers zero to 10 inclusive. Each outputted number must be shown as a 4-bit wide nybble, so zero must show as 0000 and so on.

You may separate each number outputted with a space, comma or carriage return. Smallest solution wins but the numbers can be displayed in any order you like as long as there are no repeating numbers in your sequence.

Entries in low-level binary languages need not worry about the comma or white space separators if it is not possible to output with commas or white spaces (i.e., the standard output is limited to binary only, or your solution is for an early computer kit such as the KIM-1 which has a limited digital display).

\$\endgroup\$
  • \$\begingroup\$ Yes, spaces, commas, a comma and then a space or a "\r\n" equivalent in your chosen language. \$\endgroup\$ – Shaun Bebbers Feb 15 '17 at 22:29
  • \$\begingroup\$ No sorry as that looks like 4 individual zero digits and not a 4-bit wide binary number. \$\endgroup\$ – Shaun Bebbers Feb 15 '17 at 22:33
  • \$\begingroup\$ Not that I'm really sure to write such an answer, but would it be OK to output some extra nibbles in addition to the 11 required ones? \$\endgroup\$ – Arnauld Feb 15 '17 at 23:12
  • 2
    \$\begingroup\$ They're nibbles, not nybbles. \$\endgroup\$ – 0WJYxW9FMN Feb 16 '17 at 22:08
  • \$\begingroup\$ Not according to the Commodore 64 Programmers reference guide \$\endgroup\$ – Shaun Bebbers Feb 16 '17 at 22:13

46 Answers 46

1
\$\begingroup\$

SmileBASIC, 26 bytes

FOR I=0TO&HA?BIN$(I,4)NEXT
\$\endgroup\$
15
\$\begingroup\$

MATL, 6 bytes

0:10YB

Try it at MATL Online

Explanation

0:10    % Create the array [0...10]
YB      % Convert this array to a binary string where each number is 
        % placed on a new row
        % Implicitly display the result
\$\endgroup\$
15
\$\begingroup\$

05AB1E, 9 8 bytes

T         # push 10
 4ã       # cartesian product repeat with 4
   R      # reverse list
    T>£   # take the first 11 elements of the list
      »   # join by newline and display

Try it online!

\$\endgroup\$
  • 10
    \$\begingroup\$ Wait... the Cartesian product of the digits of a number? That's just... \$\endgroup\$ – ETHproductions Feb 15 '17 at 22:47
14
\$\begingroup\$

JavaScript, 46 bytes

for(i=15;i++<26;)alert(i.toString(2).slice(1))

Why use a padding function when you can simply add 16 to each number and slice off the first binary digit?

\$\endgroup\$
9
\$\begingroup\$

Japt, 7 bytes

GôA,_¤Å

And here I was thinking Japt was doomed to be longer than every other golfing language...

Test it online!

Explanation

GôA,_¤Å  // Implicit: A = 10, G = 16
GôA      // Create the inclusive range [G...G+A].
    _    // Map each item Z to Z
     ¤   //   .toString(2)
      Å  //   .slice(1).
         // Implicit: output result of last expression

Normally commas can be removed in Japt, but this one is there because of a bug: _ normally means function(Z){Z, but for some reason the compiler thinks A_ means function(A,Z){Z.

\$\endgroup\$
  • \$\begingroup\$ Nice one. I got stuck at Aô_¤ \$\endgroup\$ – Oliver Feb 16 '17 at 1:06
8
\$\begingroup\$

Bash + GNU utils, 26

  • 4 bytes saved thanks to @Dennis
seq -w 0 1010|sed /[2-9]/d

Try it online.

\$\endgroup\$
  • 1
    \$\begingroup\$ seq -w 0 1010 should work. \$\endgroup\$ – Dennis Feb 16 '17 at 1:07
  • \$\begingroup\$ @Dennis Thanks - I don't remember using the -w option to seq before. \$\endgroup\$ – Digital Trauma Feb 16 '17 at 1:23
7
\$\begingroup\$

Bash + Unix utilities, 29 26 bytes

dc -e2o8927II^*8/p|fold -4

Try it online!

This is the same length as @DigitalTrauma/@Dennis's solution, but uses a completely different method.

Output is:

1010
0010
0110
0001
1001
0101
0100
0111
0011
1000
0000

(Any order is allowed.)


Pure Bash, 34 bytes

echo 0{0,1}{0,1}{0,1} 10{00,01,10}

Try the pure Bash version online!

Output is:

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010
\$\endgroup\$
7
\$\begingroup\$

J, 6 bytes

#:i.11

Thanks to miles for cutting it down to 6 bytes!

\$\endgroup\$
  • \$\begingroup\$ #:i.11 should work just as well \$\endgroup\$ – miles Feb 24 '17 at 1:58
  • \$\begingroup\$ I'm not sure this is valid, as per the answer to a now deleted comment. \$\endgroup\$ – Adám Mar 1 '17 at 5:57
  • \$\begingroup\$ @Adám I can't view it. Could you please explain why it's not vaild? \$\endgroup\$ – Blocks Mar 1 '17 at 7:20
  • \$\begingroup\$ Because it generates a n×4 Boolean array, which prints as digits with spaces in-between. But the comment seems to imply that spaces are not allowed inside the binary numbers. \$\endgroup\$ – Adám Mar 1 '17 at 7:22
6
\$\begingroup\$

Python 3.6, 36 35 bytes

i=11
while i:i-=1;print(f"{i:04b}")

-1 byte thanks to @JonathanAllan

Python 3.5 and earlier:

i=11
while i:i-=1;print("{:04b}".format(i))

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ i=11 (new line) while i:i-=1;print(f"{i:04b}"), for 35. \$\endgroup\$ – Jonathan Allan Feb 16 '17 at 1:44
5
\$\begingroup\$

Jelly, 7 bytes

2Bṗ4ṫ6Y

Try it online!

(5 bytes if trailing lines of nybbles are allowed, 2Bṗ4Y)

How?

Prints in descending order.

2Bṗ4ṫ6Y - Main link, no arguments
2B      - 2 converted to binary -> [1,0]
  ṗ4    - Cartesian 4th power -> [[1,1,1,1], [1,1,1,0], ..., [0,0,0,0]]
                            i.e.  16       , 15         ..., 0
    ṫ6  - tail from 6th item  -> [[1,0,1,0], [1,0,0,1], ..., [0,0,0,0]]
                            i.e.  10       , 9        , ..., 0
      Y - join with line feeds
        - implicit print

An alternative 7-byter is 2ṗ4Ịṫ6Y, the [1,0] is replaced with [1,2] and is the "is insignificant" monad (abs(z)<=1), converting 2s to 0s.

\$\endgroup\$
4
\$\begingroup\$

PHP, 33 bytes

while($i<11)printf('%04b ',$i++);
\$\endgroup\$
3
\$\begingroup\$

MATLAB / Octave, 13 bytes

dec2bin(0:10)

Online Demo

\$\endgroup\$
3
\$\begingroup\$

Jelly, 10, 9, 8 bytes

⁴r26BḊ€Y

Try it online!

I'm not that great at jelly, so I'd be open to any tips!

This uses Emigna's first algorithm


Thanks to Dennis for shaving off two bytes making me tie his own answer. :P

Explanation:

      Ḋ€    # Return all but the first element of each item in the list:
⁴r26        #   [16, 17, 18, ... 26]
     B      #   Converted to binary
        Y   # And joined with newlines
\$\endgroup\$
  • \$\begingroup\$ Ḋ€ saves a byte. \$\endgroup\$ – Dennis Feb 15 '17 at 22:53
  • \$\begingroup\$ @Dennis Ah, that makes sense. Thanks! \$\endgroup\$ – DJMcMayhem Feb 15 '17 at 22:53
  • \$\begingroup\$ ⁴r27 saves another one. \$\endgroup\$ – Dennis Feb 15 '17 at 22:58
3
\$\begingroup\$

Python 2, 38 36 bytes

n=16;exec"print bin(n)[3:];n+=1;"*11

Thanks to @DJMcMayhem for golfing off 2 bytes!

Try it online!

\$\endgroup\$
  • \$\begingroup\$ for n in range(11):print bin(n+16)[3:] also at 38 bytes. \$\endgroup\$ – ETHproductions Feb 15 '17 at 23:17
  • \$\begingroup\$ n=16;exec"print bin(n)[3:];n+=1;"*11 is two shorter \$\endgroup\$ – DJMcMayhem Feb 15 '17 at 23:33
  • \$\begingroup\$ @DJMcMayhem It is indeed. Thanks! :) \$\endgroup\$ – Dennis Feb 15 '17 at 23:42
3
\$\begingroup\$

CJam, 12 bytes

B{G+2b1>}%N*

Try it online!

Explanation

The Cartesian power approach would have been my choice, but was already taken.

So this generates numbers from 0 to 10, and for each it adds 16 and converts to binary. Adding 16 ensures that the required leading zeros are produced, together with an extra leading one which is removed.

B             e# Push 11
 {      }%    e# Map over "11", implicitly converted to the array [0 1 ... 10]
  G+          e# Add 16. This makes sure there will be 5 binary digits: a leading 1
              e# which will have to be removed and the remaining, valid digits
    2b        e# Convert to array of binary digits
      1>      e# Remove first digit
          N*  e# Join by newlines. Implicitly converts arrays to strings
\$\endgroup\$
2
\$\begingroup\$

RProgN, 15 Bytes

~16.aL1{2B26q}:

This has been a very good modivation to add a pad function. The entirety of ]L4\-'0'\m\., more than half the code, is to pad.

_Saved 6 bytes thanks to @ETHProductions, that's the pad function cut in half.

Explained

~16.aL1{2B26q}:
~               # Zero Space Segment
 16.            # The literal number 16
    aL          # The length of the Alphabet
      1         # The literal number 1
       {     }: # For each number between 16 and 26 inclusive
        2B      # Convert to base 2
          26q   # Get the characters between 2 and 6 inclusive.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ length of the Alphabet Nice way to save a byte ;-) \$\endgroup\$ – ETHproductions Feb 15 '17 at 23:41
2
\$\begingroup\$

Retina, 36 33 bytes


%%%%
+`(^|\b)%
0$%'¶$%`1
11!`\d+

Try it online!

Explanation


%%%%

Replace the empty (non-existent) input with %%%%.

+`(^|\b)%
0$%'¶$%`1

On the first run of this stage, it will match ^% and essentially replace the text %%%% with the two lines 0%%% and 1%%%. The stage will loop until the output stops changing. On the second run, it will match \b% (since digits count as word characters and % doesn't), and replace the groups by duplicating them and adding 0 to one copy and 1 to the other: 0%%% becomes the lines 00%% and 01%% (and the same sort of thing for 1%%%). Through this loop all 16 bitstrings will be produced, linefeed separated.

11!`\d+

The first 11 matches of \d+ (a run of at least 1 digit) are retrieved. The matches are output in a linefeed-separated list.

\$\endgroup\$
  • \$\begingroup\$ I'm curious in understanding how this 0$%'¶$%1` line works. What do $%, `1, represent? \$\endgroup\$ – Kritixi Lithos Feb 16 '17 at 6:53
  • \$\begingroup\$ @KritixiLithos Sorry I didn't explain the specifics, it's a bit convoluted :P. $%` represents everything before the match on the same line, and $%' is everything after the match on the same line. is a literal linefeed. So basically the replacement matches the first % on a line and replaces it with 0 plus the rest of the line it was on, a newline, the beginning of the line it was on, and a 1. Of course, the beginning and end of the line it was on are untouched by the replacement because they weren't part of the match. \$\endgroup\$ – Business Cat Feb 16 '17 at 14:13
  • \$\begingroup\$ So it's not putting a copy of the line after itself, but rather inserting the end of the line, a newline, and the beginning of the line in between the beginning and end of the line that remain intact. \$\endgroup\$ – Business Cat Feb 16 '17 at 14:15
  • \$\begingroup\$ Ah thanks, that was helpful :) (I'm trying to learn Retina now) \$\endgroup\$ – Kritixi Lithos Feb 16 '17 at 14:30
  • \$\begingroup\$ In which case, I think you can use G11` as the last line of the regex instead \$\endgroup\$ – Kritixi Lithos Feb 16 '17 at 15:26
2
\$\begingroup\$

Ruby, 25 bytes

11.times{|n|puts"%04b"%n}
\$\endgroup\$
2
\$\begingroup\$

BF, 121 101 bytes

,....>,.<...+.>.<-..+.-.>.<..+..>.<-.+.-..>.<.+.-.+.>.<-.+..-.>.<.+...>.<.-...>.<+.-..+.>.<.-.+.-.!0

Requires a trailing newline. Makes use of ! symbol (so, check the box that says !) with this interpreter (try it online!).

Potentially 51 bytes if each operator was considered as 4 bits

\$\endgroup\$
  • \$\begingroup\$ You should specify (or additionally add a byte) for the ! checkbox being enabled. \$\endgroup\$ – Conor O'Brien Feb 16 '17 at 2:32
  • \$\begingroup\$ Whoops, I'm new to that and thought it encoded it in the URL. Will specify... wait, actually, I think it's already specified in the second sentence (?), will clarify that a bit \$\endgroup\$ – Timtech Feb 16 '17 at 11:47
2
\$\begingroup\$

C#, 96 bytes


Golfed

()=>{for(int i=0;i++<11;)System.Console.WriteLine(System.Convert.ToString(i,2).PadLeft(4,'0'));}

Ungolfed

() => {
    for( int i = 0; i++ < 1; )
        System.Console.WriteLine( System.Convert.ToString( i, 2 ).PadLeft( 4, '0' ) );
}

Full code

using System;

namespace Namespace {
    class Program {
        static void Main( string[] args ) {
            m();

            Console.ReadLine();
        }

        static void m() {
            for( Int32 i = 0; i++ < 11; )
                Console.WriteLine(
                    Convert.ToString( i, 2 ). // Converts the number to binary code
                    PadLeft( 4, '0' ) );      // Fills the number with the missing '0's
        }
    }
}

Releases

  • v1.0 - 96 bytes - Initial solution.
\$\endgroup\$
  • \$\begingroup\$ I like the release version you added - are you going to include RC versions as well? \o/ \$\endgroup\$ – Shaun Bebbers Feb 16 '17 at 15:20
  • 1
    \$\begingroup\$ Going to be honest, don't know what RC means... This is how I try to post my solutions in PPCG \$\endgroup\$ – auhmaan Feb 16 '17 at 15:25
  • \$\begingroup\$ RC means 'Release Candidate' - i.e., you'd send out a few versions with minor differences and await to see which is the most stable by your RC number. So if you had version A and version B, you could have v1.0-RCa and v1.0-RCb or something. \$\endgroup\$ – Shaun Bebbers Feb 16 '17 at 15:29
  • 1
    \$\begingroup\$ Oh, that. No. If I make another release, I increment the Version Number right away. \$\endgroup\$ – auhmaan Feb 16 '17 at 15:33
2
\$\begingroup\$

C 170 120 bytes

n,i,m,k[4]={0};f(){for(m=0;m<=10;m++){n=m;i=0;for(;n;i++){k[i]=n;n/=2;}for(i=4;i>0;i--)printf("%d",k[i-1]%2);puts("");}}

Ungolfed version:

void f()
{
    int n,i,m,k[4]={0};


   for(m=0;m<=10;m++)
   {
      n=m;
      i=0;

      for(;n;i++)
      {
         k[i]=n;
         n/=2;
      }  
      for(i=4;i>0;i--)
         printf("%d",k[i-1]%2);

      puts("");        
   }
}

Can definitely be shortened!?

@Ahemone Awesome idea, Thanks!

Should work now! Try it online!

\$\endgroup\$
  • \$\begingroup\$ the first for loop in your golfed version should go to 4 rather than 3, but that doesn't matter because the loop can be eliminated entirely and the second for loop can start from 0. You can also just use while(n), but compacting the while loop down into a for loop saves more again. n/=2 will also save you a byte over the shift. You're also missing a terminating } on the golfed version causing an error on compilation. \$\endgroup\$ – Ahemone Feb 16 '17 at 12:48
  • \$\begingroup\$ @Ahemone Fixed the } and improved the code, 50 bytes shorter based on your idea. \$\endgroup\$ – Abel Tom Feb 16 '17 at 16:07
  • \$\begingroup\$ 102 bytes \$\endgroup\$ – ceilingcat Nov 7 '18 at 0:30
2
\$\begingroup\$

R - 23

We can use intToBin function from the R.utils package:

R.utils::intToBin(0:10)

[1] "0000" "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001" "1010"
\$\endgroup\$
2
\$\begingroup\$

Common Lisp, 41 39 34 32

2+5=7 bytes removed thanks to PrzemysławP.

I am using the FORMAT function function, for printing a number as binary with padding and separators, called with the magic number 8800979740570 (a.k.a. 8012345679A in base 16):

CL-USER> (format t"~,,,4:b"8800979740570)
1000,0000,0001,0010,0011,0100,0101,0110,0111,1001,1010

I used to do the same with 123456789A, but as noted by PrzemysławP, the order does not matter and we can save some bytes. This is because with the original order, the leading zeros would be done as padding and would not be separated into groups of 4 digits; here, by starting with 1000, we don't need any padding. In addition to that, I also removed the mincol argument (53) because I don't need to specify the size of the number anymore.

Formatted output of integers

The documentation for format (see above) is detailed for ~D (decimal base) but the same applies to other ones (hex ~X, binary ~B, octal ~O, custom ~R). Note that for floats there are other arguments.

The most general format is ~mincol,padchar,commachar,commaintervalD, but note that the two last arguments, commachar and commainterval are only meaningful if D has a colon-modifier :D, which by default separate digits by groups of 3 (commainterval is a custom value, instead of 3), inserting the commachar character between such groups, by default a comma.

The mincol argument is one that is found in other format directives and specifies the horizontal space allocated for the thing being printed. When the actual size required is smaller, a padding might be applied (left or right, or both for some directives). The padding fills the remaining space with padchar, a character. When the actual size is bigger, the layout might not be pretty but the object is printed fully.

More precisely (from the above link):

~D Decimal. An arg, which should be an integer, is printed in decimal radix. ~D will never put a decimal point after the number. ~mincolD uses a column width of mincol; spaces are inserted on the left if the number requires fewer than mincol columns for its digits and sign. If the number doesn't fit in mincol columns, additional columns are used as needed.

~mincol,padcharD uses padchar as the pad character instead of space.

The @ modifier causes the number's sign to be printed always; the default is to print it only if the number is negative.

The : modifier causes commas to be printed between groups of three digits

the third prefix parameter may be used to change the character used as the comma. Thus the most general form of ~D is ~mincol,padchar,commacharD.

X3J13 voted in March 1988 (FORMAT-COMMA-INTERVAL) to add a fourth parameter, the commainterval. This must be an integer; if it is not provided, it defaults to 3. This parameter controls the number of digits in each group separated by the commachar.

By extension, each of the ~B, ~O, and ~X directives accepts a commainterval as a fourth parameter, and the ~R directive accepts a commainterval as its fifth parameter.

Padding and separators

Note that the padding characters are not grouped, as explained above. For example, the zeros in front of the numbers are not separated:

 CL-USER> (format t "~10,'0:d" 12345)
 000012,345

... there is no easy way to combine format parameters to output:

 000,012,345

... that's why I first added the zeros and a comma explicitly in the answer. This is not necessary anymore.

\$\endgroup\$
  • 1
    \$\begingroup\$ Nice solution! You can save 2 bytes with: (format t"0000,~49,'0,,4:b"78187493530) - instead of space use comma as separator \$\endgroup\$ – PrzemysławP Feb 24 '17 at 18:00
  • 1
    \$\begingroup\$ Could you explain how does this directive work? ~mincol,padchar, and what later? I know 4 is length of group and :b for binary system. Are there more arguments later? Does it work similiar to ~R? \$\endgroup\$ – PrzemysławP Feb 24 '17 at 18:18
  • 1
    \$\begingroup\$ @PrzemysławP I added some explanations. It is the same for ~R, but with five parameters (the first one is the radix, i.e. the base). \$\endgroup\$ – coredump Feb 24 '17 at 18:43
  • 1
    \$\begingroup\$ (format t"~53,,,4:b"8800979740570) saves additional 5 bytes - uses fact that any order is allowed. \$\endgroup\$ – PrzemysławP Feb 24 '17 at 21:53
  • 1
    \$\begingroup\$ @PrzemysławP That's clever, I didn't pay attention to the order. Btw, I also removed "53" because in fact the size is not necessary. Thanks a lot. \$\endgroup\$ – coredump Feb 25 '17 at 6:55
2
\$\begingroup\$

C, 75 68 69 bytes

Approach 1: 75 73 74 bytes

m;p(i){putchar(i?m&i?49:48:9);}r(){for(m=11;m--;p(4),p(2),p(1),p(0))p(8);}

Try it online!


Approach 2: 68 69 bytes

m,n,o;f(){for(m=11;m--;)for(n=m,o=5;o--;n*=2)putchar(o?n&8?49:48:9);}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Suggest m,n;f(o) instead of m,n,o;f() \$\endgroup\$ – ceilingcat May 9 at 18:18
1
\$\begingroup\$

Python 2, 44 bytes

for x in range(11):print bin(x)[2:].zfill(4)

This uses the zfill function which works like rjust except it always padds with 0 so you don't waste bytes on an argument.

\$\endgroup\$
  • \$\begingroup\$ Wait what, this whole time I've been wasting bytes making my own padding function? (lambda k,l:' '*(len(k)-l)+k) Wow... +1 just because of this :D \$\endgroup\$ – HyperNeutrino Mar 1 '17 at 3:29
1
\$\begingroup\$

Pyke, 8 bytes

TFw0+b2t

Try it here!

TFw0+b2t - for i in range(10):
  w0+    -    i+16
     b2  -   bin(^)
       t -  ^[:-1]

Also 8 bytes:

TF 4@b2t

Try it here!

   4@    - set_bit(4, i)
\$\endgroup\$
1
\$\begingroup\$

Jelly, 8 bytes

2Ḷṗ4ḣ11Y

Try it online!

How it works

2Ḷṗ4ḣ11Y  Main link.

2Ḷ        Unlength 2; yield [0, 1].
  ṗ4      Take the fourth Cartesian power.
    ḣ11   Head 11; discard all but the first eleven elements.
       Y  Join, separating by linefeeds.
\$\endgroup\$
1
\$\begingroup\$

Pyth - 8 7 bytes

<5^_`T4

Try it online here.

\$\endgroup\$
1
\$\begingroup\$

stacked, 30 bytes

11:>[2 baserep'0'4 pad out]map

Try it online!

11:> is a range from 0 to 10. The rest is rather self-explanatory.

Other solutions that I've found:

11:>[bits 4 dpad''join out]map
11:>[bits 4 dpad$tostrmap]map out
11~>15+[bits behead''join out]map
16 26|>[bits behead''join out]map
\$\endgroup\$
1
\$\begingroup\$

Ruby, 38 bytes

11.times{|i|puts i.to_s(2).rjust 4,?0}
\$\endgroup\$
  • \$\begingroup\$ -1 byte by getting rid of the parentheses: 11.times{|i|puts i.to_s(2).rjust 4,?0} \$\endgroup\$ – Conor O'Brien Feb 16 '17 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.