# Shortest code for longest output [closed]

This one is pretty simple.

Write the shortest program you can, while generating the most output.

To make it fun, programs that produce infinite output will be disqualified.

The winner is the program with the largest output size/code size ratio.

The results are based on what works on my computer, which is a Mac running Mac OS X 10.7.5 with an Intel Core i5 and 8GB of memory.

• It's not clear exactly what you're asking. Should answers assume unbounded memory, unbounded index sizes, etc? Nov 3, 2013 at 23:08
• @PeterTaylor I fixed that. Nov 3, 2013 at 23:22
• Be blowed if I can figure it out, but I'm sure there is a way to have some interpretter interpret an empty file and produce any content - which would also give an infinite ratio.
– user8777
Nov 4, 2013 at 1:24
• @LegoStormtroopr GolfScript fits the bill. Executing an empty script will produce exactly one character of output: \n Nov 4, 2013 at 5:18
• @user2509848 no, because, like I said, infinite output does not count. Jan 10, 2014 at 0:35

### Python: 8 chars code, 387420489 chars output -- Ratio: 48427561.125 : 1

'a'*9**9


We can have the ratio tend to infinity by adding more **9s:

'a'*9**9**9
'a'*9**9**9**9
etc.


For example:

'a'*9**9**9**9**9**9


which has a ratio of ~10101010108.568 (unimaginably large number).

• Better than the other one... Nov 3, 2013 at 22:44
• @tbodt Why? O_o @arshajii If you add enough **9s, wouldn't it eventually become Infinity? Nov 3, 2013 at 22:45
• @Doorknob Sorry, I don't know everything about python. Now the challenge is: Figure out the maximum number of **9s you can put before the output becomes Infinity. Nov 3, 2013 at 22:46
• @Doorknob Python ints have arbitrary precision. Nov 3, 2013 at 22:47
• @tbodt See the comment above. Nov 3, 2013 at 22:48

So, these are all good programs that produce lots of output with very little code, but none of them are really short...

brainfuck, 5 characters, 255 bytes of output

-[.-]


I think this is the only use case where brainfuck really excels. I know this one isn't going to win, but I don't think we can possibly do better than the Python example. Not only that, but...

brainfuck, 4 characters, infinite output

-[.]


I conjecture that this is the shortest infinite-output program out there.

Actually, hold on, my mate's just come up with a really good one.

Python, 80 bytes, unknown amount of output

from datetime import datetime
while datetime.now()!=datetime.max()
print "This will probably get disqualified"


This program will definitely halt eventually, but will only do so after roughly 8,000 years. The exact number of characters output depends on the rate at which your computer can produce characters.

• I like the python one :D Nov 22, 2013 at 3:44
• "I conjecture that this is the shortest infinite-output program out there" nope, this is where Befunge's implicit loop (via wraparound) comes handy: . outputs an infinite stream of 0 characters. Dec 2, 2013 at 0:07

## Perl - 19 bytes, 187200000000000000 bytes output (9852631578947368.42 : 1)

print+($]x9e7)x26e7  166 petabyes with a single print statement, using no more than 1.7GB of memory. There's a few things that made this challenge more interesting that I thought it would be. Perl seems to refuse to allocate more than 1GB of memory to any single list. Hence, the 4-byte scalar reference to the inner string may only be repeated 26e7 ≈ 228 times. $] is the 'old perl version' number which, as a string, is 8 bytes long, resembling 5.016002.

With more system memory, it should be able to go higher. Assuming the full 8GB were actually available, you should be able to use \$]x9e8 for the inner string instead, which would output 1.62 exabytes.

• "If you give a 1,000,000 monkeys 1,000,000 typewriters, and give them 1,000,000 years to write stuff, one monkey will eventual write a Java program. The others just produce Perl scripts." That's what I thought when I saw this :P source Nov 6, 2013 at 2:55

## Ruby and Python, 13 chars, 599994 char output, ~46153:1 ratio

999999**99999


Simply raises a very large number to the power of another very large number. Takes about 20 seconds to run. I can't increase the numbers because that would make the number become Infinity.

(I did this earlier, I am currently working on making a loop for even longer output)

Edit: I did it!

## Ruby, 28 chars, 6e599999 char output, ~6e599998 ratio (I think)

a=999999**99999;a.times{p a}


Untested (for obvious reasons), but I'm pretty sure the first number is about 1e599994, which multiplied by 599994 is about 6e599999. Theoretically it would work, but I'm not sure if it would crash your computer, so disclaimer: I am not responsible if it harms your computer in any way :P

