# I double the source, you double the output!

Your task, if you wish to accept it, is to write a program that outputs a positive integer (higher than 0). The tricky part is that if I duplicate your source code, the output must be double the original integer.

# Rules

• You must build a full program. That is, your output has to be printed to STDOUT.

• The initial source must be at least 1 byte long.

• Both the integers must be in base 10 (outputting them in any other base or with scientific notation is forbidden).

• Your program must not take input (or have an unused, empty input).

• Outputting the integers with trailing / leading spaces is allowed.

• You may not assume a newline between copies of your source.

• This is , so the shortest (original) code in each language wins!

• Default Loopholes apply.

### Example

Let's say your source code is ABC and its corresponding output is 4. If I write ABCABC instead and run it, the output must be 8.

This uses uses @manatwork's layout.

/* Configuration */

var QUESTION_ID = 132558; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8349457; // This should be the user ID of the challenge author.

/* App */

return "https://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

}

jQuery.ajax({
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(a) {
});
comment_page = 1;
}
});
}

jQuery.ajax({
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(c) {
if (c.owner.user_id === OVERRIDE_USER)
});
else process();
}
});
}

var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(-?\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;

function getAuthorName(a) {
return a.owner.display_name;
}

function process() {
var valid = [];

var body = a.body;
if(OVERRIDE_REG.test(c.body))
body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
});

var match = body.match(SCORE_REG);
if (match)
valid.push({
user: getAuthorName(a),
size: +match[2],
language: match[1],
});

});

valid.sort(function (a, b) {
var aB = a.size,
bB = b.size;
return aB - bB
});

var languages = {};
var place = 1;
var lastSize = null;
var lastPlace = 1;
valid.forEach(function (a) {
if (a.size != lastSize)
lastPlace = place;
lastSize = a.size;
++place;

.replace("{{NAME}}", a.user)
.replace("{{LANGUAGE}}", a.language)
.replace("{{SIZE}}", a.size)

var lang = a.language;
if (! /<a/.test(lang)) lang = '<i>' + lang + '</i>';
lang = jQuery(lang).text().toLowerCase();

languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link, uniq: lang};
});

var langs = [];
for (var lang in languages)
if (languages.hasOwnProperty(lang))
langs.push(languages[lang]);

langs.sort(function (a, b) {
if (a.uniq > b.uniq) return 1;
if (a.uniq < b.uniq) return -1;
return 0;
});

for (var i = 0; i < langs.length; ++i)
{
var language = jQuery("#language-template").html();
var lang = langs[i];
language = language.replace("{{LANGUAGE}}", lang.lang)
.replace("{{NAME}}", lang.user)
.replace("{{SIZE}}", lang.size)
language = jQuery(language);
jQuery("#languages").append(language);
}

}
body { text-align: left !important}

width: 290px;
float: left;
}

#language-list {
width: 290px;
float: left;
}

font-weight: bold;
}

