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
149
\$\begingroup\$

HQ9+, 2 bytes

QQ

outputs

QQQQ

I think it is not forbidden here.

\$\endgroup\$
6
  • 15
    \$\begingroup\$ It may not be forbidden, but it's not very inter... wait, how did it get eight upvotes? \$\endgroup\$ Commented Oct 3, 2015 at 16:37
  • 60
    \$\begingroup\$ @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. \$\endgroup\$ Commented Oct 3, 2015 at 16:50
  • 13
    \$\begingroup\$ I didn’t even know that Q prints “the program’s source code”. I thought it just printed a Q. Well done for spotting the crucial difference! \$\endgroup\$
    – Timwi
    Commented Oct 7, 2015 at 17:14
  • 7
    \$\begingroup\$ @LegionMammal978 Technically this is a constant output challenge, which means non-programming languages are allowed. \$\endgroup\$ Commented Jan 16, 2016 at 0:12
  • 5
    \$\begingroup\$ This has 120 votes. 1,200 rep for typing QQ into an interpreter. \$\endgroup\$
    – Stan Strum
    Commented Sep 23, 2017 at 0:07
110
\$\begingroup\$

Shakespeare, 768

Yeah, Shakespeare's not much of a golfing language. Outputs 1,536 spaces.

Rosencrantz and Guildenstern: A Series of Tedious Events.

Rosencrantz, a count of extraordinary determination.
Guildenstern, a spacy character.

Act I: The Long Conversation

Scene I: A Tortured Friendship

[Enter Rosencrantz and Guildenstern]

Rosencrantz:
 You are a gentle, noble, valiant, loyal, loving companion.

Guildenstern:
 You are nothing!

Scene II: Asking the Hard Questions

Rosencrantz:
 Speak your mind.

Guildenstern:
 You are as fair as the sum of yourself and a daisy. Are you as
 daring as the sum of a big, bold, fiery, feisty, rough, rowdy,
 ham-fisted, hawk-eyed, broad-shouldered, bright-eyed lad and a
 large, yellow, vicious, hairy, wild, scary, long-tailed,
 sharp-clawed, small-eared lion?

Rosencrantz:
 If not, let us return to scene II.

Edit: 256

Okay, I'll actually golf it. Note that the above does not compile in any existing Shakespeare implementation because I wrote it painstakingly by hand (but am prepared to defend its correctness.)

The below translates to C with one warning in spl-1.2.1, and outputs 512 spaces:

Ummm.Ajax,1.Puck,2.Act I:I.Scene I:A.[Enter Ajax and Puck]Ajax:You old old old old old cow.Puck:You are zero!Scene II:B.Ajax:Speak thy mind.Puck:You are the sum of you and a red cat.Are you as big as the square of me?Ajax:If not, let us return to scene II.
\$\endgroup\$
9
  • 17
    \$\begingroup\$ I'll +1 if you actually golf this. \$\endgroup\$
    – lirtosiast
    Commented Oct 4, 2015 at 6:47
  • 1
    \$\begingroup\$ What manner of whitchcraft is this? I didn't even know such thing existed... \$\endgroup\$
    – Malavos
    Commented Oct 5, 2015 at 18:23
  • 11
    \$\begingroup\$ woah, you golfed it. Holy havens. \$\endgroup\$
    – Malavos
    Commented Oct 21, 2015 at 12:49
  • 2
    \$\begingroup\$ @Malavos I was just joking about the way you misspelled witchcraft, sorry -- both what and which are probably ok \$\endgroup\$
    – cat
    Commented Oct 21, 2015 at 12:55
  • 2
    \$\begingroup\$ No, I ask for your forgiviness if I sounded offensive or defensive. I was just making a reference to Futurama! :) On a serious note, I'm still surprised by this answer. We need more of those on the site. \$\endgroup\$
    – Malavos
    Commented Oct 21, 2015 at 13:39
74
\$\begingroup\$

Recall, 17 bytes

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

16 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.

\$\endgroup\$
2
  • 14
    \$\begingroup\$ This is one of the cooler answers IMO. \$\endgroup\$
    – cat
    Commented Oct 20, 2015 at 23:35
  • \$\begingroup\$ Creative answer. These are my reasons to visit codegolf. \$\endgroup\$ Commented Apr 28, 2021 at 11:15
72
\$\begingroup\$

