103
\$\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
    Oct 2, 2015 at 23:19
  • 2
    \$\begingroup\$ It has to be exact \$\endgroup\$
    – Daniel M.
    Oct 2, 2015 at 23:20
  • 3
    \$\begingroup\$ Looks like the code snippet has to be modified to handle negative scores. \$\endgroup\$ 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
    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
    Oct 2, 2015 at 23:59

300 Answers 300

1
3 4
5
6 7
10
1
\$\begingroup\$

C, 25

main(){printf("%*p",50);}

This makes use of UB, but it should work. At least it works with gcc 5.2.0

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

MSM, 12 bytes

'...;.;.;...

Outputs ........................

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

R, 3 bytes

10;

will print

[1] 10
\$\endgroup\$
1
\$\begingroup\$

QBasic, 2 bytes

?1

Nonnegative numbers in QBasic are output with both a leading and a trailing space. The PRINT command (for which ? is a shortcut) outputs a newline by default. Thus, I count 4 bytes of output: space 1 space newline.

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

Julia, 9

warn(⊆)

prints

"WARNING: issubset\n"
\$\endgroup\$
1
  • \$\begingroup\$ Might want to format your header differently so your score doesn't look like -8 instead of 9 \$\endgroup\$ Oct 5, 2015 at 4:05
1
\$\begingroup\$

Insomnia, 1

7

Output:

00

8, A, B are 3 other programs that satisfy the requirement. Their output contains NUL characters, though.

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

MUMPS, 4 bytes

w ?8

Well, I'm not sure if this really counts. What this program does is advance the output cursor 8 characters to the right. On every terminal I've used, this is indistinguishable from outputting 8 spaces, but is it really the same thing? I dunno.

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

C, 82 bytes (with bonus)

main(int a,char**b){b&&b[1]&&(a=atoi(b[1])-1);a&&main(a-1,0),printf("%.80f",.0);}

Usage:

$ wc main.c
       1       2      82
$ ./a.out | wc
       0       1     164
$ ./a.out 4 | wc
       0       1     138
$ ./a.out 133475 | wc
       0       1 10944950
\$\endgroup\$
2
  • \$\begingroup\$ I think you can shave some off by modifying b as you go: b&&*++b&&(a=atoi(*b)-1) \$\endgroup\$ Oct 6, 2015 at 10:31
  • \$\begingroup\$ Yes comrade! It does work. \$\endgroup\$
    – wefwefa3
    Oct 6, 2015 at 17:43
1
\$\begingroup\$

DOS, 7 bytes

date /t

outputs di 06-10-2015 (and a newline) on my system, but I'll admit it's locale dependent. So my second best is:

echo %PATH:~0,33%

which outputs C:\WINDOWS\system32;C:\WINDOWS;C: (and a newline).

\$\endgroup\$
5
  • \$\begingroup\$ With my system settings it prints 2015.10.07\n \$\endgroup\$ Oct 7, 2015 at 10:12
  • \$\begingroup\$ Got 6.2 bytes using "type %0". \$\endgroup\$
    – Star OS
    Dec 16, 2015 at 12:03
  • \$\begingroup\$ @StarOS That gives me The system cannot find the specified file (roughly translated) \$\endgroup\$
    – Berend
    Dec 17, 2015 at 7:45
  • \$\begingroup\$ Berend, it only works in a Batch file. +1 beacuse all of the other things don't use a shell, it uses a file \$\endgroup\$
    – Star OS
    Dec 17, 2015 at 11:25
  • \$\begingroup\$ @Berend Note, you are using Windows Batch, not DOS Batch. The string manipulation function is added in Windows, not DOS. \$\endgroup\$
    – stevefestl
    Sep 22, 2017 at 23:52
1
\$\begingroup\$

Bash + GNU coreutils, -1 byte

printf %24s `seq ${1-2}`

Score is 24-25

A non-bonus version for +5:

seq 5

which produces 1 nl 2 nl 3 nl 4 nl 5 nl.

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

Bash + GNU coreutils, 26 24 - 25 = 1 -1 byte

The x file:

yes|head -c$[24*${1:-2}]

(no trailing newline)

Running:

$ bash x 1 | wc -c
24
$ bash x | wc -c
48
$ bash x 3 | wc -c
72

