78
\$\begingroup\$

Anybody can make the output of a program bigger by adding characters, so let's do the exact opposite.

Write a full program, an inner function or a snippet for a REPL environment in a language of your choice that satisfies the following criteria:

  1. Your code must be at least 1 character long.

  2. Running the original code produces x characters of output to STDOUT (or closest alternative), where 0 ≤ x < +∞.

  3. Removing any arbitrary single character from the original code results again in valid code, which produces at least x + 1 characters of output to STDOUT.

  4. Neither the original code nor the modifications may produce any error output, be to STDOUT, STDERR, syslog or elsewhere. The only exceptions to this rule are compiler warnings.

    Your program may not require any flags or settings to suppress the error output.

    Your program may not contain any fatal errors, even if they don't produce any output.

  5. Both the original code and the modifications must be deterministic and finish eventually (no infinite loops).

  6. Neither the original code nor the modifications may require input of any kind.

  7. Functions or snippets may not maintain any state between executions.

Considering that this task is trivial is some languages and downright impossible in others, this is a .

When voting, please take the "relative shortness" of the code into account, i.e., a shorter answer should be considered more creative than a longer answer in the same language.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ While the 1 byte solution is impressive, it would be more impressive to see who can come up with the highest ratio of x:x+n. i.e. the length of normal output compared to the average length of output when any one character is removed. Adds an extra challenge to this question in my opinion. \$\endgroup\$
    – Trent
    Commented Jun 24, 2015 at 2:49
  • \$\begingroup\$ @FizzBuzz Easy: 111111111111111111^111111111111111111 (if you meant the lowest ratio). \$\endgroup\$
    – jimmy23013
    Commented Jun 24, 2015 at 12:30
  • 2
    \$\begingroup\$ Aw, just noticed 'no infinite loops.' I was working on creating a ><> program that would create output faster if an one character was removed, such that after a constant k instructions, the output of each program is strictly greater than the output of the original from then on (because the other programs would loop faster or output more each loop). It was looking pretty interesting. Maybe I'll see if I can finish it anyway, and make another challenge. \$\endgroup\$
    – mbomb007
    Commented Jun 24, 2015 at 17:28
  • \$\begingroup\$ An interesting scoring metric for this challenge could be "most unique characters, ties go to shortest length". We would then try to get all the characters in a string literal though. \$\endgroup\$
    – lirtosiast
    Commented Jul 1, 2015 at 23:11
  • \$\begingroup\$ What is meant by an inner function? \$\endgroup\$
    – dfeuer
    Commented Apr 1, 2019 at 4:16

27 Answers 27

99
\$\begingroup\$

Any REPL with caret XOR operation, 5 bytes

11^11

11^11 is of course 0. The only other possibilities are 1^11 or 11^1 which are 10, or 1111 which produces itself.

\$\endgroup\$
5
  • 7
    \$\begingroup\$ Well played, feersum. Well played. \$\endgroup\$
    – Alex A.
    Commented Jun 23, 2015 at 19:48
  • 14
    \$\begingroup\$ Minus would also do the trick. \$\endgroup\$ Commented Jun 23, 2015 at 19:49
  • 11
    \$\begingroup\$ Similarly, 99|99 \$\endgroup\$
    – Sp3000
    Commented Jun 24, 2015 at 3:30
  • \$\begingroup\$ @Challenger5 -10 is longer than 0. \$\endgroup\$ Commented Nov 8, 2017 at 8:20
  • \$\begingroup\$ @MartinEnder Oh, I see. For some reason I thought it had to be a larger number. \$\endgroup\$ Commented Nov 8, 2017 at 8:25
57
\$\begingroup\$

TI-BASIC, 3 1

"

When the last line of a program is an expression, the calculator will display that expression. Otherwise, the calculator displays Done when the program finishes. The expression here is the empty string, but it could also work with any one-digit number.

2 bytes:

isClockOn

Same as the above but with a 2-byte token.

3 bytes:

ππ⁻¹

Prints 1 due to implied multiplication. Can be extended indefinitely by adding pairs of ⁻¹'s. The below also work.

