20
\$\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
  • 4
    \$\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

50 Answers 50

1
2
1
\$\begingroup\$

Ruby, 38 bytes

11.times{|i|puts i.to_s(2).rjust 4,?0}
| improve this answer | |
\$\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
1
\$\begingroup\$

BF, 134 bytes

I'm sure this can be shortened--it's pretty much my first BF golf.

++++++++++[>+>+>+++++>>>+++++>>>+++++>>>+++++[<<<]>>>-]>>+>[-->>+>]<<<[<<<]>>[>[>>-[<<+.->]<[>]>-[<<.>]<[>]>++>]<-[<<<-]++<<[<<<]>.>-]

Try it online! Assumes a tape infinite in both directions, like the interpreter at TIO uses. An interpreter where < at the left end of the tape is a no-op would save three bytes.

Explanation

More than half of the code (the first 77 bytes, to be precise) is spent initializing the tape. The steps go like this:

++++++++++
10|

[>+>+>+++++>>>+++++>>>+++++>>>+++++[<<<]>>>-]
 0|10|10|50| 0| 0|50| 0| 0|50| 0| 0|50|

>>+>[-->>+>]<<<[<<<]>>
 0|10|11|48| 0| 1|48| 0| 1|48| 0| 1|48| 0| 1|

The cells initialized to 1 store the bits of our number plus 1: 1 represents a zero bit and 2 represents a one bit.

The initialization phase ended with the pointer on the 11. Now we use this cell to run 11 iterations of our loop:

[>          Move to the first 48
 [>>-       While we're still on a 48, move 2 cells over and decrement
  [         The cell's value now equals the bit it represents; if it's not 0:
   <<+.-    Move to the 48, increment, output, and decrement again
   >        Move to the next cell, which holds a 0
  ]         Leave the loop
  <[>]>     Pointer shenanigans to get back on the cell representing the bit
  -         Decrement again: cell is 255 for a zero bit, 0 for a one bit
  [         If cell is not 0:
   <<.>     Move to the 48, output, and move to the 0 cell
  ]
  <[>]>++   Get back on the bit's cell; increment back to original value
  >         Move to the next 48
 ]          Loop exits once we've output all four bits
            Now we increment the binary number: a one bit turns into a zero bit and
            carries; a zero bit turns into a one bit and doesn't carry
 <-         Move back to the rightmost bit cell and decrement
 [          If it is not 0, it must represent a one
  <<<-      Leave it decremented, go to the next bit cell and decrement it too
 ]          Loop exits on a bit cell that represented a zero
 ++         Increment it twice (to represent a one)
 <<[<<<]    Move back to first cell on tape
 >.         Move to 10 cell and output (newline)
 >-         Move to loop counter cell and decrement
]
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C, 110? bytes

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

Ungolfed:

int f() {
    int n,i,m,k[4]={0};
    for(m=-1;(n=++m)<11;i=0) {
        for(;n;n>>=1)
            k[i++]=n;
        for(i=4;i--;)
            printf("%d",k[i]&1);
        puts("");
    }
}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

SAS, 39 bytes

Not very creative...

data t;do i=0 to 10;put i binary4.;end;
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 44 bytes

(0..10).each{|e|puts (16+e).to_s(2)[-4..-1]}

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Common Lisp, 41 bytes

(dotimes(i 11)(format t"~5,,,'0<~B ~>"i))

Different method than other CL submission.

| improve this answer | |
\$\endgroup\$
1
\$\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.

| improve this answer | |
\$\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\$ – user65167 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\$ – user65167 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\$ – user65167 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
1
\$\begingroup\$

Perl 6, 25 22 bytes

printf "%04b "x 11,^11

Fairly similar to the ruby answer. EDIT: Saved 3 bytes by removing parantheses.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

REXX, 32 bytes

do i=0 to 10
  say x2b(d2x(i))
  end
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Japt, 6 bytes

Aò¤ù'0

Test it

Aò¤ù'0
A          :10
 ò         :Range [0,A]
  ¤        :Convert each to binary
   ù       :left pad each to length of longest
    '0     :  With "0"
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Haskell, 92 bytes