Mathematica REPL, 1 byte

#

Prints #1.

\$\endgroup\$
1
  • 17
    \$\begingroup\$ The question says to "write a complete program"; this is just a REPL snippet. \$\endgroup\$ Commented Dec 29, 2015 at 14:19
66
\$\begingroup\$

CJam, -17 bytes

r2e|i8,*

The source code is 8 bytes long and qualifies for the -25 bytes bonus.

Try it online in the CJam interpreter.

How it works

r         e# Read a token from STDIN.
 2        e# Push 2.
  e|      e# Logical OR; keep the token if it's not empty, 2 otherwise.
    i     e# Cast to integer.
     8,   e# Push [0 1 2 3 4 5 6 7].
       *  e# Repeat the array the corresponding number of times.
\$\endgroup\$
6
  • 4
    \$\begingroup\$ Crap. Well done. \$\endgroup\$ Commented Oct 3, 2015 at 0:22
  • 1
    \$\begingroup\$ How is it negative bytes? \$\endgroup\$
    – Nick T
    Commented Oct 5, 2015 at 4:49
  • 3
    \$\begingroup\$ @NickT 8 bytes (source code) - 25 bytes (bonus). \$\endgroup\$
    – Dennis
    Commented Oct 5, 2015 at 4:51
  • 21
    \$\begingroup\$ You can save several million copies of it if you're running out of disk space. \$\endgroup\$ Commented Mar 19, 2016 at 14:42
  • \$\begingroup\$ No, I think metadata would make it positive \$\endgroup\$ Commented Mar 19, 2016 at 22:12
53
\$\begingroup\$

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.

\$\endgroup\$
7
  • 5
    \$\begingroup\$ This no longer works in Python 2.7 or 3.1. \$\endgroup\$ Commented Oct 3, 2015 at 1:27
  • \$\begingroup\$ @AndersKaseorg So repl.it is wrong, then? \$\endgroup\$
    – mbomb007
    Commented Oct 5, 2015 at 18:17
  • 2
    \$\begingroup\$ @mbomb007 Apparently. GitHub suggests that’s hardly the only thing it’s wrong about. Ideone agrees with CPython. \$\endgroup\$ Commented Oct 6, 2015 at 2:01
  • \$\begingroup\$ Same comment as for your other answer: doesn't this print a trailing newline, giving 21 bytes of output? \$\endgroup\$ Commented Oct 27, 2015 at 16:20
  • 1
    \$\begingroup\$ You mean print`+.1`? \$\endgroup\$
    – mbomb007
    Commented Apr 13, 2017 at 14:16
48
\$\begingroup\$

Seed, 10 bytes

4 56111240

This compiles to the Befunge program (found by brute force)

9k.@

which produces the following 20 bytes when run (tested in CCBI, note the trailing space):

0 0 0 0 0 0 0 0 0 0 

Being unfamiliar with Befunge 98, I had to double check the spec a few times for this one:

  • k seems pretty broken to me, executing one more time than intended due to the IP moving into the repeated instruction
  • Befunge 98's stacks have infinite zeroes at the bottom
  • . outputs as a number, followed by a space
\$\endgroup\$
1
  • 20
    \$\begingroup\$ .... I....just....can't. \$\endgroup\$
    – Daniel M.
    Commented Oct 5, 2015 at 18:22
46
\$\begingroup\$

R, 3 2 bytes

Code

!0           # NOT FALSE

Outputs

TRUE

Wow, finally R, finally.

It seems that {} work too, it outputs NULL

Bonus 33 16 bytes:

Code

rep(1,number*16)

Outputs

# if number is not defined
> rep(1,number*16)                     
> Error: object 'number' not found     # output is 32 (or 2*16) bytes long error

# if number is defined
> number = 3                            
> rep(1,number*16)                     # output is 16*number bytes long
> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
\$\endgroup\$
1
  • 2
    \$\begingroup\$ I hope using the name "number" for the variable is not equivalent to " adding unnecessary whitespace to the source code" \$\endgroup\$
    – Mutador
    Commented Oct 7, 2015 at 19:33
31
\$\begingroup\$

JavaScript, 2 bytes!

Even shorter than the 3 bytes solution:

!0

