105
\$\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\$
25
  • 1
    \$\begingroup\$ For the bonus, does the output have to be exactly n * program length bytes, or is that a minimum? \$\endgroup\$
    – xnor
    Commented Oct 2, 2015 at 23:19
  • 2
    \$\begingroup\$ It has to be exact \$\endgroup\$
    – Daniel M.
    Commented Oct 2, 2015 at 23:20
  • 3
    \$\begingroup\$ Looks like the code snippet has to be modified to handle negative scores. \$\endgroup\$ Commented Oct 2, 2015 at 23:49
  • 44
    \$\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
    Commented Oct 2, 2015 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
    Commented Oct 2, 2015 at 23:59

308 Answers 308

1 2
3
4 5
11
3
\$\begingroup\$

APL (Dyalog Classic), 5 bytes

⎕←1e9

Try it online!

Explanation

Not very interesting... but I hope this hasn't been posted before.

   1e9 ⍝ This returns the constant 1000000000
⎕←    ⍝ Move this to the output buffer
\$\endgroup\$
3
\$\begingroup\$

Flobnar, 27 - 25 = 2 bytes

*< _.v\@
 *:|\<>
9&- <
3210

Try it online!

The decimal input command & defaults to its south (2) if it fails to read a number. Prints 27 times input copies of 0.

Flobnar, 8 bytes

0.!____@

Try it online!

Prints 16 copies of 0.

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

Jelly, -21 bytes

o2⁴x

The source code is 4 bytes long and qualifies for the -25 bytes bonus. Output is a list of 16's.

Try it online! without input or with input.

How it works

o2⁴x  Main link.
      If there's input, the left argument is set to that input.
      Otherwise, it defaults to 0.
o2    Logical OR of the left argument and 2.
      The return value is now either the input or 2.
  ⁴x  Repeat 16 that many times, creating an array.

16 is two bytes long, and n copies are separated by n - 1 instances of . That, plus the two bytes of [ and ] gives 2n + 2(n - 1) + 2 = 4n bytes of output.

\$\endgroup\$
1
  • \$\begingroup\$ It seems that you have (ab)used array representations here. \$\endgroup\$ Commented Sep 17, 2016 at 9:21
3
\$\begingroup\$

Python 3 (PyPy), 13 bytes

print('h'*26)

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Golf! The same language name/code is duplicated twice, is that intentional? \$\endgroup\$
    – rydwolf
    Commented Jun 1, 2023 at 17:26
  • \$\begingroup\$ @RydwolfPrograms there is a special build of python called pypy. This also works in real python. \$\endgroup\$ Commented Jun 2, 2023 at 8:51
  • \$\begingroup\$ This outputs 27 bytes to stdout, you should change it to *25 \$\endgroup\$
    – Jo King
    Commented Jun 29, 2023 at 0:29
3
\$\begingroup\$

ForWhile 6 bytes