0#_="";b#n=(if even$n`div`b then"0"else"1")++(div b 2#n)
main=putStr.unwords.map(8#)$[0..10]

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Java 8, 77 bytes

()->{for(int i=43;i>=0;i--)System.out.print(((i/4>>i%4)&1)+(i%4==0?" ":""));}

For each number, print one bit at a time by bit shifting then masking with &1.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 22 bytes

printf'%04b 'x11,0..10

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Rust, 37 bytes

||for i in 0..11{print!("{:04b} ",i)}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 57 bytes

: f 11. do 4. do j 3 i - rshift 2 mod 1 .r loop cr loop ;

Try it online!

Explanation

  • Loop from 0 to 10 (inclusive)
    • For each iteration:
    • Loop from 0 to 3, print the character at that index (with no space)
    • Print a newline

Code Explanation

: f                 \ start a new word definition
  11.               \ place 11 and 0 on stack
  do                \ loop from 0 to 10
    4.              \ place 4 and 0 on the stack
    do              \ loop from 0 to 3
      j             \ place outer loop index (current number) on stack
      3 i -         \ get the position of the digit we care about
      rshift 2 mod  \ get the value at that position
      1 .r          \ print the binary digit with no space appended
    loop            \ end the inner loop
    cr              \ print a newline
  loop              \ end the outer loop
;                   \ end the word definition
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Java 8, 195 bytes

interface C{static void main(String[]a){for(int i=0;i++<11;)System.out.println(((java.util.function.Function<String,String>)x->("0000"+x).substring(x.length())).apply(Integer.toString(i-1,2)));}}

I am never golfing in Java again...

-2 bytes thanks to ceilingcat

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @ceilingcat appears you are right, thanks! \$\endgroup\$ – HyperNeutrino Nov 13 '19 at 15:31
1
\$\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

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Shaun Bebbers Feb 7 '19 at 11:06
1
\$\begingroup\$

MathGolf, 9 bytes

Ar☻+mÅà╞n

Try it online.

Explanation:

Ar        # Push a list in the range [0,11): [0,1,2,3,4,5,6,7,8,9,10]
  ☻+      # Add 16 to each: [16,17,18,19,20,21,22,23,24,25,26]
    m     # Map each value to,
     Å    # using the following two commands:
      à   #  Convert the value to a binary string
       ╞  #  And remove the first digit
    n     # After the map: join the list by newlines
          # (after which the entire stack joined together is output implicitly)

The first four bytes could alternatively be A╒E+:

A╒        # Push a list in the range [1,11]: [1,2,3,4,5,6,7,8,9,10,11]
  E+      # Add 15 to each: [16,17,18,19,20,21,22,23,24,25,26]
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

x86-16, IBM PC DOS, 28 bytes

Binary (build NIB.COM with xxd -r):

00000000: b30a 53b1 04d2 e3b8 000e d0d3 1430 cd10  ..S..........0..
00000010: e2f5 b820 0ecd 105b 4b79 e7c3            ... ...[Ky..

Unassembled listing:

B3 0A       MOV  BL, 10         ; set initial counter 
        NIB_LOOP: 
53          PUSH BX             ; save counter in BX 
B1 04       MOV  CL, 4          ; shift / loop 4 times 
D2 E3       SHL  BL, CL         ; shift left one nibble/nybble 
        BIT_LOOP:    
B8 0E00     MOV  AX, 0E00H      ; BIOS write to console / zero AL
D0 D3       RCL  BL, 1          ; rotate left one bit, MSB into carry 
14 30       ADC  AL, '0'        ; add carry bit to '0' ASCII char 
CD 10       INT  10H            ; write to console 
E2 F5       LOOP BIT_LOOP       ; keep looping to next bit 
B8 0E20     MOV  AX, 0E20H      ; BIOS write to console / space 
CD 10       INT  10H            ; write to console 
5B          POP  BX             ; restore counter 
4B          DEC  BX             ; is >= 0? 
79 E7       JNS  NIB_LOOP       ; if so, keep looping 
C3          RET                 ; exit to DOS 

Output

enter image description here

Standalone PC DOS executable .COM program. Output is to console, descending separated by spaces.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Wren, 107 106 bytes

for(i in[0,1])for(j in[0,1])for(k in[0,1])[0,1].each{|l|
System.printAll([i,j,k,l])
if(i==1&&k==1)Fn.x()
}

Try it online!

Explanation

for(i in[0,1])for(j in[0,1])for(k in[0,1])[0,1].each{|l| // Define all those binary digits
System.printAll([i,j,k,l])                                // Print all the binary digits
if(i==1&&k==1)                                            // If the number has reached 10,
              Fn.x()                                      // Execute an undefined command in the Fn class
                                                          // That breaks out of all of the 4 for loops and stops the program
}
| improve this answer | |
\$\endgroup\$
1
2

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.