Returns true after running.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ You should note that this only works in a REPL environment \$\endgroup\$
    – Cyoce
    Commented Apr 30, 2016 at 22:30
  • \$\begingroup\$ If the \n following the print counts as a byte, one could simply use 0 or any other single number. \$\endgroup\$ Commented Aug 12, 2020 at 2:18
  • \$\begingroup\$ I did 3E5 on my first one, but thought repl was cheating. \$\endgroup\$
    – user92753
    Commented Mar 22, 2021 at 18:27
28
\$\begingroup\$

Matlab, 8 bytes

1234;'5'

Output:

enter image description here

The output contains newline, ans =, newline twice, space four times, '5', newline, so 16 bytes.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Let's hope nobody has format compact as default :) \$\endgroup\$
    – Sanchises
    Commented Oct 3, 2015 at 8:47
  • 2
    \$\begingroup\$ @sanchises :-) Yes, that depends on how Matlab's preferences have been set. But format loose is the default preference \$\endgroup\$
    – Luis Mendo
    Commented Oct 3, 2015 at 9:19
  • 2
    \$\begingroup\$ How about 123;4? Is three bytes shorter=) \$\endgroup\$
    – flawr
    Commented Mar 30, 2016 at 10:30
  • \$\begingroup\$ @flawr Good idea! Post it yourself! \$\endgroup\$
    – Luis Mendo
    Commented Mar 30, 2016 at 10:32
  • \$\begingroup\$ Nope, way too much inspired by your answer=) \$\endgroup\$
    – flawr
    Commented Mar 30, 2016 at 10:33
21
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ I've been playing around with complex numbers, but unfortunately print.8/-9j is one char short \$\endgroup\$
    – Sp3000
    Commented Oct 3, 2015 at 15:21
  • \$\begingroup\$ Don't these print a trailing newline, bringing the output's byte count to 23? \$\endgroup\$ Commented Oct 27, 2015 at 16:01
  • 1
    \$\begingroup\$ @MartinBüttner That rule was edited in later, but changing it to print id,;1 suffices to suppress the trailing newline. It doesn't add a trailing space either. \$\endgroup\$
    – xnor
    Commented Oct 27, 2015 at 21:29
  • \$\begingroup\$ @xnor print 9**21 doesn't work. It's 21 characters. print 9**23 works though \$\endgroup\$ Commented Apr 13, 2017 at 7:42
20
\$\begingroup\$

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.
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – daniero
    Commented Oct 3, 2015 at 11:13
18
\$\begingroup\$

Brainfuck, 14 bytes

+++++++[....-]

This is a little mathematical exercise. Let's denote the number of + characters in the code by a, and the number of . characters by b.

The code outputs a*b bytes, with values from a down to 1 (these are non-ASCII bytes, but it seems OK according to the spec). The code's length is a+b+3. So we have

a*b = 2 * (a+b+3)

Trying different values for a and b, we see that the minimum for a+b+3 is achieved for

a = 4       or       a = 7
b = 7                b = 4
\$\endgroup\$
2
  • \$\begingroup\$ A veteran brainfuck programmer will spot the +[[>....]-<<+] option. \$\endgroup\$
    – orthoplex
    Commented Apr 5, 2019 at 14:29
  • \$\begingroup\$ @orthoplex When I read your comment I immediately portmanteau'd brainfuck and programmer: brainfucker \$\endgroup\$
    – Poke
    Commented Jun 7, 2019 at 20:36
17
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ First GS2 answer I've seen that doesn't have any card suit characters! \$\endgroup\$
    – Cyoce
    Commented Apr 30, 2016 at 22:32
17
\$\begingroup\$

TI-Basic, 3 bytes

1ᴇ5

Prints 100000.

\$\endgroup\$
2
  • \$\begingroup\$ Actually, I think the newline would count, so you can just do 0 for 1 byte. \$\endgroup\$
    – lirtosiast
    Commented Oct 3, 2015 at 22:58
  • 2
    \$\begingroup\$ How about ᴇ3? And @lirtosiast, since TI-Basic doesn't have newlines I'm skeptical about that. \$\endgroup\$
    – Jakob
    Commented Aug 15, 2017 at 23:36
16
\$\begingroup\$

Pyth, 10 9 - 25 = -16

-1 by Dennis

**N9?zvz2

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

isaacg has a shorter answer here

Pyth, 1 byte

T

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

