21
\$\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\$
6
  • \$\begingroup\$ Yes, spaces, commas, a comma and then a space or a "\r\n" equivalent in your chosen language. \$\endgroup\$ Commented Feb 15, 2017 at 22:29
  • \$\begingroup\$ No sorry as that looks like 4 individual zero digits and not a 4-bit wide binary number. \$\endgroup\$ Commented Feb 15, 2017 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
    Commented Feb 15, 2017 at 23:12
  • 4
    \$\begingroup\$ They're nibbles, not nybbles. \$\endgroup\$
    – 0WJYxW9FMN
    Commented Feb 16, 2017 at 22:08
  • \$\begingroup\$ Not according to the Commodore 64 Programmers reference guide \$\endgroup\$ Commented Feb 16, 2017 at 22:13

51 Answers 51

16
\$\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\$
1
  • 10
    \$\begingroup\$ Wait... the Cartesian product of the digits of a number? That's just... \$\endgroup\$ Commented Feb 15, 2017 at 22:47
12
\$\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\$
1
  • \$\begingroup\$ Nice one. I got stuck at Aô_¤ \$\endgroup\$
    – Oliver
    Commented Feb 16, 2017 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\$
2
  • 1
    \$\begingroup\$ seq -w 0 1010 should work. \$\endgroup\$
    – Dennis
    Commented Feb 16, 2017 at 1:07
  • \$\begingroup\$ @Dennis Thanks - I don't remember using the -w option to seq before. \$\endgroup\$ Commented Feb 16, 2017 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\$
4
  • \$\begingroup\$ #:i.11 should work just as well \$\endgroup\$
    – miles
    Commented Feb 24, 2017 at 1:58
  • \$\begingroup\$ I'm not sure this is valid, as per the answer to a now deleted comment. \$\endgroup\$
    – Adám
    Commented Mar 1, 2017 at 5:57
  • \$\begingroup\$ @Adám I can't view it. Could you please explain why it's not vaild? \$\endgroup\$
    – Blocks
    Commented Mar 1, 2017 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
    Commented Mar 1, 2017 at 7:22
6
\$\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\$
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
  • 1
    \$\begingroup\$ i=11 (new line) while i:i-=1;print(f"{i:04b}"), for 35. \$\endgroup\$ Commented Feb 16, 2017 at 1:44
4
\$\begingroup\$

PHP, 33 bytes

while($i<11)printf('%04b ',$i++);
\$\endgroup\$
4
\$\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\$
4
\$\begingroup\$

Intel 8080 machine code, MITS Altair 8800, 10 bytes

00 00 00 00 00 00 00 00 76

Listing:

0000   00   NOP      
0001   00   NOP      
0002   00   NOP      
0003   00   NOP      
0004   00   NOP      
0005   00   NOP      
0006   00   NOP      
0007   00   NOP      
0008   00   NOP      
0009   00   NOP      
000A   76   HLT 

Uses the CPU's program counter (PC, aka instruction pointer) to count from 0 to 10 then halt. The output display is conveniently in nibble format already.

Output:

Output is displayed on Blinkenlights A3-A0.

enter image description here

Try it online!

\$\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\$
3
  • \$\begingroup\$ Ḋ€ saves a byte. \$\endgroup\$
    – Dennis
    Commented Feb 15, 2017 at 22:53
  • \$\begingroup\$ @Dennis Ah, that makes sense. Thanks! \$\endgroup\$
    – DJMcMayhem
    Commented Feb 15, 2017 at 22:53
  • \$\begingroup\$ ⁴r27 saves another one. \$\endgroup\$
    – Dennis
    Commented Feb 15, 2017 at 22:58
3
\$\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\$
0
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\$
3
  • \$\begingroup\$ for n in range(11):print bin(n+16)[3:] also at 38 bytes. \$\endgroup\$ Commented Feb 15, 2017 at 23:17
  • \$\begingroup\$ n=16;exec"print bin(n)[3:];n+=1;"*11 is two shorter \$\endgroup\$
    – DJMcMayhem
    Commented Feb 15, 2017 at 23:33
  • \$\begingroup\$ @DJMcMayhem It is indeed. Thanks! :) \$\endgroup\$
    – Dennis
    Commented Feb 15, 2017 at 23:42
2
\$\begingroup\$

SmileBASIC, 26 bytes

FOR I=0TO&HA?BIN$(I,4)NEXT
\$\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\$
1
  • \$\begingroup\$ length of the Alphabet Nice way to save a byte ;-) \$\endgroup\$ Commented Feb 15, 2017 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\$
