Sign up ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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 (trailing newlines optional, and included in byte count) must be exactly twice the length of the program, also measured in bytes, unless you fulfill the bonus.

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.

Reward: -25 bytes

Restrictions

  • No file/disk/network io allowed.

  • 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>

share|improve this question
    
For the bonus, does the output have to be exactly n * program length bytes, or is that a minimum? –  xnor yesterday
    
It has to be exact –  Daniel M. yesterday
    
Looks like the code snippet has to be modified to handle negative scores. –  El'endia Starman yesterday
6  
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. –  xnor yesterday
1  
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. –  xnor yesterday

47 Answers 47

CJam, -17 bytes

r2e|i8,*

Try it online.

share|improve this answer
1  
Crap. Well done. –  The_Basset_Hound yesterday

HQ9+, 2 bytes

QQ

outputs

QQQQ

I think it is not forbidden here.

share|improve this answer
    
It may not be forbidden, but it's not very inter... wait, how did it get eight upvotes? –  Jan Dvorak 8 hours ago
1  
@JanDvorak Much as I dislike HQ9+, this to me is a creative use. This usage most likely wasn't envisioned when HQ9+ was created, which is more than can be said of most other Hq9+ programs. +1 from me. –  Digital Trauma 8 hours ago

Python 2.6, 10

print`-.1`

Prints -0.10000000000000001, which is 20 chars.

Note that the string repr shows more precision. print-.1 just gives -.1, and print.1/3 gives 0.0333333333333 for only 13 digits of accuracy.

share|improve this answer
    
This no longer works in Python 2.7 or 3.1. –  Anders Kaseorg 23 hours ago

Mathematica, 1 byte

#

Prints #1.

share|improve this answer

Python 2, 11

print`id`*1

Print the string representation of the built-in id, which is 22 chars:

<built-in function id>

The *1 is to get the code to 11 chars. You could also do print id;00.

More boring alternative 11's are:

print'1'*22
print 9**21
share|improve this answer
    
I've been playing around with complex numbers, but unfortunately print.8/-9j is one char short –  Sp3000 9 hours ago

Matlab, 7 bytes

'a':'e'

Output:

enter image description here

The output contains newline ans = newline newline abcde newline, so 14 bytes.

share|improve this answer
    
Let's hope nobody has format compact as default :) –  sanchises 16 hours ago
1  
@sanchises :-) Yes, that depends on how Matlab's preferences have been set. But format loose is the default preference –  Luis Mendo 15 hours ago

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)
share|improve this answer
1  
Does this really write to standard output? –  immibis yesterday
    
It's a function return, using fat arrow notation. –  SpeedyNinja 22 hours ago
    
The spec requires a full program... –  Jan Dvorak 8 hours ago

Pyth, -15 -16

-1 by Dennis

**N9?zvz2

Prints [input]*9 quote characters, or 2*9 if the input is empty.

Pyth, 1 byte

T

Prints 10. It's a built in variable that initializes to 10.

share|improve this answer
    
It would be useful if .x worked with errors. Is there a justification for why it doesn't? –  Thomas Kwa yesterday
    
My best guess is that Q causes Pyth to eval the input before the actual code is executed. **N9.xvz2 works as expected, but it's not shorter than **N9?zvz2. –  Dennis 7 hours ago
    
@Dennis Interesting, and thanks. I should have known empty strings were falsy. –  Thomas Kwa 2 hours ago

Recall, 17 bytes

................!

17 NOOPs. Then the debugger ! is invoked and dumps the memory to the console. The memory is empty, but the header is 34 bytes long:

-- STATE DUMP --
----------------

Try it here.

share|improve this answer

CJam, -9

q_,{i}{;2}?G*'X*

Explanation

  • q_,

Reads the entire input and pushes it, then pushes the length.

  • {i}{;2}?

If the length of the input is over zero, convert it to an integer. Otherwise, pop the input and push 2.

  • G*

Pushes 16 (the program length), then multiplies it by 2 if there is no input, or by the input.

  • 'X*

Pushes X and multiplies it by the top of the stack.

share|improve this answer
    
@ThomasKwa Whoops. Updating in a sec. –  The_Basset_Hound yesterday
    