6(9##)

prints 12 tabs

online interpreter

Explanation

6(   \ start for-loop with 6 iterations
9##  \ print tab twice
)    \ end for-loop
\$\endgroup\$
2
\$\begingroup\$

Ruby, 6 bytes

p ?a*9

Prints "aaaaaaaaa" (including the quotes) and a newline.

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

JavaScript (ES6), 23 bytes - 25 = -2

(n=2)=>'x'.repeat(23*n)

Just for fun here's a quine version with a score of 27 bytes -25 =2

a=(n=2)=>`a=${a}`.repeat(n)
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Does this really write to standard output? \$\endgroup\$ Commented Oct 3, 2015 at 0:26
  • \$\begingroup\$ It's a function return, using fat arrow notation. \$\endgroup\$
    – bren
    Commented Oct 3, 2015 at 2:25
  • 3
    \$\begingroup\$ The spec requires a full program... \$\endgroup\$ Commented Oct 3, 2015 at 16:39
  • 2
    \$\begingroup\$ Running this program just gives me a function. Its output is a function that the length isn't double, nor does it take standard input. \$\endgroup\$
    – Unihedron
    Commented Oct 4, 2015 at 12:57
  • \$\begingroup\$ Writing functions that return the required result, as well as taking input from function arguments, is allowed. \$\endgroup\$
    – ivzem
    Commented May 24, 2017 at 12:50
2
\$\begingroup\$

JavaScript (ES6), 50-25=25

a=prompt();alert("0".repeat(48*(isNaN(a|0)?2:+a)))
\$\endgroup\$
0
2
\$\begingroup\$

PHP 7, 9 Bytes

<?=str_repeat(1,($argv[1]??2)*34);

Uses the short opening tag with echo <?= and the null coalesce operator ?? which falls back to 2 if $argv[1] is not set, without throwing an error.

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

Element, 16 bytes - 25 bonus = -9

_2:1<[2]16*'[X`]

Explanation:

_2:1<[2]16*'[X`]
_                  take input
 2:                duplicate it
   1<              test "if less than one" (like an empty input)
     [2]           FOR/IF push 2 if true
        16*        multiply by 16
           '       move result to control stack
            [  ]   FOR
             X`    output the letter X

Element, 6 bytes

9 12^`

prints 282429536481

Explanation:

9       push 9
  12^   to the twelfth power
     `  output
\$\endgroup\$
2
\$\begingroup\$

Pyth, 8 - 25 = -17 bytes

mU2?zvz2

Demonstration

Note: This only works in the online compiler, because it removes the trailng newline.

Output form:

[[0, 1], [0, 1], [0, 1]]
\$\endgroup\$
0
2
\$\begingroup\$

GolfScript, 1 byte

1

At the end of a GolfScript program, the interpreter outputs everything that's on the stack, and a trailing newline. So the output of the above program is 1 with a trailing newline, two bytes.

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

Haskell, 19 bytes

main=print[92..102]

Outputs: [92,93,94,95,96,97,98,99,100,101,102] and a NL.

\$\endgroup\$
2
  • \$\begingroup\$ The output is 37 long... unless you count newline. In that case I suggest main=print[3..15]. Actually, per OP, "No adding unnecessary whitespace to the source code to change its length". \$\endgroup\$ Commented Oct 4, 2015 at 14:15
  • 1
    \$\begingroup\$ @LeifWillerts: the newline is there, so it counts. Removed the whitespace. \$\endgroup\$
    – nimi
    Commented Oct 4, 2015 at 14:31
2
\$\begingroup\$

T-SQL, 16 bytes

SELECT SPACE(32)

This returns a string of 32 spaces. Can't think of anything fancier at the minute.

\$\endgroup\$
1
  • \$\begingroup\$ Save one byte with PRINT SPACE(30) \$\endgroup\$
    – BradC
    Commented May 21, 2018 at 21:35
2
\$\begingroup\$

Retina, 25 24 - 25 = -1 bytes

^$
11
1
11111111!
&!`111

Use the -s flag to run the code from a single file. Input is in unary.

For reference, without the bonus, there's a 1-byte solution:

!

Really any single character would do, so long as it's a valid regex (so an unmatched parenthesis wouldn't). This will just print the number of matches (zero in this case) followed by a newline, which makes two bytes.

Explanation

A translation of the sed answer, requires 25 bytes (computed as 5x5) instead of 40 (see revision history). However, we can do better in Retina:

First, we default the input to (unary) 2 if it's empty with:

^$
11

Now we turn each 1 into eight 1s and an exclamation mark with:

1
11111111!

What we ultimately want is to print 24 bytes for each of those chunks. The idea is to get a "free" multiplication by using Match mode's options to a) print all matches and b) consider overlapping matches. We also note that each match is terminated by a linefeed. So if each match is 3 bytes, we'll get 4 bytes per line, and need 6 lines. How many possible 3-byte matches are there in a string of 8 ones?

11111111
111
 111
  111
   111
    111
     111

Six. How convenient :). So we the final stage is simply:

&!`111

Which means that for each unit of source code length requested, we print:

111
111
111
111
111
111

with a trailing newline, which is exactly 24 bytes.

Note that this is why we needed to append the exclamation mark to each line: it ensures that we don't get additional matches which are shared between chunks.

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

SWI-Prolog, 3 bytes

nl.

This outputs:

\n
true.

(the \n is printed as an actual linefeed, not the two characters).

\$\endgroup\$
2
  • \$\begingroup\$ Is the dot after true in the Prolog specs or is it interpreter specific? The swish online interpreter doesn't print it allowing the 2-byte program p. \$\endgroup\$
    – Emigna
    Commented Dec 16, 2015 at 18:01
  • \$\begingroup\$ @Emigna Hmm, then I guess it depends on what interpreter you have. I used SWI-Prolog, so I will update the title of this answer to be SWI-PROLOG specific and you may answer with a shorter solution for other interpreters. \$\endgroup\$
    – Fatalize
    Commented Dec 17, 2015 at 8:37
2
\$\begingroup\$

Brian & Chuck, 9 bytes

{?
#.{-?

Try it online! There's a single unprintable \x12 (code point 18) before the first { which gets decremented each iteration, giving an output of 18 ?s.

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

PHP, 34 bytes - 25 = 9

$i=34*$argv[1];while($i--)echo"x";

34 bytes generating 34 * $argv[1] characters

2x version, 25 bytes

$i=50;while($i--)echo"x";

25 bytes generating 50 characters

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

Batch, 17 bytes

@type %0 %0 2>nul

This one does not require a special locale (as the date solution does)

And it does not require %PATH% to be set. (No environment at all, for that matter)

2>nul is required to supress type to output the filenames on stderr (which it does when more than 1 file is printed)


Currently I couldn't conjure up a f(x)=x*N solution that is shorter than 17+25 bytes

This is the best I could come up with:

@if !%1 == ! (call %0 2) else (for /l %%x in (1,1,%1) do @type %0 2>nul)

Which has a score of (72-25) 45 bytes

\$\endgroup\$
3
  • \$\begingroup\$ You could golf that n solution by 12 bytes to a total of 60 (-25 = 35): @if !%1==! (call %0 2)else for /l %%x in (1,1,%1)do @type %0, done by removing some unecessary spaces and parentheses. Also, the 2>nul thing is only needed for multiple files, and since you're only doing type %0, it isn't required there. Finally, I'm not 100% sure on how bytes for flags are counted, but you could possibly use the /v flag (if it only counts as 1 byte) on cmd to remove those 2 pesky @. Cheers! \$\endgroup\$ Commented Sep 13, 2017 at 2:51
  • \$\begingroup\$ I realize it's still not perfect as 35 is still more than 17, but it's something :T \$\endgroup\$ Commented Sep 13, 2017 at 2:53
  • \$\begingroup\$ I wonder isn't this 'file io'? \$\endgroup\$
    – stevefestl
    Commented Sep 22, 2017 at 23:51
2
\$\begingroup\$

JavaScript, 2 bytes

f=(n=2)=>("f="+f).repeat(n)
  • +27 bytes source code
  • -25 bytes bonus
\$\endgroup\$
2
\$\begingroup\$

Brain-Flak, 128 bytes

Try it online!(You probably wont be able to finish it before the universe ends)

(((((((((((((()()()){}){}){}){}()){}){({}[()])}{}){({}[()])}{}){({}[()])}{}){({}[()])}{}){({}[()])}{}){({}[()])}{}){({}[()])}{})

How it works

It starts simply by pushing ninety-eight to the stack:

((((((()()()){}){}){}){}()){})

It then implements my Triangulation algorithm:

(*push n*{({}[()])}{})

seven times.

This results in

1447283887869053957595051227533274962011504066726596247765427442496031260270109555118382532157345193860142997726793245725173763195275989332247013823819162395404940274645651775376645319691872778284304403703355308050412897911161964450479105938633399903762942

The program then terminates and spits the contents of the stack to the terminal. Since insert atrociously huge number here is 256 characters long in base 10, the output is exactly twice the source code's length.

As one might tell I used a computer aided search to find this solution. This is almost certainly the shortest solution using the triangulation method I outlined.

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

PHP no bonus, 19 17 16 14 9 bytes

<?=99**9;

prints 913517247483640899

longer versions:

  • echo date(Mc); 14 bytes print the first three letters of the month name followed by
    an ISO-8601 formatted date (like 2017-01-26T21:57:38-08:00 - length=25)
  • var_dump([01]); 15 bytes print array(1) {\n [0]=>\n int(1)\n}\n
  • echo decbin(~0); 16 bytes print 32 ones (on a 32 bit machine)
  • echo md5(12345); 16 bytes print 827ccb0eea8a706c4c34a16891f84e7b
  • <?=str_pad(_,34); 17 bytes print one underscore and 33 spaces.
  • <?=decbin(2**33); 17 bytes print a 1 and 33 zeroes
  • printf("%34d",0); 17 bytes print 33 spaces and a 0
  • echo date(uuuuuu); 18 bytes print 6*6 zeroes
  • <?=date(uuuuYY); 16 bytes print 6 zeroes four times and the current year twice

close but no cigar:

  • <?=pi(); 8 bytes print 3.1415926535898 (15 characters; 64 bit machine)
  • <?=log(2); 10 bytes print 0.69314718055995 (16 chars; 64 bit)
  • <?=date(wc); 12 bytes print 26 chars
  • var_dump(_.pi()); 17 bytes print 30 chars (64 bit)
\$\endgroup\$
2
\$\begingroup\$

Excel,10 Bytes

=pi()&1234

prints 3.141592653589791234

\$\endgroup\$
1
  • \$\begingroup\$ A different approach gives 5 bytes: `=10^9' \$\endgroup\$
    – Wernisch
    Commented Sep 24, 2019 at 13:29