\$\endgroup\$
3
  • \$\begingroup\$ It would be useful if .x worked with errors. Is there a justification for why it doesn't? \$\endgroup\$
    – lirtosiast
    Commented Oct 3, 2015 at 0:16
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Dennis
    Commented Oct 3, 2015 at 17:13
  • 2
    \$\begingroup\$ Any trailing newline is included in the output's byte count. T outputs 10 and a newline. Use d, it's a space which gets a newline appended \$\endgroup\$
    – Stan Strum
    Commented Sep 23, 2017 at 0:10
14
\$\begingroup\$

Perl 5, 16 bytes - 25 = -9

$_ =$]x($_*2||4)

This is an oddball approach to the problem.

Run with the -p command line argument.

I saw a Perl answer below that used a special variable to print more text - and thus shorten their byte count. So I used a much more verbose special variable. It prints 8 characters with a 2 character variable name. Thus, with a byte count of 16 (padded with one whitespace character to make it 16), it prints 2 * $], where $] is the Perl version printed as 5.xxxxxx, dependent upon your Perl version. Without input it prints it four times, equaling 8*4 or 32, which is double the byte count of the code.

I love Perl.

\$\endgroup\$
7
  • \$\begingroup\$ I think this fails for empty input. Per the rules, if no input is provided, the multiple should default to 2. \$\endgroup\$
    – Alex A.
    Commented Oct 3, 2015 at 0:12
  • \$\begingroup\$ @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 :( \$\endgroup\$
    – Codefun64
    Commented Oct 3, 2015 at 1:00
  • 2
    \$\begingroup\$ 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! \$\endgroup\$ Commented Oct 3, 2015 at 8:07
  • \$\begingroup\$ @DomHastings Man, thanks! I got it down to 2 bytes total because of your help :) Thank you very much for your suggestions. \$\endgroup\$
    – Codefun64
    Commented Oct 3, 2015 at 16:12
  • 1
    \$\begingroup\$ I think you can shorten it to this print"n"x(21*pop||42). Run on the command line with perl -e'print"n"x(21*pop||42)' 3 | wc -c \$\endgroup\$
    – hmatt1
    Commented Oct 6, 2015 at 16:33
10
\$\begingroup\$

JavaScript, 4 bytes

+1/0

Prints Infinity

I think this is the shortest possible JS solution without ES6 :P