The comma is not necessary, however you can avoid the "if" altogether if you do a logical "or": q2e|i will give you the number you want –  aditsu 8 hours ago
    
Also you could use S instead of 'X, the only issue is that the output will not be visible until you select it (well, if you run it offline and redirect to a file, it's not a problem) –  aditsu 8 hours ago
    
Uh.. nevermind, I just saw Dennis's answer now :p –  aditsu 8 hours ago
    
Even with the conditional, you can shorten this. You don't need to calculate the length, since an empty string is already falsey. You also don't need to pop the empty string off the stack, since it does not contribute to the output. Which also allows yo to avoid the braces for the else branch. I came up with this before I looked at any of the answers: r_{i}2?SB**. –  Reto Koradi 6 hours ago

Macaroni 0.0.2, 23 chars

print tobase pow 32 9 2

Prints 329 in binary, which happens to conveniently turn out to be 46 characters long (it's 1000000000000000000000000000000000000000000000), without a trailing newline.

share|improve this answer

C, 19

printf("%0.36f",0);

prints 0.000000000000000000000000000000000000 which is 38 bytes long.

share|improve this answer

gs2, -18 bytes

CP437: W↕0!↨.2

Hex dump: 57 12 30 21 17 2e 32

W reads numbers from STDIN into a list. ↕0 appends a 2 to the list, and ! extracts the first element. Then ↨. (the list [0,1,2,3,4,5,6]) is repeated (2) this many times.

This is very similar to Dennis's CJam answer -- gs2 just combines r and i into one byte.

Note: I had to fix a bug in the gs2 implementation for this to work: previously, each program had a hidden newline appended to its output, which was entirely unintentional. It only surfaced after I tried to solve this program (the language was designed for anarchy golf, which ignores trailing newlines in all problems), and I only pushed a fix to master just now, so feel free to take this answer with a grain of salt.

share|improve this answer

Perl, 27 bytes - 25 = 2

$n=pop||2;print"n"x($n*27);

Thanks to the excellent commentators for their help on this one :)

It produces 27 "n"'s multiplied by $n, which defaults to 2 if @ARGV is undefined.

share|improve this answer
    
I think this fails for empty input. Per the rules, if no input is provided, the multiple should default to 2. –  Alex A. yesterday
    
@AlexA. Damn, I missed that. New to code golf. Edit incoming, but my answer has a lot of characters now because I have no idea how to do this more creatively in perl :( –  Codefun64 23 hours ago
1  
Hey there, a few more tips that might help you here, by default in a program script, pop and shift work on @ARGV (in a sub they work on @_) so you could do something like $n=pop||2 to get the first code block down, and you have ;} at the end, which can almost always be just } to save another. Something that might help you save more chars is the string repetition operator x, consider print 1x20 or something along those lines... :) Hope that helps! –  Dom Hastings 16 hours ago
    
@DomHastings Man, thanks! I got it down to 2 bytes total because of your help :) Thank you very much for your suggestions. –  Codefun64 8 hours ago

dc, 10 - 25 = -15

2?A*Ar^1-n

Takes a blank line for "no input".

Calculates 10 ^ (10 * n) - 1, where n is the input, or 2 if input is empty. Prints a string of 9s of the required length.

  • 2 push 2 to the stack in case input is empty
  • ? push input to the stack
  • A push 10 to the stack (dc has shortcuts A-F for 10 - 15)
  • * pop twice and multiply (multiply input by 10)
  • A push 10 to the stack
  • r reverse top two stack elements
  • ^ exponentiate 10 ^ (10 * input)
  • 1- subtract 1 from the top of stack
  • n print with no newline.
share|improve this answer
    
dang, that is so clever. And lucky with the code length there :) But maybe you should add a small explanation for the uninitiated. And I think newline counts, so you should probably use n instead of p. –  daniero 13 hours ago
    
@daniero Explanation added. –  Digital Trauma 8 hours ago

TI-Basic, 3 bytes

1ᴇ5

Prints 100000.

share|improve this answer
    
Actually, I think the newline would count, so you can just do 0 for 1 byte. –  Thomas Kwa 1 hour ago

Julia, 42 bytes - 25 = 17

print("@"^42((r=readline())>""?int(r):2))

