13
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ Do you have to put the digits in chunks of four separated by spaces? \$\endgroup\$ – xnor Jul 17 '15 at 6:36
  • \$\begingroup\$ @xnor No. I did for readability, but I will include a bonus opportunity for that as well. \$\endgroup\$ – DDPWNAGE Jul 17 '15 at 6:37
  • \$\begingroup\$ What if I use a data type that is of unbounded size (Python 3's int for instance)? \$\endgroup\$ – isaacg Jul 17 '15 at 6:41
  • \$\begingroup\$ @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. \$\endgroup\$ – DDPWNAGE Jul 17 '15 at 6:43
  • 3
    \$\begingroup\$ What if our langauge uses something different, like 30 bit integers (Haskell) or arbitrary precision integers (J)? \$\endgroup\$ – FUZxxl Jul 17 '15 at 13:01

22 Answers 22

14
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ looks like I shouldn't have tried so hard to get the 4 groupings haha \$\endgroup\$ – protist Jul 17 '15 at 13:21
  • \$\begingroup\$ @protist Don't try to get bonus points that are too hard to implement. It's almost never worth the effort. \$\endgroup\$ – FUZxxl Jul 17 '15 at 13:24
  • \$\begingroup\$ The question specifically asked for a byte count rather than character so, the score should be 20 bytes. \$\endgroup\$ – ankh-morpork Jul 19 '15 at 19:16
  • \$\begingroup\$ @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. \$\endgroup\$ – FUZxxl Jul 19 '15 at 19:23
7
\$\begingroup\$

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>

\$\endgroup\$
6
\$\begingroup\$

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
\$\endgroup\$
  • 2
    \$\begingroup\$ @DDPWNAGE Give other people some time to compete before accepting an answer :) \$\endgroup\$ – orlp Jul 17 '15 at 7:05
  • \$\begingroup\$ Alright, I was thinking about temporarily accepting it, so people know what to beat. \$\endgroup\$ – DDPWNAGE Jul 17 '15 at 7:06
  • 2
    \$\begingroup\$ @DDPWNAGE Remember that you can't easily unaccept an answer after you accepted an answer and waited for a few hours. \$\endgroup\$ – FUZxxl Jul 17 '15 at 15:50
4
\$\begingroup\$

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.

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

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Kade Jul 17 '15 at 15:36
4
\$\begingroup\$

CJam, 13.44 (21 × 0.64)

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

Try it online.

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

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

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

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
\$\endgroup\$
  • 2
    \$\begingroup\$ well you can drop into K ;) k){-1{" "/:,/'$:(0N 4#0b\:x),x}@'1+!x} \$\endgroup\$ – protist Jul 17 '15 at 13:14
3
\$\begingroup\$

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)

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

Ruby, 28 (35 * 0.8)

?1.upto(*$*){|x|puts"%.32b #{x}"%x}
\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ 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.) \$\endgroup\$ – CL- Jul 18 '15 at 15:33
2
\$\begingroup\$

Octave, 23 characters

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

Example output for input 5:

ans =
00000000000000000000000000000001
00000000000000000000000000000010
00000000000000000000000000000011
00000000000000000000000000000100
00000000000000000000000000000101
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ This doesn't print 32-bit / 64-bit integers. \$\endgroup\$ – user0815 Jul 17 '15 at 16:48
  • \$\begingroup\$ Sorry, thanks for the heads up. I have changed the code accordingly. \$\endgroup\$ – Robby Jul 17 '15 at 16:52
  • \$\begingroup\$ This answer is almost entirely identical to the Octave answer. \$\endgroup\$ – Alex A. Jul 17 '15 at 19:30
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

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

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";
\$\endgroup\$
1
\$\begingroup\$

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]
\$\endgroup\$
1
\$\begingroup\$

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.

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

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
\$\endgroup\$
1
\$\begingroup\$

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)")}
\$\endgroup\$
1
\$\begingroup\$

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)}
\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
  • 1
    \$\begingroup\$ Lb32jsäð0:» works for 11 bytes \$\endgroup\$ – Mr. Xcoder Aug 10 '18 at 14:51
  • \$\begingroup\$ @Mr.Xcoder Thanks, completely forgot about using j to prepend so it becomes the correct length. \$\endgroup\$ – Kevin Cruijssen Aug 10 '18 at 15:11

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.