Old version with 26 bytes:

The x file:

yes|head -c$((26*${1:-2}))

(no trailing newline)

\$\endgroup\$
1
  • \$\begingroup\$ @Unihedron I don't see how this violates that rule. \$\endgroup\$
    – Doorknob
    Oct 4, 2015 at 14:30
1
\$\begingroup\$

POSIX bc, 4 bytes

10^7

This creates the output 10000000.

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

Javascript, 3 bytes!

1e5

Returns 100000. Y'all JS golfers are overthinking it!

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

CJam, 1 byte

A

Prints 10. Very straightforward.

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

Pyth, 4 bytes

*8"1

will print out

11111111
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Why did someone EDIT my answer??? \$\endgroup\$ Oct 7, 2015 at 15:25
  • \$\begingroup\$ I like how you rolled back their changes and then added them back in. \$\endgroup\$
    – Luminous
    Oct 9, 2015 at 12:53
  • 1
    \$\begingroup\$ @Luminous I did it because I want editors to learn not to edit my answers. They can suggest in comments instead! \$\endgroup\$ Oct 10, 2015 at 12:41
  • 1
    \$\begingroup\$ Well the thing in this case was that editor didn't just edit your answer. They changed it to be completely different. They should've made it another answer. Other wise you shouldn't feel like your answer is being violated if someone edits it. We can edit other people's answers for plenty of reasons. \$\endgroup\$
    – Luminous
    Oct 11, 2015 at 14:43
  • \$\begingroup\$ @Luminous Yeah, and I added them back in because I got that as a suggestion. But if they do not have rep to comment then they should add just a small not on the bottom of the answer but not EDIT it. \$\endgroup\$ Oct 12, 2015 at 11:33
1
\$\begingroup\$

APL, -8 bytes

'*'⍴⍨17×{0::2⋄⎕}⍬

Explanation:

  • {0::2⋄⎕}⍬: Try to read a number from the keyboard. If the user entered a valid number, return it; if not, return 2.
  • 17×: multiply it by 17 (the length of the code)
  • '*'⍴⍨: output that many asterisks.
\$\endgroup\$
1
\$\begingroup\$

[this is a sticky note] This answer has multiple versions.

Due to the fact that I honestly am not quite sure what "input" means here. The first is the one I would use to score myself. Go down to the second to find an explanation. For scoring 1 point, do I tie with the Matmematica one? Or for scoring -1 (-5?) points, do I win? Hmm.

Anywho, this was quite a fun challenge. Maybe I can make my answer a bit better but anyways.

Python 2.7, 26 - 25 = 1

If input means input from stdin:

print("|"*26*input())[:-1]

Python 2.7, 24 - 25 = -1

If input means a variable:

n=2;print("|"*24*n)[:-1]

Wuut?

n=2                   # Set a variable n to 2
;                     # Separate statements (like a line break)
print                 # Print..
(                     # This is in 2.7, not 3, where print is a
                       # statement, not function
  "|"                 # Any character works here
  *24                 # Multiply said character by 24
  *n                  # Multiply resulting string by n
)
[:-1]                 # Remove one character due to the line break
                       # automatically created by print.

Python 2.7, 20 - 25 = -5.

If input means a variable, and you don't count n=2; as part of the program:

Simply change 24 to 20:

print("|"*20*n)[:-1]

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It's standard for input to mean STDIN or a function argument. \$\endgroup\$
    – lirtosiast
    Oct 27, 2015 at 14:46
1
\$\begingroup\$

Burlesque - 7 Bytes

blsq ) '*14.*Q
**************

It just prints 14 asteriks, while the length of the program is 7 bytes. As for the bonus:

ri12.*'*j.*Q

is 12 bytes long and prints 12*n (number provided on STDIN) asteriks.

\$\endgroup\$
2
  • \$\begingroup\$ Does your bonus version have a default value? \$\endgroup\$
    – Titus
    Jan 27, 2017 at 6:32
  • \$\begingroup\$ No. ri just converts a string to integer. \$\endgroup\$
    – mroman
    Jan 29, 2017 at 15:50
1
\$\begingroup\$

Minkolang 0.9, 11-25 = -14 bytes