5
  • \$\begingroup\$ I'm curious in understanding how this 0$%'¶$%1` line works. What do $%, `1, represent? \$\endgroup\$
    – user41805
    Commented Feb 16, 2017 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\$ Commented Feb 16, 2017 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\$ Commented Feb 16, 2017 at 14:15
  • \$\begingroup\$ Ah thanks, that was helpful :) (I'm trying to learn Retina now) \$\endgroup\$
    – user41805
    Commented Feb 16, 2017 at 14:30
  • \$\begingroup\$ In which case, I think you can use G11` as the last line of the regex instead \$\endgroup\$
    – user41805
    Commented Feb 16, 2017 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\$
2
  • \$\begingroup\$ You should specify (or additionally add a byte) for the ! checkbox being enabled. \$\endgroup\$ Commented Feb 16, 2017 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
    Commented Feb 16, 2017 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\$
4
  • \$\begingroup\$ I like the release version you added - are you going to include RC versions as well? \o/ \$\endgroup\$ Commented Feb 16, 2017 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
    Commented Feb 16, 2017 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\$ Commented Feb 16, 2017 at 15:29
  • 1
    \$\begingroup\$ Oh, that. No. If I make another release, I increment the Version Number right away. \$\endgroup\$
    – auhmaan
    Commented Feb 16, 2017 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\$
3
  • \$\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
    Commented Feb 16, 2017 at 12:48
  • \$\begingroup\$ @Ahemone Fixed the } and improved the code, 50 bytes shorter based on your idea. \$\endgroup\$
    – Abel Tom
    Commented Feb 16, 2017 at 16:07
  • \$\begingroup\$ 102 bytes \$\endgroup\$
    – ceilingcat
    Commented Nov 7, 2018 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\$

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\$
1
  • \$\begingroup\$ Suggest m,n;f(o) instead of m,n,o;f() \$\endgroup\$
    – ceilingcat
    Commented May 9, 2019 at 18:18
2
\$\begingroup\$

C, 68 bytes

f(){puts("0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010");}

Somehow, this is the shortest C answer so far…

EDIT: I was missing 1000 somehow. Well, it’s still winning.

\$\endgroup\$
1
  • \$\begingroup\$ Isn't this missing 8? (1000) \$\endgroup\$ Commented Mar 13, 2017 at 17:07
2
\$\begingroup\$

Commodore VIC-20/VC-20/C64 - 95 69 68 67 tokenized BASIC bytes used (obfuscated and minimized)

0dEfnb(x)=sgn(xandb):fOi=0to10:b=8:fOj=0to3:?rI(stR(fnb(i)),1);:b=b/2:nE:?:nE

Copy and paste the above text into WinVice, for instance. Just about fits onto 80 characters on a Commodore 64.

Here's the non-obfuscated symbolic listing for illustrative purposes:

0 DEF FN B(X)=SGN(X AND B)
1 FOR I=0 TO 10
2  LET B=8
3  FOR J=0 TO 3
4   PRINT RIGHT$(STR$(FN B(I)),1);
5   LET B=B/2
6  NEXT J
7  PRINT
8 NEXT I

Of course this may be minimised and further obfuscated. Explanation:

The DEF FN command in line zero is a simple function which accepts one numeric parameter. This will give the algebraic sign the number passed to it as a parameter, in this case is ANDed with the value B (declared elsewhere, but becomes global once declared).

Line 1 starts the loop counter I

Line 2 sets the value of the 3rd BIT.

Line 3 starts loop counter J (we want bits 0 - 3 essentially).

Line 4 gets the STR$ value (string value) of the function FN B(I), this is then passed to the RIGHT$ command as CBM BASIC auto-pads outputted numbers with a white space, so we get the last character in the string created by STR$.

Line 5 moves the BIT counter down one (like x >> 1 in C I suppose).

Line 6 moves the J loop up one.

Line 7 prints a new line (line echo PHP_EOL; in PHP).

Line 8 moves the I loop up one.

I've 'nested' the listing in the example to better show the loop and loop order.

C64 counting in nybbles

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, I've done this in the obfuscated version above (although not the NEXT J one) - I'll give it a go for that as well, saving another BASIC byte! \$\endgroup\$ Commented Feb 7, 2019 at 11:06
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\$
1
  • \$\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
    Commented Mar 1, 2017 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\$

Pyth - 8 7 bytes

<5^_`T4

Try it online here.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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