\$\endgroup\$
9
  • \$\begingroup\$ What version of NodeJS do you have? This doesn't work with mine. \$\endgroup\$
    – Dennis
    Commented Oct 4, 2015 at 14:04
  • 1
    \$\begingroup\$ This also works on Firefox console. \$\endgroup\$ Commented Oct 5, 2015 at 7:29
  • 1
    \$\begingroup\$ The trailing newline should be included in the output length. \$\endgroup\$
    – lirtosiast
    Commented Oct 7, 2015 at 1:21
  • \$\begingroup\$ Node.js v0.10.40 doesn't print anything at all because there's no print command. Do you have a different version of Node.js or does this only work in a shell that prints the evaluation of a line? (in which case I don't think this answer would be valid). \$\endgroup\$
    – Nateowami
    Commented Oct 7, 2015 at 8:15
  • 3
    \$\begingroup\$ What about !0? \$\endgroup\$ Commented Sep 21, 2016 at 1:35
9
\$\begingroup\$

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.

\$\endgroup\$
9
\$\begingroup\$

C, 27 25

main(){printf("%50f",0);}

Thanks @Titus for knocking off 2 bytes


And for my non-competing 16 byte solution in C, go here: https://codegolf.stackexchange.com/a/111330/16513

^I say non-competing because the error code could possibly depend on your compiler, Also note I'm using GCC in that solution. Also I'm not certain if it breaks rule 1 or not, I think it probably does so I went ahead and labeled it non-competing

\$\endgroup\$
16
  • 1
    \$\begingroup\$ I do not think that counts as a valid C program but main(){printf("%0.XXf",0);} works. \$\endgroup\$
    – wefwefa3
    Commented Oct 4, 2015 at 14:59
  • 2
    \$\begingroup\$ @Zereges No, C has the "implicit declaration" rule that usually lets you use stuff without the proper #include. The compiler will emit a warning, and it will sometimes crash, but it will usually work. \$\endgroup\$
    – anatolyg
    Commented Oct 5, 2015 at 7:42
  • 1
    \$\begingroup\$ @AlbertRenshaw Also, I do not think it works here. \$\endgroup\$
    – Zereges
    Commented Oct 5, 2015 at 11:49
  • 1
    \$\begingroup\$ Note that 27*2 != 38. \$\endgroup\$
    – mbomb007
    Commented Oct 5, 2015 at 18:27
  • 1
    \$\begingroup\$ @Zereges. This code does not exhibit undefined behaviour. The "implicit declaration" and "implicit return type" features are deprecated in recent standards, but their usage is still permitted and results are well defined. There are circumstances in using such features causes undefined behaviour, but this code does not involve those. \$\endgroup\$
    – Peter
    Commented Oct 6, 2015 at 8:28
8
\$\begingroup\$

V, 2 Bytes

This outputs

ÿÿÿÿ

Try it online!

Explanation

This is a really hacky answer, and it works by abusing the internals of V. Essentially how it works is that ÿ is a command in V that signals the program is over, and any pending commands must complete. Otherwise, some implicit endings would not work, and the interpret would hang more often. This command is automatically sent several times at the end of the program, and most of the time has no effect on the output.

é is a command that inserts a single character. However, it does it by grabbing a raw byte, so it doesn't interpret ÿ as "end", it interprets it as "this is the character you need to insert." makes it insert this character 4 times instead of one.

\$\endgroup\$
7
\$\begingroup\$

bash, 11 bytes

Here's a pleasingly ironic usage of a data compression tool :) ...

gzip -f<<<2

Here's a hex dump of the output (22 bytes)...

0000000: 1f 8b 08 00 e3 ce 32 59 00 03 33 e2 02 00 90 af  ......2Y..3.....
0000010: 7c 4c 02 00 00 00                                |L....
\$\endgroup\$
8
  • \$\begingroup\$ This outputs 22 bytes of "extended ASCII" (eg. 8 bits per character). This seems to meet the OP's definition of ASCII if you read the comments on the Question post. \$\endgroup\$
    – Wossname
    Commented Jun 3, 2017 at 16:06
  • \$\begingroup\$ On TIO I got 16, but on my linux machine, I got 34 \$\endgroup\$
    – Stan Strum
    Commented Sep 23, 2017 at 0:27
  • \$\begingroup\$ @StanStrum, I don't know what TIO is, but that 34 under linux is odd. Perhaps you have gzip aliased to use different switches to normal? \$\endgroup\$
    – Wossname
    Commented Sep 23, 2017 at 9:31
  • \$\begingroup\$ I don't know if that's the case, but that does seem odd. I'll try it on some LiveCDs to see if I messed it up with one of my programs @Wossname \$\endgroup\$
    – Stan Strum
    Commented Sep 23, 2017 at 14:19
  • 1
    \$\begingroup\$ I'm not that guy, and I won't take advantage of my newly earned 129 rep tyvm. I'll see if I can make a version that works on my Linux installation \$\endgroup\$
    – Stan Strum
    Commented Sep 23, 2017 at 19:19
7
\$\begingroup\$

JavaScript ES6, 33 - 25 = 8

(a=2)=>Array(a*33).fill(0).join``

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ I can't seem to make this code run on console. Is it right? "Uncaught SyntaxError: Unexpected token =>" \$\endgroup\$
    – Malavos
    Commented Oct 5, 2015 at 18:24
  • \$\begingroup\$ @Malavos you need an es6 compliant browser such as the latest Firefox. Chrome doesn't support default args afaik \$\endgroup\$
    – DankMemes
    Commented Oct 5, 2015 at 19:00
  • \$\begingroup\$ I'm using UCBrowser, but that's right! It's based on chromium and gecko anyway. Tried it on nightly, got "000000000000000000000000000000000000000000000000000000000000000000". Nice! Could you explain your code for me? I'm a newbie. \$\endgroup\$
    – Malavos
    Commented Oct 5, 2015 at 19:06
  • 2
    \$\begingroup\$ @Malavos The hardcoded 33 is actually the length of the code. It creates a new array with length 33*a where a is the input parameter defaulting to 2 as per the OP, fills it, and returns a joined string. \$\endgroup\$
    – DankMemes
    Commented Oct 5, 2015 at 23:17
  • 2
    \$\begingroup\$ By creating your array with one extra element you can fill with the join Arrary(1+a*<length>) and join`0` \$\endgroup\$
    – Shaun H
    Commented Oct 6, 2015 at 17:27
6
\$\begingroup\$

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.

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

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.

\$\endgroup\$
2
  • \$\begingroup\$ Some golfing hints: a for loop is shorter than a while loop and lets you drop the braces, it's slightly cheaper to use cstdio and just printf a string, and you don't need to return anything. \$\endgroup\$
    – Luke
    Commented Oct 4, 2015 at 4:31
  • \$\begingroup\$ You can strip your approach to 63 bytes: #include<iostream> int main(){for(int i=10;--i;)std::cout<<&i;} But there is already a shorter answer with a slightly different approach. \$\endgroup\$
    – movatica
    Commented May 31, 2019 at 10:17
6
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ @ThomasKwa Whoops. Updating in a sec. \$\endgroup\$ Commented Oct 2, 2015 at 23:43
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Oct 3, 2015 at 16:46
  • \$\begingroup\$ 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) \$\endgroup\$ Commented Oct 3, 2015 at 16:47
  • \$\begingroup\$ Uh.. nevermind, I just saw Dennis's answer now :p \$\endgroup\$ Commented Oct 3, 2015 at 16:53
  • \$\begingroup\$ 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**. \$\endgroup\$ Commented Oct 3, 2015 at 18:14
