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. Jul 15 '17 at 17:55
• @Mr.Xcoder I think that you should have prevented reading your own source code. Jul 15 '17 at 18:09
• It only needs to work when doubled once? We don't need to support n many doublings? 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. Jul 17 '17 at 9:11
• This shouldn't be hard for esolangs that automatically dump the top of stack upon program termination. Jul 18 '17 at 22:26

QBasic,  44  28 bytes

There is no newline at the end. Outputs 4 when single, 8 when doubled.

4
?x+y
END
DATA 4,0


Explanation

For the single version:

• 4 is a line number.
• READ x,y takes the first two values from the DATA statement and stores them in x and y. Thus, x gets 4 and y gets 0.
• ?x+y adds the two numbers and prints them.
• END exits the program.

In the doubled version, the DATA statement becomes DATA 4,04, which assigns 4 to both x and y, thus making x+y equal 8 instead.

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.

Perl 5, 7 bytes

With -M5.10.0

say
+1#


Try it online!

say
+1#say
+1


-2 thanks to Ton Hospel

• Use say to gain 2 more bytes (The -M5.10.0 or -E instead of -e is free) Feb 17 '18 at 23:19

SHENZHEN I/O Assembly, 35 bytes, 3+4¥, 4 Power

@add 7
+mov acc x1
+slx x1
teq 1 1



Makes use of some cool SHENZHEN I/O Assembly features, like looping execution (chips loop forever and have to be instructed to sleep), persistent conditionals (conditional branches can be anywhere, based on previous test's outcome), and execute once command (@).

Brief step-through, if you have not played the game (if you browse this forum, you would probably enjoy it.)

Singled:

@ add 7      # Execute once, acc = 7
+ mov acc x1 # Skipped (no test passed)
+ slx x1     # Skipped (no test passed)
teq 1 1    # Test = true
+ mov acc x1 # Output 7
+ slx x1     # Halt

Doubled:

@ add 7      # Execute once, acc = 7
+ mov acc x1 # Skipped (no test passed)
+ slx x1     # Skipped (no test passed)
teq 1 1    # Test = true
@ add 7      # acc = 14
+ mov acc x1 # Output 14
+ slx x1     # Halt
teq 1 1

• Ooh, nice to see Shenzhen I/O here. Mar 4 at 5:19

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? Jul 15 '17 at 16:57
• @Mr.Xcoder after your comment I have saw the python solution. It is the same way Jul 15 '17 at 17:07
• Why not <?=strlen(file(__FILE__)[0]);# Jul 15 '17 at 17:34
• @DarrenH I have not thinking about it Jul 15 '17 at 17:45
• this is 23 bytes: <?=filesize(__FILE__);# - and doubled is 46 bytes. Jul 23 '17 at 13:27

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. Jul 17 '17 at 17:05
• Even if I make as REPL? 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. Jul 18 '17 at 21:31
• Marked as REPL. Jul 19 '17 at 10:36

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 Jul 15 '17 at 21:30
• @Mr.Xcoder To be fair it works in pretty much every non-esoteric language with a REPL. Jul 16 '17 at 22:54
• I thought J's operator precedence was right to left? e.g. 3*6+5 = 33 Jul 21 '17 at 18:38
• @RichardDonovan right. I goofed Jul 22 '17 at 0:37

Whispers v2, 23 bytes

+1
> 1
>> Output 3
>> 1


Outputs 1 normally and 2 when doubled.

How it works

Normal

Whispers only recognises lines that match one of a certain set of regexes. In the normal version, this reduces the program down to

> 1
>> Output 3


(i.e. the first and last lines don't match any regexes). The program then runs the last line, >> Output 3. This calls the 3rd line and outputs the result. However, as the reduced program only has 2 lines, this indexes modularly, returning the $$\3 \mod 2\$$th line (i.e. the first line), > 1. This is a basic nilad line which returns $$\1\$$, which is then outputted.

Doubled

The doubled program is

+1
> 1
>> Output 3
>> 1+1
> 1
>> Output 3
>> 1


Removing lines which don't match any regex, we get

> 1
>> Output 3
>> 1+1
> 1
>> Output 3


Again, the last line is run, telling the program to output the result of line 3. This time however, we do have a line 3 to output: >> 1+1. This does return $$\2\$$ but not because it evaluates $$\1+1\$$. Rather, it adds the value of line 1 to itself, which happens to be $$\1\$$.

Interestingly, this means that by changing > 1 to > n where $$\n\$$ is any number, the programs output $$\n\$$ and $$\2n\$$. For example

+1
> π
>> Output 3
>> 1


outputs $$\\pi\$$ normally or $$\2\pi\$$ when doubled

Google Sheets / Excel, 2 Bytes

The Trival Solution

+7


Or

+n


Such that n is an integer between 1 and 9, inclusive.

Google Sheets and Excel both will take a leading + or - and evaluate them down to = or =-, respectively if they are the leading character of a cells formula text. As a result, -n, such that n is an integer between 1 and 9, inclusive, is a valid negative equivalent of the above

As a single formula this evaluates to a call stack that looks a little something like

+7
=7
7


However when this worksheet formula is doubled this call stack evaluates down to

+7+7
=7+7
=14


Pyth, 3 bytes

s[1


sums a list

Try it!

Double-try it

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"! Jul 16 '17 at 5:43

Self-modifying Brainfuck, 7 bytes

<-<201/


or doubled:

<-<201/<-<201/


Outputs 1 and 2 respectively.

The idea is simple: we use Self-modifying Brainfuck's self-modifying capability to make sure that the printing command only happens at the end of the full program.

Try it online!

Try it online, doubled!

Little Man Computer, 20 bytes (source)

LDA 1
OUT
HLT



Online Emulator (Flash)

• Wowee never thought i'd have to see LMC again after I finished my a levels ... lovely little answer Jul 19 '17 at 11:09

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. 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. 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" ^^ ) 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.
– user19214
Mar 16 '18 at 4:03
• This for 10 bytes. Apr 3 at 18:59

Python 2, 24 bytes

'+1#';exec'print 1''' ''


Abusing string syntax.

Powershell, 2 bytes

+1


It's a full Powershell program. The output has printed to STDOUT.

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.

Python 2, 27 bytes

+7
try:_
except:_=7;print _


Try it online!

Try it doubled!

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 :) Aug 30 '17 at 2:10
• @MickyT you should post that as your own answer! That's a super cool one :) Dec 15 '17 at 19:54
• Done, but found another byte which made it a bit more boring Dec 17 '17 at 17:47

R, 2 bytes

+1


prints 1 and when doubled

+1+1


prints 2

• or +T or +2 or... Jul 17 '17 at 19:21
• This is exactly the same as the J answer. 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. Jul 17 '17 at 21:08
• My bad. I haven't been actively recently and missed that. Jul 17 '17 at 21:10
• +2 is a byte shorter. Jul 19 '17 at 4:38

R, 2 bytes

+T


Never expected to codegolf this much in R. Due to the +, R changes the T for a numeric, using 1 as default.

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 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!! 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? 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. :) 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 Jul 12 '18 at 12:50