Of course, you can keep going:

## Ruby, 37 chars, 6e359992800041 char output, ~6e359992800040 ratio

a=999999**99999;a.times{a.times{p a}}


And so on, but I doubt any computer can handle that :P

• That's really a polyglot... Nov 3, 2013 at 22:33
• @tbodt Hehe, right! When I add my changes it won't be though Nov 3, 2013 at 22:33

If infinite input was allowed,

cat /dev/random


Since it's not,

head -99 /dev/random


(25128 output : 20 input = 1256.4:1)

I'm not on a Linux box, but I imagine you could do something like

timeout 99d cat /dev/random


and get a huge output. (via GigaWatt's response)

• You can substitute one character and make your output 8562 times longer: timeout 99d. Yep, 99 day runtime. Also, I'm not certain on this, but eventually you'll empty the entropy pool with /dev/random and it'll block, so /dev/urandom may be more appropriate. (I managed to get 40 MB/s with urandom and only 128 KB/s with random) Nov 5, 2013 at 17:56
• @GigaWatt that is awesome. Nov 7, 2013 at 19:14

## HQ9+, 11471

9


Actual character count varies depending on the interpreter, but probably around 10000 would be right?

• What is HQ9+? I've never heard of it. Nov 5, 2013 at 19:08
• Ahh, sorry, it was mostly a joke since it's not a 'real' programming language, but: esolangs.org/wiki/HQ9%2B Nov 5, 2013 at 19:14

# C#: 108 chars. Ratio: 742123445489230793057592 : 1

for(ulong i=0;i<ulong.MaxValue;i++){Console.Write(new WebClient().DownloadString(@"http://bit.ly/dDuoI4"));}


It just downloads and prints the wikipedia's List of law clerks of the Supreme Court of the United States (4344904 chars) 18446744073709551615 times.

• That requires a URL shortener. I'm not sure about that. Dec 1, 2013 at 19:01
• Ok. And what about this: for(ulong i=0;i<ulong.MaxValue;i++){Console.Write(new string('a',int.MaxValue));} 2147483647 chars * 18446744073709551615 times = 39614081238685424720914939905 / 81 => Ratio: 489062731341795366924875801 Dec 1, 2013 at 20:02
• Much better. No URL shortener required. Dec 2, 2013 at 1:22

# ~-~! - Ratio: (6444464)/154 ~= 10101010101.583328920493678

'=~~~~,~~~~,~~~~:''=|*<%[%]'=',',',':''&*-~|:'''=|*<%[%]''&',',',':'''&*-~|:''''=|*<%[%]'''&',',',':''''&*-~|:''''&':''=|*<%[%]@~~~~,~~~~,~~:''&*-~|:''&':


How it works: First, it sets ' to 4^3, or 64. Then, it makes '' a function which sets ' to '^4 * times (where * is it's input). ''' is then made a function which calls '' with the input as '^4. Then, '''' is made a function which calls ''' with '^4 as it's input. '''' is then called with an input of 64. Finally, '' is changed to a function which prints a space * times; this is then called with an input of '.

Turns out, in the end, ' is 6444464, and my program's length is 154; punch that into Wolfram|Alpha and it spits out 10101010101.583328920493678, which it doesn't even bother to calculate. I don't even know how many digits it contains, but 6444 contains 463. Pretty nice for a language that only supports unary explicit numbers and doesn't have an exponent function ;3

I could have made this a lot larger, but, overkill.

## Javascript: 27 chars; 260,431,976 char output; 9,645,628.74 ratio

for(s=i=61;s=btoa(s),i--;)s


This code recursively encodes the input 61 to Base64 61 times. Encoding any input of length n to Base64 produces an output of length n * 8/6, rounded up to a multiple of 4.

This must be run from a JavaScript console environment that natively supports the Base64-encoding function btoa. (Any modern browser, but not Node.js.) Note Chrome cannot run higher than i=61, while Firefox can only reach i=60. Note also that Chrome's console cannot actually display the output because it is too large, but you can verify the result size by running

for(s=i=61;s=btoa(s),i--;)s.length


If this program were allowed to run for the maximum i=99, it would produce a hypothetical output of size 14,566,872,071,840 (14.5 trillion, 14.5e12) chars, for a hypothetical ratio of around 540 billion (5.39e11).

# Befunge-93: 48 characters, about ((2^32)^2)*10 characters output

Befunge's stack is theoretically infinite, but the numbers that stack stores are limited to the size of an unsigned long integer (here assumed to be 32 bits). Thus, to a Befunge interpreter, (x+1)>x is false for the proper value of x. We use this fact to first push all values from zero to the maximum (twice, with a one every third number), and then for each value on the stack, we output and decrement it, then pop it when it reaches zero. Eventually the stack empties and the program terminates. I may be a bit off on the output size, but it should be somewhere in that ballpark.

>::1# + #1\# :# _> #- #1 :# :# .# _# .# :# _@


## C: 48 chars, approx. (2^32 - 1)*65090 bytes output

main(){for(int i=1<<31;i<~0;)puts("!");main();}


Note that the 65090 is not exact, and depends on stack size. The program will eventually stop when it crashes. Also, I could just put a longer and longer string in puts() to make the ration approach infinity, but that seems rather cheaty.

• that's an infinite loop Mar 11, 2014 at 0:15
• Ouch, you're right, I think. I'll see if I can figure how to fix that. Mar 11, 2014 at 2:05

## Ruby, 23 characters - ~500000000000000 (5e14) Output

while rand
puts 0
end


## Ti-Basic 84, 13 characters - ~3000 Output

:Disp 1
:prgmA


Name the program prgmA

## ruby, 28396 44 chars

a=1e99.times;a{a{a{a{a{puts'a'*99}}}}}}}}}}


Not very short, but it compensates for it in output, which is so much I haven't yet been able to measure it.

• by my calculations this would be about 1e127 chars output. anyway, assigning to a variable would cut the code size about in half. also, 1e99 is a bigger number that takes less space. also, use map instead of each, use puts instead of print, remove extra whitespace between print and "ier.... You could also replace that big string with 'a'*999 (or even ?a*999) which is a longer string that takes less space. summary: this is not golfed at all Nov 4, 2013 at 0:01
• @Doorknob Thanks. I know nothing about ruby, except chapter 3 of why's poignant guide to ruby. Nov 4, 2013 at 0:17
• yeah, why don't you just assign (0..1e99).map to a variable? like a=(0...1e99).map;a{a{a{a{a{puts'a'*99}}}}} Nov 4, 2013 at 3:01
• My Ruby interpretter ran out of memory evaluating a=(0...1e99).map. You might want to tone that back a bit. 0..1e9 would use about 4GB. Nov 5, 2013 at 17:06

# Mathematica 9 chars Ratio: ~ 4564112 : 1

The following is a picture of Mathematica input. I haven't figured how to render it in SE.

Here's a screen shot showing the number of digits in the output. IntegerDigits converts the output to a list of digits. Length counts the number of digits.

Keystrokes to enter: 9, ctrl6, 9, ctrl6, 9, ctrl6, 9, ctrl6, 9....

• What? You can type that into Mathematica? Nov 3, 2013 at 23:32
• Yes, this is legitimate input to Mathematica. Nov 3, 2013 at 23:33
• And what keystrokes are required? Nov 3, 2013 at 23:34
• @tbodt Keystrokes are now shown in the answer. Nov 4, 2013 at 0:01
• I don't know if you would have to specify order, but you would want to have it calculate top down: (9^9)^9 is a 78 digit number, but 9^(9^9) is a 369,693,100 digit number (thanks, wolframalpha) Nov 5, 2013 at 17:19

# Python 3, 115 bytes, runs for 7983 years (unknown # of chars)

EDIT: ymbirtt beat me to it ._.

I know, this isn't really short, and I know that the other Python answer is much longer, But I decided to give this a shot.

The program runs for about 8000 years, which, as you know, is a pretty long time.

What it does is continuously get the current time using the datetime.datetime.now() function, and it compares it to 9999-12-31 24:59:59.999999, which is as far as I know the maximum date in Python.

If is is equal, the program stops. If it isn't, it continously outputs a.

import datetime
while 1:
if str(datetime.datetime.now())=="9999-12-31 24:59:59.999999":exit
else:print("a")

• What if you miss that moment? May 14, 2016 at 16:28
• @C5H8NNaO4 If you leave it on for all 7983 years, you hopefully won't miss it.
– m654
May 14, 2016 at 16:31

java(131): unknown but finite amount

class A{public static void main(String[] args){while(Math.random()>0){for(long l=0;l!=-1;l++){System.out.println("1234567890");}}}}
`

Using the low chance of Math.random() to get to 0 in a loop and then goin 2^64-1 loops thru a foreach with the output 1234567890;