√(25
e^(πi
⁻ii
ii³
2ππ   
cos(2π

Longer solutions:

11/77►Frac
ᴇ⁻⁻44
cos(208341   //numerator of a convergent to pi; prints "-1"

There are probably also multi-line solutions but I can't find any.

\$\endgroup\$
4
  • \$\begingroup\$ I'm not familar with TI BASIC. Are you counting the sqrt and parenthesis as a single character? \$\endgroup\$ Commented Jun 23, 2015 at 20:03
  • 6
    \$\begingroup\$ Yes, the sqrt and parenthesis are together a single token, stored as one byte in the calculator's memory and entered with one keypress. \$\endgroup\$
    – lirtosiast
    Commented Jun 23, 2015 at 20:04
  • 8
    \$\begingroup\$ finally a way to stop that annoying Done message! \$\endgroup\$ Commented Jun 23, 2015 at 20:33
  • 1
    \$\begingroup\$ There is no Done token; the calculator displays Done when finished executing any program without an expression on the last line (including the empty program). \$\endgroup\$
    – lirtosiast
    Commented Jun 28, 2015 at 17:27
50
\$\begingroup\$

CJam, JavaScript, Python, etc, 18 bytes

8.8888888888888888

The outputs in CJam are:

8.8888888888888888 -> 8.88888888888889
8.888888888888888  -> 8.888888888888888
88888888888888888  -> 88888888888888888
.8888888888888888  -> 0.8888888888888888

JavaScript and Python work in similar ways. It isn't competitive in JavaScript and Python, but it's not easy to find a shorter one in CJam.

\$\endgroup\$
1
  • 16
    \$\begingroup\$ Nice floating-point abuse! \$\endgroup\$
    – nderscore
    Commented Jun 25, 2015 at 1:10
31
\$\begingroup\$

Octave, 5 bytes

10:10

(x : y) gives the array of numbers between x and y in increments of 1, so between 10 and 10 the only element is 10:

> 10:10
ans = 10

When the second argument is less than the first, octave prints the empty matrix and its dimensions:

> 10:1
ans = [](1x0)

> 10:0
ans = [](1x0)

When a character is removed from the first number, there are more elements in the array:

> 1:10
ans = 1 2 3 4 5 6 7 8 9 10

> 0:10
ans = 0 1 2 3 4 5 6 7 8 9 10

When the colon is removed, the number returns itself:

> 1010
ans = 1010
\$\endgroup\$
1
  • 1
    \$\begingroup\$ also valid in R \$\endgroup\$
    – mnel
    Commented Mar 8, 2016 at 10:17
30
\$\begingroup\$

Microscript, 1 byte

h

This produces no ouput, as h suppresses the language's implicit printing. Removing the sole character produces a program whose output is 0\n.

I'll try to come up with a better answer later.

EDIT ON NOVEMBER 17:

This also works in Microscript II, with the exception that, instead of yielding 0\n, the empty program yields null.

\$\endgroup\$
22
\$\begingroup\$

Pyth, 3 bytes

cGG

G is preinitialized with the lowercase letters in the alphabet. c is the split-function.

cGG splits the alphabet by occurrences of the alphabet, which ends in ['', ''] (8 bytes).

When the second parameter is missing, c splits the string by whitespaces, tabs or newlines. Since none of them appear in G, the output for cG is ['abcdefghijklmnopqrstuvwxyz'] (30 bytes).

And GG simply prints twice the alphabet on two seperate lines: abcdefghijklmnopqrstuvwxyz\nabcdefghijklmnopqrstuvwxyz (53 bytes).

Try it online: Demonstration

\$\endgroup\$
17
\$\begingroup\$

Python REPL, 6 bytes

Not the shortest, but here's another floating point abuse answer:

>>> 1e308
1e+308
>>> 11308
11308
>>> 11e08
1100000000.0
>>> 11e38
1.1e+39
>>> 11e30
1.1e+31

But...

>>> 11e308
inf
\$\endgroup\$
14
\$\begingroup\$

JavaScript (and a lot more), 5byte

44/44 or

44/44 -> 1
44/4  -> 11
4444  -> 4444
4/44  -> 0.09090909090909091
\$\endgroup\$
12
\$\begingroup\$

CJam, 5

''''=

Try it online

''''=   compares two apostrophes and prints 1
'''=    prints '=
''''    prints ''
\$\endgroup\$
12
\$\begingroup\$

Lenguage, 5 bytes

00000

The length of the program is 5 which corresponds to the brainf*** program , reading an end of input character and terminating without output.

Removing any char results in the code 0000 which has a length of 4 corresponding to the brainf*** program . printing out one character (codepoint 0) and terminating.

The Unary equivalent would be 0000000000000 (13 zeros) because you have to prepend a leading 1 to the binary length of the code so 101 becomes 1101.

\$\endgroup\$
0
10
\$\begingroup\$

PHP, 6 bytes

I have been watching this group for a couple of weeks and am amazed of your programming techniques. Now I had to sign in, there is something I can do, sorry :-) However, this might be my last post here...

<?php 

(note the space after second p)

This outputs empty string. Removing any character outputs the text without the character. Note it can produce HTML errors (content not rendered by browsers for eg. <?ph).

I also tried with the echo tag. ie. eg.:

<?= __LINE__;;

This one outputs 1. If = is omitted, <? __LINE__;; is the output. However, removing any of the magic constant character will result in E_NOTICE: Notice: Use of undefined constant LNE - assumed 'LNE' in...

If notices are not considered errors (point 4 of rules), this also applies :-)

\$\endgroup\$
4
  • \$\begingroup\$ Oh, that's clever. I guess a linefeed would work as well. If the task was to produce HTML, this would be invalid. However, PHP can be run from the command line, just like any other programming language, where the output is simply printed to the screen. \$\endgroup\$
    – Dennis
    Commented Jun 25, 2015 at 20:43
  • \$\begingroup\$ @Dennis PHP outputs anything. You can always use CTRL-U in your browser to see the output. HTML might be invalid, but in fact the program is not (there is no <HTML> tag etc.) \$\endgroup\$
    – Voitcus
    Commented Jun 25, 2015 at 20:45
  • 1
    \$\begingroup\$ My point is that there's no need to involve a browser at all. Just execute php test.php. The second code is invalid by the way. The question does not forbid errors but error output, meaning errors, warning, notices, etc. \$\endgroup\$
    – Dennis
    Commented Jun 25, 2015 at 20:48
  • 2
    \$\begingroup\$ #! in a script file also works. \$\endgroup\$
    – jimmy23013
    Commented Jun 26, 2015 at 1:46
10
\$\begingroup\$

Python, 10 8 bytes

256**.25

Works in Python and friends. Thanks to Jakube for showing how to make it 2 bytes smaller.

From IDLE:

>>> 256**.25
4.0
>>> 26**.25
2.2581008643532257
>>> 56**.25
2.7355647997347607
>>> 25**.25
2.23606797749979
>>> 256*.25
64.0
>>> 256*.25
64.0
>>> 256**25
1606938044258990275541962092341162602522202993782792835301376L
>>> 256**.5
16.0
>>> 256**.2
3.0314331330207964

originally I had this (10 bytes):

14641**.25

From IDLE:

>>> 14641**.25
11.0
>>> 4641**.25
8.253780062553423
>>> 1641**.25
6.364688382085818
>>> 1441**.25
6.161209766937384
>>> 1461**.25
6.18247763499657
>>> 1464**.25
6.185648950548194
>>> 14641*.25
3660.25
>>> 14641*.25
3660.25
>>> 14641**25
137806123398222701841183371720896367762643312000384664331464775521549852095523076769401159497458526446001L
>>> 14641**.5
121.0
>>> 14641**.2
6.809483127522302

and on the same note:

121**.25*121**.25

works identically due to nice rounding by Python, in 17 bytes.

>>> 121**.25*121**.25
11.0
>>> 21**.25*121**.25
7.099882579628641
>>> 11**.25*121**.25
6.0401053545372365
>>> 12**.25*121**.25
6.172934291446435
>>> 121*.25*121**.25
100.32789990825084
>>> 121*.25*121**.25
100.32789990825084
>>> 121**25*121**.25
3.8934141282176105e+52
>>> 121**.5*121**.25
36.4828726939094
>>> 121**.2*121**.25
8.654727864164496
>>> 121**.25121**.25
29.821567222277217
>>> 121**.25*21**.25
7.099882579628641
>>> 121**.25*11**.25
6.0401053545372365
>>> 121**.25*12**.25
6.172934291446435
>>> 121**.25*121*.25
100.32789990825084
>>> 121**.25*121*.25
100.32789990825084
>>> 121**.25*121**25
3.8934141282176105e+52
>>> 121**.25*121**.5
36.4828726939094
>>> 121**.25*121**.2
8.654727864164496
\$\endgroup\$
4
  • \$\begingroup\$ @Akiino I do not understand the edit you made. The line you removed was one of the required possibilities and no different then any of the ones below it. Could you explain why you removed it? I rolled it back, but can concede to undo the action with adequate reasoning. \$\endgroup\$
    – rp.beltran
    Commented Jan 7, 2016 at 19:05
  • \$\begingroup\$ My inner perfectionist just couldn't sleep well knowing that the line (the one that i removed) is duplicated for no reason. There are lines 56..., 26... and then 56... again, but there is only one way to get 56, so it should be included only once, isn't it? \$\endgroup\$
    – user38962
    Commented Jan 9, 2016 at 12:01
  • \$\begingroup\$ Oh, I didn't see that it was on there twice. My bad, good catch. \$\endgroup\$
    – rp.beltran
    Commented Jan 9, 2016 at 18:51
  • \$\begingroup\$ I rolled back my rollback. \$\endgroup\$
    – rp.beltran
    Commented Jan 9, 2016 at 18:52
7
\$\begingroup\$

SWI-Prolog interpreter

__A=__A.

Note: You cannot remove the final .. Prolog interpreters will always look for a final period to run your query, so if we stick strictly to the rules of this contest and allow ourselves to remove the period it won't run, it will jump a line and wait for additional commands until one is ended by a period.

The original query __A=__A. outputs true..

The query _A=__A. outputs _A = '$VAR'('__A'). Similar modifications (i.e. removing one _ or one of the two A) will result in similar outputs.

Finally, the query __A__A. outputs in SWI-Prolog:

% ... 1,000,000 ............ 10,000,000 years later
% 
%       >> 42 << (last release gives the question)
\$\endgroup\$
2
  • 12
    \$\begingroup\$ @Jakube I don't see how having a mandatory final period is a lot different than having to input the enter key to run a snippet in another language. If you remove the linefeed it won't run either. \$\endgroup\$
    – Fatalize
    Commented Jun 24, 2015 at 9:34
  • \$\begingroup\$ It is more equivalent to a semicolon in some languages, and that does count as a character. \$\endgroup\$
    – tomsmeding
    Commented Jun 29, 2015 at 16:26
5
\$\begingroup\$

Sed, 1 byte

d

As sed requires an input stream, I'll propose a convention that the program itself should be supplied as input.

$ sed -e 'd' <<<'d' | wc -c
0
$ sed -e '' <<<'d' | wc -c
2

An alternative program is x, but that only changes from 1 to 2 bytes of output when deleted.

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

K, 3 bytes

2!2

Outputs 0 in the REPL. Removing the first 2 outputs 0 1, removing the exclamation results in 22, and removing the last 2 results in a string that varies between K implementations but is always at least 2 characters (in oK, it's (2!); according to @Dennis, Kona outputs 2!).

\$\endgroup\$
4
  • \$\begingroup\$ @Sp3000 Removing the * just prints 2. Removing the 2 prints (*!) (an incomplete function) in oK; I don't know about other interpreters (e.g. Kona, k2, etc.), although I believe Kona would print *[!;] or something similar. I went with removing the star because it seemed the safest. \$\endgroup\$ Commented Jun 24, 2015 at 1:23
  • \$\begingroup\$ You can't choose, It must work removing any character \$\endgroup\$
    – edc65
    Commented Jun 24, 2015 at 1:24
  • \$\begingroup\$ @edc65 Fixed at the expense of not a single byte. \$\endgroup\$ Commented Jun 24, 2015 at 2:01
  • 1
    \$\begingroup\$ @Dennis Updated. \$\endgroup\$ Commented Jun 24, 2015 at 14:22
5
\$\begingroup\$

MATLAB, 9 7 bytes

It's 2 bytes longer than the other MATLAB/Octave answer, but I like it nonetheless, as it's a bit more complex.

Matlab's ' operator is the complex conjugated transpose. Using this on a scalar imaginary number, you get i' = -i. As imaginary numbers can be written simply as 2i one can do:

2i--2i'
ans =
     0    

Removing any one of the characters will result in one of the below:

ans =
   0.0000 - 1.0000i
   2.0000 - 2.0000i
   0.0000 + 4.0000i
   0.0000 + 4.0000i
   0.0000 + 1.0000i
   2.0000 + 2.0000i
   0.0000 + 4.0000i
\$\endgroup\$
0
4
\$\begingroup\$

GolfScript, 2 bytes

This answer is non-competing, but since it is the piece of code that inspired this challenge, I wanted to share it anyway.

:n

By default, all GolfScript programs print the entire stack, followed by a linefeed, by executing puts on the entire stack. The function puts itself is implemented as {print n print} in the interpreter, where print is an actual built-in and n is a variable that holds the string "\n" by default.

Now, a GolfScript program always pushes the input from STDIN on the stack. In this case, since there isn't any input, an empty string is pushed. The variable assignment :n saves that empty string in n, suppressing the implicit linefeed and making the output completely empty.

By eliminating n, you're left with the incomplete variable assignment : (you'd think that's a syntax error, but nope), so the implicit linefeed is printed as usual.

By eliminating :, you're left with n, which pushes a linefeed on the stack, so the program prints two linefeeds.

\$\endgroup\$
4
\$\begingroup\$

J, 5 bytes

|5j12

Magnitude of the complex number 5 + 12i in REPL.

   |5j12 NB. original, magnitude of the complex number `5 + 12i`
13
   5j12  NB. the complex number `5 + 12i`
5j12
   |j12  NB. magnitude of the undefined function j12
| j12
   |512  NB. magnitude of 512
512
   |5j2  NB. magnitude of 5 + 2i
5.38516
   |5j1  NB. magnitude of 5 + 1i
5.09902

.

J, 9 bytes

%0.333333

Based on floating point precision, reciprocal and matrix inverse.

   %0.333333 NB. original, reciprocal of 0.333333
3
   0.333333  NB. 0.333333
0.333333
   %.333333  NB. matrix inverse of 333333
3e_6
   %0333333  NB. reciprocal of 333333
3e_6
   %0.33333  NB. reciprocal of 0.33333
3.00003

Try it online here.

\$\endgroup\$
4
\$\begingroup\$

APL, J and possibly other variants, 3 bytes

--1

It outputs 1 in APL. -1 outputs ¯1, and -- outputs the following in TryAPL:

┌┴┐
- -
\$\endgroup\$
4
\$\begingroup\$

Mathematica, 3 bytes

4!0

4!0  ->  0    the factorial of 4, times 0
4!   ->  24   the factorial of 4
40   ->  40
!0   ->  !0   the logical not of 0, but 0 is not a boolean value
\$\endgroup\$
3
\$\begingroup\$

Dyalog APL, 2 bytes

⍴0 returns an empty string (length 0)

returns (length 1)

0 returns 0 (length 1)

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

Swift (and a lot more), 8 bytes

93^99<84

output (4 chars):

true

When removing the nth character the output is:

n -> out
----------
0 -> false
1 -> false
2 -> false
3 -> false
4 -> false
5 -> 10077
6 -> false
7 -> false

There are 78 possible solutions like this in the format of a^b<c.

I think the goal of this challenge should be as many bytes as possible, because the more bytes, the more possible bytes to remove and therefore more difficult.

\$\endgroup\$
3
  • \$\begingroup\$ I thought about that, but it would have been to easy for stack-based languages. If this was a code bowling challenge, aditsu would have an infinte score. His code can be repeated over and over again; each copy will do exactly the same. \$\endgroup\$
    – Dennis
    Commented Jun 29, 2015 at 23:41
  • \$\begingroup\$ @Dennis Oh I see, yeah makes sense, but I think bytes are maybe not a very good measure for this task \$\endgroup\$
    – Kametrixom
    Commented Jun 30, 2015 at 1:13
  • \$\begingroup\$ I agree. That's why it's a popularity contest. Most upvoted answer wins. \$\endgroup\$
    – Dennis
    Commented Jun 30, 2015 at 2:05
2
\$\begingroup\$

MathGolf, 2 bytes

♂(

Try it online!

Explanation

♂   Push 10
 (  Decrement TOS by 1

Why does this work?

The regular output is 9. If the ( is removed, the output is 10. If the is removed, the program implicitly pops a 0 from the stack to feed the ( operator, which outputs -1.

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

05AB1E (legacy), 1 byte

Any single byte in 05AB1E (legacy) would work, except for [ (infinite loop).

See here all possible outputs for all possible single-byte 05AB1E programs.

Removing that single byte so an empty program remains will output the content of info.txt to STDOUT instead by default.

Try it online.


05AB1E, 1 byte

?

Try it online.

Unfortunately an empty program in the new version of 05AB1E only outputs a 1-byte newline by default (Try it online), so almost none of the 1-byte characters are possible since they'll have to output nothing. The only possible program that outputs nothing (so also not the implicit newline) is ? as far as I know.

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

Clojure, 11 bytes

(defn x[]1)

At first I thought to just post a single character answer in the REPL like the other languages, e.g.

user=> 1
1

But the problem is that if you remove that character, the REPL doesn't do anything upon the enter keypress. So instead it had to be wrapped with a function as permitted by the question's rules. When you call this function, it returns 1. If you remove the only character in the function,

(defn x[])

the function returns nil which prints an additional two bytes.

user=> (defn x[]1)
#'user/x
user=> (x)
1
user=> (defn y[])
#'user/y
user=> (y)
nil
\$\endgroup\$
1
  • 1
    \$\begingroup\$ To clarify: The contest requires an inner function, which would be 1 in this case (1 byte). \$\endgroup\$
    – Dennis
    Commented Jun 24, 2015 at 16:24
0
\$\begingroup\$

Japt, 2 bytes

Try it online!

A is pre-initialized to 10, É subtracts 1, and the result is 1 byte: 9.

Removing É results in the program A, which outputs A's value, 10, so the result is 2 bytes.

Similarly, removing A results in just É which is interpreted as -1 and output as -1, so the result is 2 bytes.

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

Runic Enchantments, 6 (modifiable) bytes

11-{{B\
/B~~@/
\00<
R"Error"@

Try it online!

Runic does not have a REPL environment and writing a full program to satisfy the constraints is not readily feasible (leading to either invalid programs or no output). So the portion of the code 11-{{B is acting as an inner function and only this portion can be modified. The B instruction is close enough to a function pointer and return statement that this classification should be acceptable (as it jumps the IP to a position in the code as well as pushing a return location to the stack which may be accessed by a subsequent B or discarded).

  • Standard output: 0
  • Removing either of the 1s: 05
  • Removing the -: 11
  • Removing either {: Error051
  • Removing the B: 120

The final \ on the end of the first line provides an external boundary around the inner function in the event that the return instruction is removed so that the IP doesn't go meandering all over everywhere willy nilly (and print no output).

Note that Error051 is just an arbitrary string, I could put anything I wanted into that portion of the code and "Error" was an amusing result for having code that feks up the return coordinates and the IP teleports to an arbitrary location.

\$\endgroup\$

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