6
\$\begingroup\$

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

l?!2f4+*v
-1o;!?::<

test it here!

Thanks Cole and Sp3000

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)

\$\endgroup\$
7
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – cole
    Commented Oct 3, 2015 at 0:05
  • \$\begingroup\$ 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. \$\endgroup\$
    – torcado
    Commented Oct 3, 2015 at 0:16
  • \$\begingroup\$ 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) \$\endgroup\$
    – torcado
    Commented Oct 3, 2015 at 0:32
  • \$\begingroup\$ Happy to help, especially if it's ><>. \$\endgroup\$
    – cole
    Commented Oct 3, 2015 at 0:47
  • 2
    \$\begingroup\$ ?! is usually better than 0=? \$\endgroup\$
    – Sp3000
    Commented Oct 4, 2015 at 2:59
6
\$\begingroup\$

JavaScript (ES5), 68 bytes - 25 bonus = 43

alert(Array(1+(+prompt()||2)).join(document.scripts[0].textContent))

(in case your browser won't allow for the snippet to run for security reasons, try this fiddle http://jsfiddle.net/thePivottt/c3v20c9g/ )

This script only works in a browser following at least DOM3 (with Node.textContent) and ECMAScript 5 (or perhaps an older version). I tried to make is as standard conforming and compatible as possible. It also assumes that the script is in the first script element of the document.

It actually concatenates multiple copies of the script itself, which is pretty awesome. Note that the snippet tool on SE puts extra whitespace around the script. We could ignore that whitespace with .trim() but I don't find it necessary considering the program is perfect without SE's meddling. Just save this HTML5 file if you want to see it run perfectly.

<!DOCTYPE html>
<html>
  <head>
    <title>Minimalist HTML5 page</title>
    <script>alert(Array(1+(+prompt()||2)).join(document.scripts[0].textContent))</script>
  </head>
</html>

This script uses prompt and alert because console.log is not part of any standard, even if most modern browsers use it. If the number of repetitions passed is not a valid number or is empty, it defaults to 2. If the input is a decimal number, the program crashes due the the invalid array length.

The code uses a few interesting features of JavaScript:

  • Array(1+(+prompt()||2))

    • Array(INT) creates an Array of INT cells.

    • +prompt() takes an input and turns it into a number. If we passed the input as a string, the Array function would simply wrap it in a one-element array.

    • +prompt()||2 returns the input if it is truthy, else it returns 2.

    • This whole code creates an array of N empty elements, where N is one more than the amount of repetitions asked.

  • .join(document.scripts[0].textContent)

    • The array's join(STRING) method creates a string by concatenating all the cells, putting the provided STRING between values. In this program, there are N+1 empty elements in the array, or exactly N in-between spots. The result will be a string containing N times the provided STRING.

    • document.scripts[o] is the first <script> element of the document.

    • The textContent of Node instances returns the whole text found inside them and their child nodes, including scripts.

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

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.

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I had a very similar answer of: print$=x9x(pop||2) nice! \$\endgroup\$ Commented Oct 3, 2015 at 8:54
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.