# Intro:

I remember, when I was a kid, I would get a calculator and keep on pressing the + button, and see how high I could count. Now, I like to program, and I'm developing for iOS.

Counting is a fundamental skill for both humans and computers alike to do. Without it, the rest of math can't be done. It's done simply by starting at 1 and repetitively adding 1 to it.

# The challenge:

This is but a simple challenge. What I would like your program to do is print from 1 to whatever Integer it takes in. However, I'll throw a twist in it, since decimal counting is kinda boring:

The counting cannot be in base 10, it has to show itself counting in binary.

So, to count to 5, using 32-bit integers, it would look like this:

0000 0000 0000 0000 0000 0000 0000 0001 ..... 1
0000 0000 0000 0000 0000 0000 0000 0010 ..... 2
0000 0000 0000 0000 0000 0000 0000 0011 ..... 3
0000 0000 0000 0000 0000 0000 0000 0100 ..... 4
0000 0000 0000 0000 0000 0000 0000 0101 ..... 5


It's a computer. They know binary best. Your input can be either a 32-bit or 64-bit integer. It is truly up to you. However, if you use 32-bit integers, your output must be 32-bit integers in binary, and if you use 64-bit integers, your output must be 64-bit integers in binary.

# Sample input:

a 32-bit integer, 5

# Sample output:

0000 0000 0000 0000 0000 0000 0000 0001
0000 0000 0000 0000 0000 0000 0000 0010
0000 0000 0000 0000 0000 0000 0000 0011
0000 0000 0000 0000 0000 0000 0000 0100
0000 0000 0000 0000 0000 0000 0000 0101


# Scoring:

Your score is equal to however many bytes your code is. As this is Code Golf, lowest score wins.

## Bonus points:

If you show, in the output, the number it's at as a base 10 number (for example, 0000 0000 0000 0000 0000 0000 0000 0001 in binary is equal to the base 10 1), multiply your score by 0.8.

If you group 4 digits of output like I did, then multiply your score by 0.8 (again). This isn't required.

Do not round up, and do not round down. Your score is a floating-point number.

# Good luck!

