95
\$\begingroup\$

The Challenge

Write a complete program that writes twice as many bytes to standard output as the length of the program.

Rules

  • The program must write ASCII characters to the standard output.

  • The contents of the output doesn't matter.

  • The output, measured in bytes, must be exactly twice the length of the program, also measured in bytes, unless you fulfill the bonus.

  • Any trailing newline is included in the output's byte count.

Bonus

Your program can optionally take a number, n, as input. If so, the output must be exactly n * program length bytes. You can assume that n will always be a positive integer. If no input is provided, n must default to 2.

If you do this, you can subtract 25 bytes from your score.

Shortest program wins.

Restrictions

  • No standard loopholes.

  • The program must be at least 1 byte long.

  • No adding unnecessary whitespace to the source code to change its length. Similarly, comments don't count.

  • Unless you fulfill the bonus, the program must accept no input. If you do fulfill the bonus, the integer must be the only input.

Lowest score (program length in bytes - bonus) wins.

The shortest answer for each language wins for that language.

Leaderboards

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=59436,OVERRIDE_USER=41505;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\-?\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\-?\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
23
  • 1
    \$\begingroup\$ For the bonus, does the output have to be exactly n * program length bytes, or is that a minimum? \$\endgroup\$ – xnor Oct 2 '15 at 23:19
  • 2
    \$\begingroup\$ It has to be exact \$\endgroup\$ – Daniel M. Oct 2 '15 at 23:20
  • 3
    \$\begingroup\$ Looks like the code snippet has to be modified to handle negative scores. \$\endgroup\$ – El'endia Starman Oct 2 '15 at 23:49
  • 43
    \$\begingroup\$ A bonus of -25 is basically mandatory for some languages, since it lets them achieve a negative score. In the future, I'd suggest using a percent bonus, or just making the bonus the question if you really want answers to go for it. Or, just don't have a bonus. \$\endgroup\$ – xnor Oct 2 '15 at 23:58
  • 4
    \$\begingroup\$ For "no input is provided", do we assume the empty string is passed in? I can't see how one would deal with the user never typing in an input and the program just waiting. \$\endgroup\$ – xnor Oct 2 '15 at 23:59

278 Answers 278

1
6 7
8
9 10
0
\$\begingroup\$

Binary-Encoded Golfical, 13+1 (-x flag)=14 bytes

Noncompeting, language postdates the question.

Hexdump:

00 90 01 00 09 17 17 17 17 17 17 17 1D

This encoding can be converted back into the original graphical representation using the included Encoder utility, or run directly by adding the x flag.

Original image:

enter image description here

Magnified 64x, with color labels:

enter image description here

Explanation: Stores 9, prints it seven times (with a newline each time), then turns around and prints it seven more times.

\$\endgroup\$
0
\$\begingroup\$

Mumps, 31 - 25 = 6 bytes