2
\$\begingroup\$

TI-Basic, 2 bytes

::

(Ab)uses the fact that programs output Done when nothing is evaluated on the last line.

\$\endgroup\$
2
  • \$\begingroup\$ This doesn't work. You need two colons after the last expression to display Done. \$\endgroup\$
    – lirtosiast
    Commented Jul 11, 2017 at 1:33
  • \$\begingroup\$ @lirtosiast Fixed \$\endgroup\$
    – Timtech
    Commented Jul 11, 2017 at 2:19
2
\$\begingroup\$

VBA, 2 1 -3 20 - 25 = -5 Bytes

?Spc([Max(A1,2)*20])

Which outputs [A1] (analagous to n) else 1 times 22 (length of the code) spaces

Previous Version 1 Byte

?

Output

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

dc, 4 bytes

1dff

prints

1
1
1
1

TIO

\$\endgroup\$
1
  • \$\begingroup\$ nope. 1 \n 1 \n 1 \n 1 \n @Stephen \$\endgroup\$
    – cab404
    Commented Sep 17, 2017 at 4:35
2
\$\begingroup\$

Pyth, 3 - 25 = -22 bytes

This was an accident, but it magically works, however, to make this work, I had to waste a byte.

yDQ

This returns a range between 0-input. The brackets, commas, and spaces add up to Q times 3.

I honestly can't quite tell you how or why this works.

Test Suite

Pyth, 1 byte (no bonus)

d

Returns a space and a newline, which is 2 characters.

Test Suite

\$\endgroup\$
6
  • \$\begingroup\$ doesn't appear to work for no input \$\endgroup\$
    – ASCII-only
    Commented Apr 20, 2018 at 4:22
  • \$\begingroup\$ @ASCII-only I believed that input was always going to be in regex /[0-9]+/, unless otherwise specified. \$\endgroup\$
    – Stan Strum
    Commented Apr 20, 2018 at 4:24
  • \$\begingroup\$ "If no input is provided, n must default to 2." idk though, the question may be unclear :/ \$\endgroup\$
    – ASCII-only
    Commented Apr 20, 2018 at 4:30
  • \$\begingroup\$ Note: @Blue just pointed out that input will always be positive so you can just do logical or \$\endgroup\$
    – ASCII-only
    Commented Apr 20, 2018 at 7:34
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$
    – ASCII-only
    Commented Apr 20, 2018 at 8:00
2
\$\begingroup\$

LibreLogo, 21 bytes

Code:

input_half_the_length

Result:

Unknown name: ‘input_half_the_length”.

enter image description here

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

Minecraft Functions (18w15a, 1.13 snapshots), 26 bytes

Uses one function named ab

ab

function ab
tp @e ~0 ~0 ~0

It just recurses until it hits the default limit of 65536, at which point it outputs Executed 65536 commands from function 'minecraft:ab'

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

Perl 5, 17 - 25 = -8

Tip of the hat to primo for getting me to reread perlvar

die$/x(<>*17||34)

Try it online!

\$\endgroup\$
1 2
3
4 5
11

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.