table td {
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>

</tbody>
</table>
</div>
<div id="language-list">
<h2>Winners by Language</h2>
<table class="language-list">
<tr><td>Language</td><td>User</td><td>Score</td></tr>
<tbody id="languages">

</tbody>
</table>
</div>
<table style="display: none">
</tbody>
</table>
<table style="display: none">
<tbody id="language-template">
</tbody>
</table>

• @Mr.Xcoder Then I'll just have to include one in my own source. – steenbergh Jul 15 '17 at 17:55
• @Mr.Xcoder I think that you should have prevented reading your own source code. – caird coinheringaahing Jul 15 '17 at 18:09
• It only needs to work when doubled once? We don't need to support n many doublings? – Cody Gray Jul 16 '17 at 8:36
• @Daniel Let's say your source is  (empty program) and it produces 5. If you double it, your source is  (empty program) and that produces 5 as well, no matter what you do. That being said, an empty program duplicated is still the empty program, and always produces the same output, except for the case where the empty program means something else (a random number generator, for example), which could not be valid anyway. – Mr. Xcoder Jul 17 '17 at 9:11
• This shouldn't be hard for esolangs that automatically dump the top of stack upon program termination. – MD XF Jul 18 '17 at 22:26

## JavaScript (ES6), 63 bytes

Prints either 1 or 2 through an alert dialog.

clearTimeout((t=this).x),t.x=setTimeout(alert(${t.n=-~t.n})); ### Doubled clearTimeout((t=this).x),t.x=setTimeout(alert(${t.n=-~t.n}));clearTimeout((t=this).x),t.x=setTimeout(alert(${t.n=-~t.n})); • I guess this would work also: window.i=++window.i||1; in the browser console. It ouputs 1. Browser refresh, window.i=++window.i||1;window.i=++window.i||1; ouputs 2. – Christiaan Westerbeek Jul 17 '17 at 13:13 • @ChristiaanWesterbeek True. But then it's a REPL answer (and you can actually just do this.i=++this.i||1;). – Arnauld Jul 17 '17 at 13:23 • I don't know what that means, REPL answer – Christiaan Westerbeek Jul 17 '17 at 13:26 • @ChristiaanWesterbeek REPL stands for Read-Eval-Print Loop. In that case, the final result is not explicitly printed by the code but by the shell it's running in (like the browser console). – Arnauld Jul 17 '17 at 13:31 • @ChristiaanWesterbeek (Actually, just +1 would work in REPL -- like this Python REPL answer does.) – Arnauld Jul 17 '17 at 13:33 # PHP, 30 bytes Original <?=strlen(file(__FILE__)[0]);#  Try it online! # PHP, 60 bytes doubled <?=strlen(file(__FILE__)[0]);#<?=strlen(file(__FILE__)[0]);#  Try it online! • Port of the Python solution? – Mr. Xcoder Jul 15 '17 at 16:57 • @Mr.Xcoder after your comment I have saw the python solution. It is the same way – Jörg Hülsermann Jul 15 '17 at 17:07 • Why not <?=strlen(file(__FILE__)[0]);# – Darren H Jul 15 '17 at 17:34 • @DarrenH I have not thinking about it – Jörg Hülsermann Jul 15 '17 at 17:45 • this is 23 bytes: <?=filesize(__FILE__);# - and doubled is 46 bytes. – hanshenrik Jul 23 '17 at 13:27 # J, 2 bytes +1  +1 is 1, and +1+1 is 2. Fun fact: since J's operator precedence is just right-to-left, this is interpreted as +(1+1) • This is a real Polyglot: Python REPL, APL, J, Pip – Mr. Xcoder Jul 15 '17 at 21:30 • @Mr.Xcoder To be fair it works in pretty much every non-esoteric language with a REPL. – Cyoce Jul 16 '17 at 22:54 • I thought J's operator precedence was right to left? e.g. 3*6+5 = 33 – Richard Donovan Jul 21 '17 at 18:38 • @RichardDonovan right. I goofed – Cyoce Jul 22 '17 at 0:37 # Befunge-98 (PyFunge), 3 bytes This answer draws heavy inspiration from this one, go vote them up too! ".@  ## Explanation When wrapping back around to the beginning of the source in quote mode, this Befunge-98 interpreter pushes at least 1 space (ASCII 32). If we print this value and exit, we get 32. If we double the source code, we don't wrap back around, but rather end quote mode with the duplicated ". This means that the last character pushed is not a space, but a @, with (drumroll) ASCII 64! A complete coincidence that the character that ends the program in Befunge is twice that of a space. # Pyth, 3 bytes s[1  sums a list Try it! Double-try it # SOGL V0.12, 1 byte I  Try it Here!, or try the duplicated version I is the increase command, and as no input is provided, it increases 0 (and then in the duplicated program, 1 to 2) • Can you provide a working link for me to test it on my mac as well (the classic problem)? EDIT: Thanks. – Mr. Xcoder Jul 15 '17 at 15:19 # Charcoal, 7 bytes ＰＩＬ⊞Ｏυω  Try it online! Appends the empty string to the empty list and prints the length of the result (1) in decimal without moving the cursor. When doubled, the empty string gets appended twice so the length is now 2, which then overwrites the 1. Try it online! In verbose syntax, this is Multiprint(Cast(Length(PushOperator(u, w))));. • When I look at the code, it looks like it says "pillow"! – Cort Ammon Jul 16 '17 at 5:43 # Hexagony, 4 bytes [@!)  This solution was found using brute force by Martin and prints 002 regularly and 4 when doubled. This is the only 4 byte solution, but there are several hundred 5 byte solutions. ### Expanded versions: Regular:  [ @ ! ) . . .  Doubled:  [ @ ! ) [ @ ! ) . . . . . . . . . . .  Hexagony has 6 instruction pointers, one starting at each corner of the hexagon and initially moving "clockwise." The [ instruction causes the interpreter to change IP one left. The first program leads us on a bit of a merry go around, executing: [![.!.)[...)!@ which results in the 002 output. The other program is a bit more tame, executing: [))[..)...[.......)!@ which results in the clean 4. # Python, 15 bytes 1or exit(2) 0/0  Outputs via exit code. Divides by zero and exits with code 1. Doubled: 1or exit(2) 0/01or exit(2) 0/0  Calls exit(2) and exits with code 2. # Python?, 9 bytes ';exit(2)  Throws a syntax error and exits with code 1. Doubled: ';exit(2)';exit(2)  Calls exit(2) and exits with code 2. # JavaScript (REPL), 2 bytes So long as putting code in the console counts as a full program: +1  • Putting the code in the console does not count as a full program. This is a snippet and therefore, unfortunatly, invalid. – Pavel Jul 17 '17 at 17:05 • Even if I make as REPL? – Jake Taylor Jul 18 '17 at 9:19 • I'd say mark it as REPL and run with it. People who have sour grapes over "JavaScript REPL" but not over the various golfing languages which get such things down to one byte are being oddly specific with their moralistic tendencies. The interpreter which runs JS REPL code is a lot more straightforward than, say, Bubblegum. – CR Drost Jul 18 '17 at 21:31 • Marked as REPL. – Jake Taylor Jul 19 '17 at 10:36 # Little Man Computer, 20 bytes (source) LDA 1 ADD 5 OUT HLT  Online Emulator (Flash) • Wowee never thought i'd have to see LMC again after I finished my a levels ... lovely little answer – space junk Jul 19 '17 at 11:09 # Python 2, 24 bytes '+1#';exec'print 1''' ''  Abusing string syntax. # V, 4 bytes øß.  Try it online! # Cubix, 6 bytes @1u.2O  When run, this pushes 1, u-turns, Outputs as a number, and h@lts:  @ 1 u . 2 O  When doubled, the cube is  @ 1 u . 2 O @ 1 u . 2 O . . . . . . . . . . . .  which simply pushes 2, Outputs as a number, and h@lts. Try it online! • While looking at the duplicate question for a bit of fun I came up with this ^5O@N. Also has the twist of printing out it's length :) – MickyT Aug 30 '17 at 2:10 • @MickyT you should post that as your own answer! That's a super cool one :) – Giuseppe Dec 15 '17 at 19:54 • Done, but found another byte which made it a bit more boring – MickyT Dec 17 '17 at 17:47 # R, 2 bytes +1  prints 1 and when doubled +1+1  prints 2 • or +T or +2 or... – Giuseppe Jul 17 '17 at 19:21 • This is exactly the same as the J answer. – Zacharý Aug 23 '17 at 23:31 # Fourier, 3 bytes @^o  Try it Online! Try it Doubled! Explanation: Undoubled: @ - Clear screen ^ - Increment the accumulator (initialised to zero) o - Output the value of the accumulator  Doubled: @ - Clear screen ^ - Increment the accumulator (initialised to zero) o - Output the value of the accumulator @ - Clear screen ^ - Increment the accumulator o - Output the value of the accumulator  # Ruby (REPL), 3 2 bytes Must be run interactively for the implicit output: +2  Outputs 2. Doubled: +2+2  Outputs 4. 2 and 4 are more interesting than 1 and 2 • @MorganThrapp REPL answers are allowed, as long as they're clearly labeled as such. – Dennis Jul 17 '17 at 21:08 • My bad. I haven't been actively recently and missed that. – Morgan Thrapp Jul 17 '17 at 21:10 • +2 is a byte shorter. – m-chrzan Jul 19 '17 at 4:38 # PHP, 26 bytes <?=ob_clean()+ob_start()?>  Try it online! Try it doubled! Explanation PHP can use an output buffer. This buffer has the facility to be cleaned without its contents ever being sent. When the script is run just once, the output buffer has not initially been started and so cannot be cleaned, and ob_clean() returns false. The buffer is then successfully started and ob_start() returns true. false + true is really 0 + 1, and so the script echoes 1 into the buffer and ends with the buffer being outputted. If the script is doubled then this first 1 is not output but is still in the buffer when the second half of the doubled code starts. The output buffer is now cleaned, the 1 is lost, and ob_clean() this time returns true. Another output buffer is then started (they are, in fact, stacked) and ob_start() again returns true, so this time we have true + true, which is 1 + 1, and so 2 is sent to the output buffer and then on to the actual output when the script ends. # PHP, 16 bytes Does not actually satisfy the rules because of PHP warnings. ob_start(bcadd);  Test it online. • Not really 16 bytes as you need to turn errors off to pass OP's rules – IsThisJavascript Aug 8 '17 at 13:00 • And if you include the tags, opening and closing because you cannot double the source without the closing tag, it comes in at 61 bytes. sandbox.onlinephpfunctions.com/code/… ! Quite far from 16!! – IsThisJavascript Aug 8 '17 at 13:02 • The OP’s rules actually say that warnings are OK, BUT, strangely, and I don't understand what's going on here, if the warnings are not turned off then the single code and the doubled code both return 9, and so the warnings are an essential part of your code. The shortest I can get your script down to is 56 bytes (sandbox.onlinephpfunctions.com/code/…), 40 more than the 16 you are falsely claiming. How about changing your answer? – WebSmithery Aug 13 '17 at 15:20 # C++, 158138 123 bytes 20 byte savings thanks to @zbw I'm sure there's a more efficient way to do it in C++ but this is all I could think of. #include <iostream> #ifdef m #define m a r; #else int v; #define m struct a{a(){++v;}}q;int main(){std::cout<<v;} #endif m  How it works: On the first instance, it declares a global int v initialized to 0 and a struct a which increments every time an a is instanced. It instances an a, and prints the value of the member. On the second instance of the source, it just instances another a. • You could change a to a struct, where it'll default to public, saving 6 bytes (explanation here). Also, remove the unneeded #undef, for 9 bytes, and reorder the cases in the #ifndef so it becomes #ifdef for another. You can remove the int  before main. Try it online! – zbw Jul 20 '17 at 15:47 • @zbw Good catches! Thanks. (Can't believe I forgot the struct thing in particular.) The #undef was to avoid a compiler warning but I guess that's not an issue for code golf. :) – fluffy Jul 21 '17 at 15:49 # Proton, 6353 52 bytes +(x? !print(x=2):0) while(x? x-2? print(1):0:(x=1))x  Prints 1. Try it online! Doubled: +(x? !print(x=2):0) while(x? x-2? 0:print(1):(x=1))x+(x? !print(x=2):0) while(x? x-2? 0:print(1):(x=1))x  Prints 2. Try it online! • Removing the spaces seems to give the desired results – Mr. Xcoder Jul 12 '18 at 12:50 # Python 3, 33 bytes Cheating a little: id=print('\r',1+(not id),end='');  • On a full console, you should only see 1 or 2; or did you mean that the whole output stream contains more than the number itself? (I'm not sure how this behaves in Windows, but on Debian or a Mac, this looks like a single output). – lungj Jul 22 '17 at 19:38 • Oh, yeah, I did a test in TIO before and saw that it didn't work there. In a console, the \r gives a carriage return, but not a newline. Thus, when the second copy of the code is run, it actually starts overwriting the content from the first copy of the code. – lungj Jul 22 '17 at 19:43 # bash, 16 bytes a simple one: wc -c<$0;exit 0;


First program: Try it online!

Second program: Try it online!

• Thanks @Step-hen for the 2 TIO, I tried to use the site myself while writing the answer, but nothing happened when I pressed the run button (no output on my own attempts to set it up... weird as it was identical to yours...). Thanks for putting those 2 working links. – Olivier Dulac Jul 19 '17 at 15:31
• No problem, I believe something is going wonky with TIO's backend right now (Dennis was talking about DOS attempts) so it's working on and off, sometimes it acts like it is infinitely looping. – Stephen Jul 19 '17 at 15:33
• @StepHen: this looks like what was happening to me indeed (play button was looping). Thanks fo the info. Maybe not a DOS but just someone putting a script there that requires too much processing power? (Hanlon's razor : "Never attribute to malice that which is adequately explained by stupidity" ^^ ) – Olivier Dulac Jul 19 '17 at 15:34
• wc is not part of bash, so should be mentioned in the title line. And I see no bashism so think, this will run on sh+wc too. – yeti Mar 16 '18 at 4:03

## Rust, 62 bytes

fn main(){let a=include_str!("a.");print!("{}",a.len()-1)}//


What's happening is the file is including itself as a string during compiletime, and getting the length of itself.

When appended, the extra code is commented out, but still effects the length of the file.

Only works if the file name is 'a.'

## Secondary Solution, 63 bytes

fn main(){let a=include_str!(file!());print!("{}",a.len()-1)}//


This solution doesn't use a hardcoded filename, losing 1 byte.

• If the file name is needed, then you have to add the file name to the bytecount. Meta post – Jo King Feb 11 '18 at 22:01
• Done. I still gain a byte. ^_^ (I'd remove the dot, but otherwise rustc complains) – moonheart08 Feb 12 '18 at 2:28

-2 thanks to Martin Ender

-5 thanks to Ørjan Johansen

(1)\r2)S(


The \r in the code should be a literal carriage return character.

There's no way for spec-complaint Underload to actually solve this challenge. This submission uses several tricks to do it:

• stringie, the TIO interpreter, ignores unmatched close brackets and segfaults on unmatched open brackets, which is allowed.
• Using \r allows us to overwrite whatever has already been output with new output. This requires a terminal for which \r clears the line, rather than just allowing it to be overwritten with new text.

# Implicit, 2 1 byte

-1 thanks to ASCII-only

.


:)

• Link's broken for me – Aidan F. Pierce Mar 27 '18 at 4:17
• @AidanF.Pierce fixed. – MD XF Mar 27 '18 at 18:32
• . seems to work? – ASCII-only May 14 '18 at 10:35

# Brachylog, 8 bytes

w₆-₇ℕ~l"


Although Brachylog has implicit function output, full programs won't automatically print without being given arguments, making this challenge a bit more interesting.

w₆          Declaratively write a number
-₇        which minus 7
ℕ       is a whole number (so length won't throw an error)
~l     equal to the length of
"    an (implicitly terminated) empty string.


Doubling the code turns the empty string into a string of length 7, changing the printed output from 7 to 14.

## Keg, 2 bytes

This indeed works. (Works in the old TIO version, I don't know which version is this)

1+


## A version that I can understand, 4 bytes

1(+)


Explanation:

1#    Push 1 onto the stack
(+)# Add the whole stack together

• This probably has something to do with why it works. I can't quite tell what the stack is doing, though, because TIO seems to be running an older and significantly different version of Keg.py where line 67 is return self.content.pop(). – Unrelated String Sep 13 at 3:08
• @UnrelatedString, I haven't asked Dennis to update Keg on TIO yet because we're still testing to see that I haven't broken everything by implementing functions – Jono 2906 Sep 13 at 22:46

# ><>, 24 20 bytes

\0r+n;
"

"
0
8
p
1



Try it online!

Uses the same trickery as my original 24 byte answer, just outputs 1 and 2 instead of 2/4.

\0r+n;
"

"
0
a
p
1
1
+


Try it online!

And try the double!

This starts at the \, diverts code execution down, does its trick*, pushes two 1's, then wraps around again. It hits the \ again, but from a different angle now, so the 0r+n gets executed and ; terminates the program.

(*) Note that the trick is to alter the source code by turning the instruction at 0, 10 into a space instead of a \ when the code is doubled.

When the code hits n; (print number and quit), the top of the stack is either 2 or 2, 2. In each case, a 2 gets printed, So we need to add 2 and 2 in case of the double source code, but we don't have a second stack item on the regular run. This is solved by 0r: push a zero and reverse stack. We now either sum 2 and 2 and ignore the (now bottom) 0, or we add 2 and 0.

# Vim, 5 bytes

i0<esc><C-a>:


Since V is backwards compatible, you can Try it online! or Try it doubled!