This language was created after this challenge, but not for it.

nd1+?2["d].

Try it here.

Explanation

This takes an integer from input, pushing a 2 on the stack if it's -1 (i.e., the input is empty). Then I use the clever quine trick: the " pushes the whole program (except the ") onto the stack. To make up for the ", I duplicate the top of stack with d. This is printed out n or 2 times and then the program stops. In the case where there is no input, there is indeed a -1 on the stack. However, Minkolang outputs nothing for negative numbers, so it does not add anything.

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

Perl 6, 3 points

say 'a'x 21 # 11
# say adds a newline
print 'a'x 28*(@*ARGS[0]//2)
# 28 - 25 = 3
\$\endgroup\$
1
\$\begingroup\$

Sass, 30 bytes

codegolf is absolutely awesome

if you try to compile this with sass the result is the following error

Invalid CSS after "...olutely awesome": expected "{", was "" 
/* 60 bytes */
\$\endgroup\$
3
  • \$\begingroup\$ I get invalid top-level expression on line 1 at column 1, but I also don't think this is a language as it's not turing-complete and it only compiles to CSS. \$\endgroup\$
    – cat
    Dec 16, 2015 at 13:04
  • \$\begingroup\$ @cat I'm pretty sure I could implement /// in Sass using just string manipulation and a while loop, and /// is turing complete. \$\endgroup\$
    – Sparr
    Dec 17, 2015 at 10:05
  • \$\begingroup\$ @Sparr fair enough \$\endgroup\$
    – cat
    Dec 17, 2015 at 11:57
1
\$\begingroup\$

PlatyPar, 1 byte

#

# starts a number, but since no number is found after it, it is substituted with 59. Here's a "real" answer:

77^

Prints the result of 7^7, or 823543, which is of length 6.

Try it online!

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

Z80 machine code, 16 bytes

3E 1F 0E 02 1E 2A F5 CD 05 00 F1 3D F2 02 01 C7

This was made on an Osborne Executive running CP/M version 3.0 using SID. Here is a disassembly (with comments following # characters for readability)

MVI A,1F     # Set register A equal to 0x1F (iterations-1)
MVI C,2      # Write is BDOS call 2 (CP/M's system calls)
MVI E,2a     # I'm writing '*' as output. Its ASCII value is 0x2a
PUSH PSW     # Push operates on register pairs. This pair contains A.
CALL 5       # Do the BDOS call (which destroys some registers)
POP PSW      # But we can get A back from the stack.
DCR A        # Decrement the loop counter
JP 102       # If last result is non-negative, go to address 0x102
RST 0        # Otherwise, exit

All programs are loaded at a fixed address of 0x100, so the jump to 0x102 is well-defined. After running this, CP/M wants to print a newline character. If we count that as output from the program, change the second byte from 1F to 1E.

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

Fuzzy Octo Guacamole, 8 bytes

42*![o;]

Also could use:

8  ![o;]

Or

81*![o;]

But that is cooler.

Prints 8 "8"s and 8 newlines, including a trailing one.

Explanation:

4: Push 4 to the stack.

2: Push 2 to the stack.

*: Pop and multiply the top 2 items on the stack and push the result (8)

!: Set the loop counter to the top. Is now 8.

[: Start a loop that lasts 'loopcounter' (8) iterations.

o: Peek at the top of the stack and push it to the temp variable.

;: Print the temp var.

]: End the loop.

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

Pylongolf, 2 bytes

.;

. - Reset the stack
; - Debugally print both the stack and the variables.

The interpreter I use prints debugally by converting an 2 arrays into a string which has that string begin with [ and end with ] which prints:

[][]
\$\endgroup\$
1
\$\begingroup\$

Lua, 35 - 25 = 10 bytes

Takes input from the command line, which is in the varargs.

print(("n"):rep(35*(...or 2)-1))
\$\endgroup\$
1
  • \$\begingroup\$ You can shave off 3 bytes with print(("n"):rep(35*(...or 2)-1)) \$\endgroup\$
    – Katenkyo
    Mar 30, 2016 at 9:55
1
\$\begingroup\$

Labyrinth, 10 bytes - 25 = -15

Another Labyrinth collaboration with Sp3000.

?02
`\
~"@

This prints 10n linefeeds to STDOUT.

Try it online!

Explanation

The most interesting part is probably how the default value of 2 is handled.

If an input number is given, the following code is executed:

?   Read integer from STDIN.
0   Multiply by 10. This is because digits in Labyrinth work by multiplying the top of
    the stack by 10 and then adding themselves, such that multi-digit numbers can be
    written into the code more easily.
    The top of the stack is now positive (and contains the number of characters to be
    printed) so the instruction pointer (IP) turns right/south towards the \.

If no input number is given, ? pushes a 0 instead, and this happens:

?   Push 0.
0   Multiply by 10, which is still 0. Since the top of the stack is now 0, the IP
    keeps moving forward/east instead.
2   Multiply by 10, add 2, which sets the top of the stack to 2. The IP hits a
    dead end so it turns around.
0   Multiply by 10 to give 20. Now the top of the stack is positive and IP
    turns left/south towards the \. Again, the top of the stack is the number
    of characters to be printed.

Now all we need to do is print one character each while decrementing the top of the stack to zero. The cheapest character to be printed in Labyrinth is a linefeed, because \ prints one without affecting the stack at all. As an additional trick, we decrement via multiply by -1, bitwise NOT, to ensure that the top of the stack is negative in the top left corner of the loop (otherwise the IP would move towards the ? again).

The loop is then simply:

\   Print linefeed.
`   Multiply by -1.
~   Bitwise NOT.
"   No-op. This cell acts a junction. While the top of the stack is positive
    the IP will turn left/north, otherwise it will move forward/east.

When the IP leaves the loop it hits the @ which terminates the program.


For completeness, here are also two 7-byte versions without the bonus:

7:(
@`!

prints

-7-6-5-4-3-2-1

Try it online!

And

>11!:
@

prints

11001011111011

Try it online!

The former is a very simply (but compact) loop which prints -n while decrementing n from 7 down to 0.

The latter is a simple modification of Sp3000's solution to this challenge (which makes the execution a little bit crazier though).

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

Hexagony, 6 bytes

o!!!!@

Prints

111111111111

Try it online!

Explanation

Probably one of the simplest Hexagony programs I've written. The unfolded code is

 o !
! ! !
 @ .

and is simply executed in reading order. o sets the current memory edge to 111 (any letter from d to z would do). Then the ! print that four times and @ terminates the program.

I might try for the bonus later, but I have some doubts that it will fit in side-length 4 (and side-length 5 might end up costing more than the bonus gives).

\$\endgroup\$
2
  • \$\begingroup\$ How about this? TIO \$\endgroup\$
    – Adyrem
    May 28, 2018 at 12:21
  • \$\begingroup\$ @Adyrem That only works if measured in characters (whereas the challenge requires counting bytes). And by the time you get to 5-digit code points (in order to fix that), UTF-8 requires 3 bytes for the character. \$\endgroup\$ May 28, 2018 at 13:09
1
\$\begingroup\$

APL, 3 bytes

1e5

Print 100000...

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

dc, 7 bytes

2oFddnf

2o         Set the output radix to 2: write to stdout using the binary digits [01]
  F        Push 0xF on the stack, equivalent to 1111b
   dd      Duplicate the top-of-stack, then duplicate the top-of-stack: 1111b, 1111b, 1111b
     n     Pop the topmost item from the stack (1111b) and write it (using binary, per
               the above) to stdout. Do not follow with a newline.
      f    Dump the contents of the stack (1111b, 1111b), following each item with a
               newline.

Visible characters comprise 12 bytes; add two (2) newlines for a total of 14.

Edit: Since I'm the only dc answer with a natural number for a score, why not post the following?

dc, 8 bytes

cccccccP

Clears the stack seven times, then attempts to pop the top (non-existent) value and print it as text (i.e., a number with output-radix UCHAR_MAX+1). Since the stack is empty, this results in a fifteen-byte error message followed by a single newline. (Works for GNU dc 1.2)

\$\endgroup\$
1
  • \$\begingroup\$ Hello, and welcome to PPCG! This is a cool answer. Can you explain what exactly o, F, et cetera do? \$\endgroup\$ Jun 21, 2016 at 23:27
1
3 4
5
6 7
10

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.