• Do you have to put the digits in chunks of four separated by spaces?
– xnor
Commented Jul 17, 2015 at 6:36
• @xnor No. I did for readability, but I will include a bonus opportunity for that as well. Commented Jul 17, 2015 at 6:37
• What if I use a data type that is of unbounded size (Python 3's int for instance)? Commented Jul 17, 2015 at 6:41
• @isaacg Specify whether your program uses 32 or 64-bit integers. You don't have to accommodate for those integers that are out of bounds; you only have to output what you input. In other words, the choice is yours. Commented Jul 17, 2015 at 6:43
• What if our langauge uses something different, like 30 bit integers (Haskell) or arbitrary precision integers (J)? Commented Jul 17, 2015 at 13:01

## APL, 10 characters

Another on in APL. Assumes⎕IO←1 (the default). No bonus points. Reads the number from the input device. If your APL uses 64 bit integers instead of 32 bit integers, substitute 64 for 32 as needed.

Notice that APL transparently converts to floating point numbers when the range of an integer is exceeded. It's hard thus to exactly say what integer size APL works with.

⍉(32⍴2)⊤⍳⎕


### explanation

2          ⍝ the number 2
32⍴2       ⍝ a vector of 32 twos.
(32⍴2)⊤X   ⍝ X represented as base 2 to 32 digits precision
⍳X         ⍝ a vector of the integers from 1 to X
⎕          ⍝ a number queried from the terminal
(32⍴2)⊤⍳⎕  ⍝ the output we want, flipped by 90°
⍉(32⍴2)⊤⍳⎕ ⍝ the output we want in correct orientation (⍉ is transpose)

• looks like I shouldn't have tried so hard to get the 4 groupings haha Commented Jul 17, 2015 at 13:21
• @protist Don't try to get bonus points that are too hard to implement. It's almost never worth the effort. Commented Jul 17, 2015 at 13:24
• The question specifically asked for a byte count rather than character so, the score should be 20 bytes. Commented Jul 19, 2015 at 19:16
• @dohaqatar7 There are encodings of APL (like codepage 907) that fit the entire APL character set into one byte. There are some APL extensions that cannot be encoded with the traditional APL code pages, but I don't use any of them. Commented Jul 19, 2015 at 19:23

# JavaScript (ES6) 56.8 (71*0.8)

32 bit version, as JavaScript can not handle 64 bit precision (at most 53 bits using floating point doubles)

Without grouping

f=n=>{for(i=0;i++<n;)console.log((8*(8<<26)+i).toString(2).slice(1),i)}


With grouping - score 60.16 (94*.64)

f=n=>{for(i=0;i++<n;)console.log((8*(8<<26)+i).toString(2).slice(1).match(/..../g).join ,i)}


Test in any browser (ES5)

function f(n)
{
for(i=0;i++<n;)console.log((8*(8<<26)+i).toString(2).substr(1).match(/..../g).join(' '),i)
}

// Test
console.log = function(x,y) { O.innerHTML += x+' '+y+'\n' }
Count to: <input id=I><button onclick="O.innerHTML='';f(+I.value)">-></button>
<pre id=O></pre>

# Pyth, 18 * 0.8 * 0.8 = 11.52 bytes

VSQjd+c.[64.BN\04N


Example output:

0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 1
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 2
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 3
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0100 4
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0101 5
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0110 6
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0111 7
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1000 8
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1001 9
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1010 10

• @DDPWNAGE Give other people some time to compete before accepting an answer :)
– orlp
Commented Jul 17, 2015 at 7:05
• Alright, I was thinking about temporarily accepting it, so people know what to beat. Commented Jul 17, 2015 at 7:06
• @DDPWNAGE Remember that you can't easily unaccept an answer after you accepted an answer and waited for a few hours. Commented Jul 17, 2015 at 15:50

# Pyth, 19 * 0.8 * 0.8 = 12.16 bytes

VSQjd+cjk.[032.BN4N


Example output for input 5:

0000 0000 0000 0000 0000 0000 0000 0001 1
0000 0000 0000 0000 0000 0000 0000 0010 2
0000 0000 0000 0000 0000 0000 0000 0011 3
0000 0000 0000 0000 0000 0000 0000 0100 4
0000 0000 0000 0000 0000 0000 0000 0101 5


Demonstration.

## Python 2, 48 * 0.8 = 38.4

i=0;exec"i+=1;print format(i,'032b'),i;"*input()


Converts a number to binary, uses string formatting to convert it to binary with 32 digits, and then also prints the decimal number for the bonus. Uses an exec loop to increment from 1 to the input value.

• Nice solution! I don't believe it was specified, but this will fail on large numbers in the 32-bit range: OverflowError: repeated string is too long. Not sure if that's a limit on just my machine though.
Commented Jul 17, 2015 at 15:36

# CJam, 13.44 (21 × 0.64)

ri{)_2b64Ue[4/S*S@N}/


Try it online.

# APL, 23.68 (37 × .8 × .8)

{⎕←(⍕⍵),⍨⊃,/,/' ',⍨⍕¨8 4⍴(32⍴2)⊤⍵}¨⍳⎕


# KDB(Q), 50 * 0.8 * 0.8 = 32

I feel a bit sad with my submission :( There should be a better way to do this!

{-1{" "sv raze@'string(0N 4#0b vs x),x}@'1+til x;}


# Explanation

                                         1+til x     / counting
{                                  }@'            / lambda each
(0N 4#0b vs x),x               / convert to binary and join with input
" "sv raze@'string                               / convert to string, concatenate each string and join with space
{-1                                             ;}   / print and surpress output in lambda


# Test

q){-1{" "sv raze@'string(0N 4#0b vs x),x}@'1+til x;}5
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 1
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 2
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 3
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0100 4
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0101 5

• well you can drop into K ;) k){-1{" "/:,/'$:(0N 4#0b\:x),x}@'1+!x} Commented Jul 17, 2015 at 13:14 # Common Lisp, 96.0 Score: (* 150 .8 .8) (lambda(y)(flet((p(n &aux(x(format()"~39,'0b ~:*~d"n)))(dolist(p'(4 9 14 19 24 29 34))(setf(aref x p)#\ ))(princ x)(terpri)))(dotimes(i y)(p(1+ i)))))  ## Example Calling the function with 10: 0000 0000 0000 0000 0000 0000 0000 0001 1 0000 0000 0000 0000 0000 0000 0000 0010 2 0000 0000 0000 0000 0000 0000 0000 0011 3 0000 0000 0000 0000 0000 0000 0000 0100 4 0000 0000 0000 0000 0000 0000 0000 0101 5 0000 0000 0000 0000 0000 0000 0000 0110 6 0000 0000 0000 0000 0000 0000 0000 0111 7 0000 0000 0000 0000 0000 0000 0000 1000 8 0000 0000 0000 0000 0000 0000 0000 1001 9 0000 0000 0000 0000 0000 0000 0000 1010 10  ## Explanation (format()"~39,'0b ~:*~d" #b101010101010) gives: "000000000000000000000000000101010101010 2730"  The intermediate string (an array) is modified to put a space character at the following zero-based indices: 4 9 14 19 24 29 34. Then it is printed. Note that the apparently straightforward (format t"~39,'0,' ,4:b ~:*~d" #b101010101010) format does not do what we want. It prints: 00000000000000000000000001010 1010 1010 2730  (the padding is not grouped by 4) # Ruby, 28 (35 * 0.8) ?1.upto(*$*){|x|puts"%.32b #{x}"%x}


# C, 97 * 0.8 * 0.8 = 62.08

a,x;main(b){for(scanf("%u",&b);a++<b;printf("%d\n",a))for(x=32;x--;)printf("%*d",x%-4-2,a>>x&1);}


Example output for input "5":

0000 0000 0000 0000 0000 0000 0000 0001 1
0000 0000 0000 0000 0000 0000 0000 0010 2
0000 0000 0000 0000 0000 0000 0000 0011 3
0000 0000 0000 0000 0000 0000 0000 0100 4
0000 0000 0000 0000 0000 0000 0000 0101 5
0000 0000 0000 0000 0000 0000 0000 0110 6
0000 0000 0000 0000 0000 0000 0000 0111 7
0000 0000 0000 0000 0000 0000 0000 1000 8
0000 0000 0000 0000 0000 0000 0000 1001 9


I could add one more whitespace character to separate the decimal numbers from the binary numbers, but technically the problem doesn't require it, I think? EDIT: Thanks, CL!

• Incidentally you can replace x%-4-1 with x%-4-2 to add the space between the binary and decimal for no extra byte cost. (This would also get rid of the extra space at the beginning of each line.)
– CL-
Commented Jul 18, 2015 at 15:33

# Octave, 23 characters

dec2bin(1:input(""),32)


Example output for input 5:

ans =
00000000000000000000000000000001
00000000000000000000000000000010
00000000000000000000000000000011
00000000000000000000000000000100
00000000000000000000000000000101


# MatLab, 19 bytes

@(x)dec2bin(1:x,32)


Not too much to this one, MatLab has a built in decimal to binary converter and automatically prints the result.

• This doesn't print 32-bit / 64-bit integers. Commented Jul 17, 2015 at 16:48
• Sorry, thanks for the heads up. I have changed the code accordingly. Commented Jul 17, 2015 at 16:52
• This answer is almost entirely identical to the Octave answer. Commented Jul 17, 2015 at 19:30

# Julia, 42 bytes

This is a bit shorter without the bonuses.

n->for i=1:n println(lpad(bin(i),64,0))end


This creates an unnamed function that takes an integer and prints the binary representation of each number from 1 to n, each left padded with zeros to 64 characters.

### With bonuses, 78 bytes * 0.8 * 0.8 = 49.92

n->for i=1:n for j=1:4:64 print(lpad(bin(i),64,0)[j:j+3]*" ")end;println(i)end


This creates an unnamed function that takes an integer and prints the binary representation as before, this time split into groups of 4 with the number in base 10 at the end.

# Common Lisp, score: 64.0

100 bytes * 0.8 * 0.8

I'm pretty happy with my score, but I still feel like there should be a possibility to simplify my code a bit.

## Output

0000 0000 0000 0000 0000 0000 0000 0001  1
0000 0000 0000 0000 0000 0000 0000 0010  2
0000 0000 0000 0000 0000 0000 0000 0011  3
0000 0000 0000 0000 0000 0000 0000 0100  4
0000 0000 0000 0000 0000 0000 0000 0101  5
0000 0000 0000 0000 0000 0000 0000 0110  6
0000 0000 0000 0000 0000 0000 0000 0111  7
0000 0000 0000 0000 0000 0000 0000 1000  8
0000 0000 0000 0000 0000 0000 0000 1001  9
0000 0000 0000 0000 0000 0000 0000 1010  10


## Code

(defun r(n)(dotimes(i n)(format t"~{~a~a~a~a ~}~a~%"(coerce(format()"~32,'0B"(1+ i))'list)(1+ i))))


## Explanation

As described in coredump's answer, the format string

"~32,'0B"


does output base2 numbers but there seems to be no possibility to get the grouping right as well. Hence I coerce the string into a list and iterate over it by picking out groups of 4 with this format string:

"~{~a~a~a~a ~}~a~%"

After each group of 4 there's a blank, and after the last group, the base10 number is printed.

## Without grouping (60x0.8 => 48.0)

(defun r(n)(dotimes(i n)(format t"~32,'0B ~:*~a~%"(1+ i))))


This uses ~:* to process the (single) format argument again.

## PHP, 51.84 (81 × .8 × .8)

32-bit version, as PHP is limited to only 32-bit on Windows regardless of whether the OS is 64-bit.

Takes one command line argument.

for($i=0;$i++<$argv[1];)echo chunk_split(str_pad(decbin($i),32,0,0),4," ")."$i\n";  ## CoffeeScript, 60.8 (76 × .8) 32-bit version for reasons mentioned above, as CoffeeScript compiles down to JavaScript. f=(x)->console.log(("0".repeat(32)+i.toString 2).slice(-32),i)for i in[1..x]  With grouping it becomes slightly longer: 64.64 (101 × .8 × .8) f=(x)->console.log(("0".repeat(32)+i.toString 2).slice(-32).match(/.{4}/g).join(" "),i)for i in[1..x]  # Haskell, 56 bytes f n=putStr$unlines$take n$tail$sequence$replicate 32"01"


Usage:

*Main> f 5
00000000000000000000000000000001
00000000000000000000000000000010
00000000000000000000000000000011
00000000000000000000000000000100
00000000000000000000000000000101


For 64bit, replace the 32 with 64. Every other number works, too.

## J, 20 bytes

(32#2)#:>:i.".1!:1<1


Sample input and output:

3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1


# Swift: 98.56 (154 * 0.8 * 0.8)

for x in 1...Int(Process.arguments[1].toInt()!){var p=String(x,radix:2)
let q=count(p)
for i in 0..<32-q{p=(((q+i)%4==0) ?"0 ":"0")+p}
println("\(p) \(x)")}


## Ruby, 64 bit

### 70 * 0.8 * 0.8 = 44.8 bytes (split, decimal)

1.upto(gets.to_i){|i|puts ("%064d"%i.to_s 2).scan(/.{4}/)*?\s+" #{i}"}


### 51 * 0.8 = 40.8 bytes (decimal)

1.upto(gets.to_i){|i|puts "%064d"%i.to_s(2)+" #{i}"}


### 67 * 0.8 = 53.6 bytes (split)

1.upto(gets.to_i){|i|puts "%064d"%i.to_s(2).scan/.{4}/}


### 44 bytes (no bonuses)

1.upto(gets.to_i){|i|puts "%064d"%i.to_s(2)}


# 05AB1E, 13 11 bytes

Lb32jsäð0:»


-2 bytes thanks to @Mr.Xcoder.

Outputs without space-delimiter nor sequence-number.

Try it online.

Explanation:

L              # List of range [1,input]
#  i.e. 5 → [1,2,3,4,5]
b             # Convert each to a binary string
#  i.e. [1,2,3,4,5] → ['1','10','11','100','101']
32j          # Join everything together with a minimum length per item of 32,
# which basically prepends spaces to make it length 32
#  i.e. ['1','10','11','100','101'] → '                               1                              10                              11                             100                             101'
sä        # Split it into the input amount of parts
#  i.e. 5 → ['                               1','                              10','                              11','                             100','                             101']
ð0:     # Replace every space with a 0
#  i.e. '                             101' → '00000000000000000000000000000101'
»    # Join everything together by newlines (and output implicitly)

• Lb32jsäð0:» works for 11 bytes Commented Aug 10, 2018 at 14:51
• @Mr.Xcoder Thanks, completely forgot about using j to prepend so it becomes the correct length. Commented Aug 10, 2018 at 15:11

# Japt-R, 6 bytes

õÈ¤ùTH


Try it

õÈ¤ùTH     :Implicit input of integer
õ          :Range [1,input]
È         :Map each X
¤        :  To binary string
T      :    with 0
H     :    to length 32
:Implicit output joined with newlines


## With bonuses, 8.96 bytes

õÈ¤ùTH ò4 pX ¸


Try it

õÈ¤ùTH             :As above
ò4          :Partitions of length 4
pX       :Push X
¸     :Join with spaces


# JavaScript, 50 bytes

f=n=>n&&print(n.toString(2).padStart(32,0,f(n-1)))


Try it online!

## With first bonus, 41.6 bytes

f=n=>n&&print(n.toString(2).padStart(32,0,f(n-1)),n)


Try it online!

## With both bonuses, 44.8 bytes

f=n=>n&&print(...n.toString(2).padStart(32,0,f(n-1)).match(/.{4}/g),n)


Try it online!

# Vyxalj, 76 5 bytes

vΠ₆↳›


Try it Online!

The benefits of having built-ins to pad things with spaces and to convert spaces to 0s. Uses 64 bit integers.

two can play at the flag game Japt.

## Explained

vΠ₆↳›
vΠ      # Convert each number in the range [1, input] to binary
₆↳    # left pad each binary string with spaces so that it's length 32
›   # replace spaces with 0s
# join on newlines because of -j flag