My submission was supposed to be unique (but I hadn't checked all of the answers yet) in that the basic "Assume 2x" code and the bonus-enabled code would work out to be the same length!

Basic code:

F J=1:1:28 W 1

Which is 14 bytes.

Here's the initial bonus-enabled code that takes input, anything that equates to '0' including any non-numeric input assumes 2, then outputs the correct number of output characters. This version is 39 bytes not including the bonus.

R I S:+I=0 I=2 F I=1:1:I F J=1:1:39 W 1

Calculating the bonus 39-25=14 bytes as well!

Too bad (for my narrative) that I looked it over and came up with a shorter version of the bonus code:

R I S:+I=0 I=2 F I=1:1:I*31 W 1

This is only 31 bytes long, so 31-25=6 bytes total, and the header reflects this.

\$\endgroup\$
0
\$\begingroup\$

Mathematica, 9 bytes

Echo[16!]

If I am correct, this should output:

>> 20922789888000

with a trailing newline.

\$\endgroup\$
0
\$\begingroup\$

Gogh, 3 bytes

6GJ

You can run it like this:

./gogh o '6GJ'

Or without implicit output (4 bytes):

8GJ¡

You can run this using:

./gogh "" "8GJ¡"

The inverted exclamation point outputs the TOS.


Explanation

6    “ Push the integer literal 6 ”
G    “ Push a range (0, TOS]      ”
J    “ Join the TOS               ”
\$\endgroup\$
0
\$\begingroup\$

Python 2, 11 bytes

print`0`*21

This outputs 21 zeroes and a newline.

000000000000000000000


From the Python 2 documentation:

A '\n' character is written at the end, unless the print statement ends with a comma.

\$\endgroup\$
0
\$\begingroup\$

Cubix, 15 bytes

Non competing due to language age.

While having some fun with @ETHproductions language, I thought this would be interesting. It turned out a little longer than I thought it would and I'm sure it can be improved.

(.O.NU.\!@O$..u

This maps to a cube with edge length 2

    ( .
    O .
N U . \ ! @ O $
. . u . . . . .
    . .
    . .

Outputs

109998887776665554443332221110

N push 10 to the stack
U turn left moved forward turn left
O output number of TOS
$ skip a no op
( decrement TOS, followed by a number of no ops
u turn right, move forward and turn right, more no ops
O output number of TOS, more no ops
\ reflect to the east
! jump end program @ if truthy
O output number of TOS
$ skip N at start of program

\$\endgroup\$
0
\$\begingroup\$

Lua, 20 bytes (bonus: 11 bytes)

Program:

print(("a"):rep(40))

Output:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

(Change 40 to 39 if newline is counted)

Bonus:

print(("a"):rep(36*(io.read()or 2)))

(If newline is counted, use print(("a"):rep(38*(io.read()or 2)-1)) [13 bytes] instead.)

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! Usually, our community does not like storing input in variables beforehand. It would be better accepted to read from STDIN. \$\endgroup\$ – GamrCorps Mar 30 '16 at 3:47
0
\$\begingroup\$

Underload, 12 bytes

(\/)a:*::**S

Outputs

(\/)(\/)(\/)(\/)(\/)(\/)

Try It Online

Explanation

(\/)            # push \/ to the stack        : \/
    a           # add brackets to TOS         : (\/)
     :*         # duplicate the TOS and join. : (\/)(\/)
       ::       # duplicate a couple of times : (\/)(\/)<>(\/)(\/)<>(\/)(\/)
         **     # join stack items            : (\/)(\/)(\/)(\/)(\/)(\/)
           S    # output TOS
\$\endgroup\$
0
\$\begingroup\$

Tellurium, 10 bytes

µa~Åm16.

Outputs a 16 times. Simple enough.

More detailed explanation:

µ         Begins reading a string
a
~         Stops reading a string and stores it in the selected cell
Å         Command set 2
m         Print the cell's value n times
16
.

There might be a shorter version using Åw but I'm too tired to do that :P

\$\endgroup\$
0
\$\begingroup\$

Java, 85

class A{public static void main(String[]s){for(A a:new A[170])System.out.println();}}
\$\endgroup\$
0
\$\begingroup\$

Perl5, 11 bytes

x is string repetition operator

print 1x22
\$\endgroup\$
1
  • \$\begingroup\$ That's only 10 bytes, and you can use 'say' to cut down on two more (run with -M5.010 flag which is free) \$\endgroup\$ – Gabriel Benamy Oct 23 '16 at 18:44
0
\$\begingroup\$

BASH, 19 bytes

54 bytes -25 bonus. Using the variable input for x repeats:

#!/bin/sh
a=$1;for i in `seq 1 ${a:=2}`;do cat $0;done

similar but more readable:

#!/bin/sh
a=$1
for i in `seq 1 ${a:=2}`; do
 cat $0
done

19 bytes for a simple sh script.

#!/bin/sh
cat $0 $0
\$\endgroup\$
2
  • \$\begingroup\$ Personally I dislike `backticks` a lot, but it saves 1 char. \$\endgroup\$ – CousinCocaine Aug 4 '16 at 16:11
  • 1
    \$\begingroup\$ DARN, "No file/disk/network io allowed", therefor I exclude this answer from the competition. \$\endgroup\$ – CousinCocaine Aug 4 '16 at 16:14
0
\$\begingroup\$

Turtlèd, 8 bytes (non-competing)

15:[*,l]

explanation:

[implicit] initial cell is *, initial char var is *
15       put 15 in register
  :      move right by as many cells as the amount in register
   [*  ] while cell not *
     ,l  write char var, move left 1 cell
\$\endgroup\$
0
\$\begingroup\$

Nim, 27 bytes

echo getStackTrace()[0..51]

Prints the first 53 bytes of the following, plus a trailing newline:

Traceback (most recent call last)
<filename>.nim(1)       <filename>

Note that despite having the filename in the output, this answer's validity is not dependant on the length of the filename. For example, saved in x.nim, this is output:

Traceback (most recent call last)
x.nim(1)         

There are nine trailing spaces there. Conversely, when saved in areallylongfilename.nim, this is output:

Traceback (most recent call last)
areallylongfilena

Newlines are also output in each example. Example usage:

$ nim c length.nim
$ ./length | wc -c
54
\$\endgroup\$
0
\$\begingroup\$

Scala, 52 - 25 = 27 bytes

object A extends App{println("#"*(args(0).toInt*52)}

Simply prints the string # repeated (arg * lengthOfCode) times.

\$\endgroup\$
0
\$\begingroup\$

s-lang, 30 bytes

t[][sssssssssssssss]t[s][ssss]

Link

\$\endgroup\$
0
\$\begingroup\$

Python 3, 20 bytes

print('#'*40,end='')

The optional argument end removes the newline. This is necessary due to universal newline support in Python which generates a \n on Linux and \r\n on Windows.

\$\endgroup\$
0
\$\begingroup\$

TI-Basic, -2 bytes

:Input N
:".
:While N23>length(Ans
:Ans+".
:End
:Ans
\$\endgroup\$
2
  • \$\begingroup\$ Where is the default value? \$\endgroup\$ – Titus Jan 27 '17 at 4:40
  • \$\begingroup\$ TI-Basic doesn't accept empty input. \$\endgroup\$ – Julian Lachniet Jan 27 '17 at 11:33
0
\$\begingroup\$

Juggle, Non-competing, 8 bytes

Golfed: 1<4=a[p]

Ungolfed

1<4    <--- literal for 1 bit-shifted left 4 indices, equal to 16
=a     <--- Set a equal to 16
[      <--- Iterate until a == 0
p      <--- Print a new line
]      <--- Decrement a by 1

This code effectively just outputs 16 \n characters, so there's not much to it.

\$\endgroup\$
0
\$\begingroup\$

Kitanai, 35 bytes

$0[70]#?(neq@0)([sub@1]print"!"&1)%

Just a simple loop to print 70 times the character "!".

\$\endgroup\$
0
\$\begingroup\$

PKod - 6 bytes

Code:
=0+ni6

Explanation:

=0 - Set variable as 0 (ascii code of 0 is 48, thus 48 is stored in the variable)
+n - Add 1 to variable (making it 49 on first iteration) and print ascii char code
i6 - 'i' kicks you back 2 blocks in code (to the '+' sign) until variable equals next char
which is '6'

Output:
495051525354
(note how it prints ascii char code from 1 to 6 (49 50 51 52 53 54) but without spaces.)

\$\endgroup\$
0
\$\begingroup\$

Java - 132 - 25 = 107 bytes

interface o{static void main(String[]a){int i=2,k=0;if(a.length>0)i=Integer.parseInt(a[0]);for(;k<132*i;k++)System.out.print('X');}}

Takes one number(Supposed to be a number, NumberFormatException otherwise) and converts it to an integer, if no input is present, it defaults to 2. Prints X characters.

Ungolfed version:

interface o {
    static void main(String[] a) {
        int i=2, k=0;
        if (a.length > 0)
            i = Integer.parseInt(a[0]);
        for (; k < 132 * i; k++)
            System.out.print('X');
    }
}
\$\endgroup\$
0
\$\begingroup\$

Processing, 31 29 bytes

for(int i=1;i++<34;)print(i);

I stumbled upon this submission from long ago and decided that it can be golfed more.

Outputs all the integers from 2 to 34 in a single line (58 bytes).

2345678910111213141516171819202122232425262728293031323334
\$\endgroup\$
2
  • \$\begingroup\$ I count 31 bytes of code ;) \$\endgroup\$ – Titus Jan 27 '17 at 6:38
  • \$\begingroup\$ @Titus Haha, thanks for noticing that :) \$\endgroup\$ – user41805 Jan 27 '17 at 7:37
0
\$\begingroup\$

ForceLang, 13 bytes

io.write 1e25
\$\endgroup\$
0
\$\begingroup\$

Alice, 10 bytes - 25 = -15

2/
&oi@/a*

Try it online!

Prints a 0x02 byte and 10n-1 null bytes when given an argument n, and simply 20 null bytes when no argument is given.

Explanation

2   Push 2 as the default input.
/   Reflect to SE. Switch to Ordinal.
i   Read all input as a string. Pushes "" if no input is given.
    Reflect off bottom boundary, move NE.
    Reflect off top boundary, move SE.
/   Reflect to E. Switch to Cardinal
a   Push 10, the length of the source code.
*   Multiply. The first argument this pops is 10. But two pop another integer 
    argument, some implicit conversion needs to happen. If an input was given,
    that string is popped and converted to an integer, which will be used as
    the second argument. But if no input was given, the value "" contains no 
    integers, so it's discarded and Alice pops the next value instead (the
    default value 2 we pushed earlier).
    IP wraps around to the first column.
&   Repeat the next command that many times.
o   Pop that many values and print them as bytes.
i   Try to read more input, irrelevant.
@   Terminate the program.

Some alternatives

Without the bonus, we can do this in 4 bytes, printing 8 null bytes:

8&o@

Simple enough. There's a really fun and 6 byte solution with readable output though:

 /
O@n

This prints Jabberwocky with a trailing linefeed. The reason is that n is logical not, and the "canonical" truthy (non-empty) string used by Alice when applying this command to a falsy (empty) string is "Jabberwocky". So...

/   Reflect to SE. Switch to Ordinal.
n   Logical NOT. Pops an implicit empty string from the stack and turns it into
    "Jabberwocky".
    Reflect off bottom right corner, move back NW.
/   Reflect to W. Switch to Cardinal.
    IP wraps around to the last column.
/   Reflect to NW. Switch to Ordinal.
    Reflect immediately off top boundary, move SW.
O   Print "Jabberwocky" with a trailing linefeed.
    Reflect off bottom left corner, move back NE.
/   Reflect to S. Switch to Cardinal.
@   Terminate the program.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 2 bytes

!7

Prints 7 fatorial, or 5040.

If you include the trailing newline then use:

!6

Which would print 720.

\$\endgroup\$
0
\$\begingroup\$

k, 2 bytes

This is one byte, but I don't like the answer because it doesn't feel "proper".

1

When run in oK as a file (as opposed to interactively) it prints out a newline after printing the 1.


Another sort-of cheating answer:

11

If run in the closed-source k interpreter, it would output 11 followed by a newline and a space (and it would expect the next command).


$1

This would result in ,"1" if trailing whitespace was not counted.

\$\endgroup\$
0
\$\begingroup\$

Fourier - Non-competing, 12 - 25 = -13 bytes

Fourier is newer than this challenge.

I*12(1ox^~x)

Works by multiplying the input by 12 (the length of the program) and outputting the number 1 that many times.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Braingolf, (9 - 25) = -16 bytes

91+<9*1-^

Takes n input and outputs a 1 followed by x zeroes where x = (n * 9) - 1

Both of these are non-competing

Braingolf, 5 bytes

91+9^

Outputs 1000000000 (10**9)

\$\endgroup\$
1
  • \$\begingroup\$ This is non-competing. \$\endgroup\$ – Erik the Outgolfer May 17 '17 at 16:47
0
\$\begingroup\$

Powershell 4 Bytes

,1*8

This prints '1' 8 times in the console window.

\$\endgroup\$
1
6 7
8
9 10

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.