This reads a line from STDIN using readline(). If it's empty, i.e. no input has been provided, then n is defined to be the input converted to an integer. Otherwise n is 2. We then print 42​n @s to STDOUT.

share|improve this answer

JavaScript ES6, 33 - 25 = 8

(a=2)=>Array(a*33).fill(0).join``
share|improve this answer

dc, 19 - 25 = -6

2?19*[1n1-d0<l]dslx

Takes a number (2 is pushed to the stack as backup) and multiplies it by 19. Prints a 1 (no newline) and decrements the number. Loops while the number is greater than 0.

share|improve this answer

><>, 21 + 2 (-v flag) - 25 = -2 bytes

l0=?2f6+*v
-1o;?=0::<

test it here!

Thanks Cole for helping fix my errors

First checks the stack length, if it's 0 put 2 on the stack. Multiplies it by 21 (code length), then outputs the unicode representation of that number and decrements it by 1, loops until 0. (you'll have to inspect the output to see the characters, since the browser won't display them)

share|improve this answer
    
Yeah, sorry to ruin your answer (but it is shorter than mine, if that makes you feel better). Edit: whoops hit enter too soon, your entry as it stands right now is not valid per the rule "You can assume that n will always be a positive integer. If no input is provided, n must default to 2." I also think it might print one too many characters (It goes from 14 to 0 which is 15 characters by my count, not 14). If I'm correct, l0=?2f6+*v as the first line and 1-::0=?;o> as the second should make it right. –  Cole yesterday
    
Hm, not sure what you mean. It shouldn't print 0 in any case. Putting a 2 as the initial stack outputs (EDIT: stuff i can't paste) which, at least using this char counter, is 28 chars. –  torcado yesterday
    
I understand what you're saying now. I interpreted "Your program can optionally take a number, n, as input" meaning that if the input would be 2. I'll edit the answer, thanks! (also i can save some by using a string instead of numbers) –  torcado yesterday
    
Happy to help, especially if it's ><>. –  Cole yesterday

Perl, 18 - 25 = -7

print$=x(<>*9||18)

The special variable $=, a.k.a. $FORMAT_LINES_PER_PAGE, begins its life as 60, and therefore only needs to be duplicated half as many times as byte output needed.

share|improve this answer
1  
I had a very similar answer of: print$=x9x(pop||2) nice! –  Dom Hastings 15 hours ago

Ruby, 24 - 25 = -1

$><<?x*24*(gets||2).to_i
share|improve this answer

q, 10 3 bytes

3#0

Outputs 6 bytes: "0 0 0\n".

share|improve this answer
1  
If I understand the challenge correctly, an 8-byte long program should produce 16 bytes of output... –  undergroundmonorail 19 hours ago
    
Right. I should have read past the title. I've updated my answer. –  Alexander Belopolsky 18 hours ago

JavaScript (ES6), 50-25=25

a=prompt();alert("0".repeat(48*(isNaN(a|0)?2:+a)))
share|improve this answer

Ruby, 6 bytes

p ?a*9

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

share|improve this answer

Mouse, 21 bytes

1I:(I.43<^9!1I.+I:)$

Ungolfed:

1 I:                  ~ Begin a loop index at 1
( I. 43 < ^           ~ While I < 43...
  9 !                 ~ Print 9 to STDOUT
  1 I. + I:           ~ Increment I
)$
share|improve this answer

><>, 29 + 2 (-v flag) - 25 = 6

l0=?21-:0(?;fe+1-ao:0=f*e+0.~

Takes input on the stack, which I believe is populated through -v on the official interpreter. Prints newlines only.

share|improve this answer

Microscript, 1

0

Prints the digit 0, followed by a newline.

share|improve this answer
    
codegolf.stackexchange.com/revisions/59457/3 What kind of edit is that?? Hahaha –  Albert Renshaw 22 hours ago

C++, 80 bytes

#include<iostream>
int main(){int i=0;while(i<20){std::cout<<&i;i++;}return 0;}

note the newline character is two characters. (if you don't want it to be, change i<20 to i<=19 to get back to the same byte count.)

Sample output (will change every time)

0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C0088FA0C

same 8 character memory address 20 times.

share|improve this answer

q (bonus), 27 bytes

1#[;"x"]27*2^first"J"$.z.x;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.