# 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

# Vim, 5 bytes

i0<esc><C-a>:


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

# 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!

# Carrot, 4 bytes

2^F*


Outputs 2 normally and 4 when doubled.

Explanation normal:

2 //Input on the stack
^ //Convert to operations mode
F //Convert stack to float mode
* //Multiply stack by an empty argument (don't do anything)
//Implicit output of stack


Explanation doubled:

2  //Input on the stack
^  //Convert to operations mode
F  //Convert stack to float mode
*2 //Multiply stack by 2
^  //Convert to string stack mode
F* //Place the literal "F*" onto the string stack
//Implicit output of float stack


In Carrot there are three stack modes: string, float and array. Only the current stack modes stack is output at the end of the program.

# TI-Basic, 4 bytes

Fairly simple. I'm wondering if there are many alternate solutions.

1:2Ans       --> prints 2
1:2Ans1:2Ans --> prints 4


Alternate solutions

1+0 <-- 3 bytes based on @alexanderbird's answer

• what's wrong with (2 ? – Oki Sep 10 '17 at 18:36
• @Oki Nothing? I just didn't consider that. Mind if I change my answer to add that one? – Timtech Sep 11 '17 at 16:32
• i don't mind. tough your solution is much fancier – Oki Sep 12 '17 at 11:27

# Java, 121 bytes

### Original Code:

class A {static int n=1;public static void main(String[]a){System.out.print(n);}}/*
class B extends A{static{n++;}}//*///


### Duplicated Code:

class A {static int n=1;public static void main(String[]a){System.out.print(n);}}/*
class B extends A{static{n++;}}//*///class A {static int n=1;public static  void main(String[]a){System.out.print(n);}}/*
class B extends A{static{n++;}}//*///


Technically you have to run the main method of A the first time, and of B the second time. Still, it's one program.

• You can save a byte when you remove the whitespace class A { – Edwardth Jul 19 '17 at 14:51

# 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, 4847 43 bytes

<?php ob_end_clean();ob_start();echo++$i;?>  Try it online! Result: 1 There's 2 PHP answers on here already, one of them looks like a port from Python and the other one breaks the error rule imo, so there's my shot at it. Doubled: # PHP, 9695 86 bytes <?php ob_end_clean();ob_start();echo++$i;?><?php ob_end_clean();ob_start();echo++$i;?>  Try it online! Result: 2 Explanation: ob_end_clean() once called, turns off the ob_start(). However, ob_end_clean will only clean items that started within the ob_start(). So to counter act this, we clear it first, run ob_start() then execute our counter. • Welcome to PPCG! I believe you can remove some newlines (after the semicolons) to save some bytes. – Stephen Aug 8 '17 at 13:14 • Good spot, thank you :) And thanks for the welcoming! – IsThisJavascript Aug 8 '17 at 13:23 # k, 2 bytes *2  Try it online! A "monadic" (one argument) * means "first", and a dyadic (two argument) * is multiplication. Therefore *2 is seen as "the first element of a list containing 2", which is 2. *2*2 first evaluates 2*2, which is 4, and then evaluates *4, which is 4. ## Perl, 15 bytes ++$_,exit print


Try it online!

Doubled:

++$_,exit print++$_,exit print


Try it online!

Increments $_ to 1 in the first instance, print outputs $_ by default, and we pass that as an argument to exit. When doubled, print explicitly outputs ++$_ which is now 2. # Carrot, 5 bytes ^F+1  Explanation: ^ --- Stack mode F --- Convert to float +1 --- Add 1 to the previous result <space> -- I have no idea  # Brian & Chuck, 15 bytes >>.>?2<<<?1 !>.  >>.>?2<<<?1 !>.>>.>?2<<<?1 !>.  Try it twice! This was my first attempt at learning Brian & Chuck. I took a short break after writing this answer and I've forgotten most of how it works. It relies on the facts that: • Chuck's 4th value is zero in the single program and non-zero in the double • . is a no-op in Brian's instructions, but prints in Chuck's instructions • In the double program, Brian's instructions are appended to Chuck's, but Chuck's are ignored because they're on the 3rd line. # Ly, 6 bytes 1&+s>l  Try it online! breakdown Command Operation stack content cell 1 push 1 1 &+ push sum of the stack 1,1 s pop to backup cell 1 1 > next stack 1 l push backup cell 1 1 implicit output of stack - or second execution: 1 push 1 1,1 1 &+ push sum of the stack 1,1,2 1 s pop to backup cell 1,1 2 > next stack 2 l push backup cell 2 2 implicit output  # Google Sheets / Excel, 2 Bytes Anonymous Worksheet function that returns 7 when single and 14 when doubled. +7  Or more accurately +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  # Cubix, 4 5 bytes Of course as soon as I look at it again this presents itself. Not as nice as the previous, but shorter. ))O@  Try it online!  ) ) O @ . .  Increment, output and exit. ))O@))O@  Try it online!  ) ) O @ ) ) O @ . . . . . . . . . . . . . . . .  Increment twice, output and exit # TI-BASIC, 2 bytes 2!  There are also 5 more solutions, of equal or greater length. (2 abs(2 int(2 iPart(2 round(2  These all work on the same principle. Something evaluates to 2, and twice it is merely 2*2. • You should consider changing your formatting to have your solution in a separate code block prior to your outputs for both the singlet and doublet forms – Taylor Scott Sep 10 '17 at 19:13 # Bash + coreutils, 43 bytes x=$(history|cut -c 8-|tail -1);echo ${#x};#  # Explanation Outputs the length of the last command entered. Uses the same concept as the Python and PHP solutions to prevent execution of any further copies (finishes with a comment marker, so anything after it is ignored). Length of the command will be doubled when it is written twice, hence the output. • Can't you simplify this to echo${#0};#? – Neil Jul 15 '17 at 19:12
• i dont know about the above comment, but tail -n 1 can be shortened to tail -1 – phil294 Jul 15 '17 at 20:00
• @Neil I tried that but echo ${#0};# outputs 4 and echo${#0};#echo ${#0};# also outputs 4 – Darren H Jul 15 '17 at 20:44 • @Blauhirn nicely spotted, thank you – Darren H Jul 15 '17 at 20:44 • @Neil That would output the filename next to the count. You need to use a pipe like wc -c <$0 # – Richard Jul 17 '17 at 14:35

# Wumpus, 6 bytes

$@1$O+


Try it online!

Doubled:

$@1$O+$@1$O+


Try it online!

The first program pushes 1, adds the top two items on the stack (the 1 and an implicit 0), reflects off the end of the line, outputs and exits.

The second program does the same, except it adds two 1s to the stack, meaning it prints 2 and exits. This works for any number from 1-9.

A shorter solution may exist that takes advantage of two lines making the pointer bounce differently.

# ///, 20 bytes

Prints 1:

/\\1\//\/1\/2\/\//\1


Try it online!

Duplicated prints 2:

/\\1\//\/1\/2\/\//\1/\\1\//\/1\/2\/\//\1


Try it online!

# How it works

• The initial substitution /\\1\//\/1\/2\/\// (in both versions) searches for the string \1/ in the remainder and replaces it by /1/2//.
• In the single program there is no such string, and nothing is replaced. The program is now reduced to the final \1, which prints a 1.
• In the duplicated program \1/ crosses the boundary between the copies.
• After substituting, the remaining program becomes /1/2//\\1\//\/1\/2\/\//\1, which is the substitution /1/2/ followed by the single program.
• This substitution then replaces every 1 in the single program by 2, giving /\\2\//\/2\/2\/\//\2.
• This then runs pretty much like the single program does, except for printing 2 instead of 1.
• The normally redundant \ before the final 1, and the corresponding \\ in the initial substitution, are needed because without them, the substitution would be applied again to the /1/2/ result, causing an infinite loop.
• Was just thinking about doing a /// submission, but it looks like you beat me to it! – Esolanging Fruit Feb 12 '18 at 4:17

# Momema, 12 bytes

00 1+1*1-8*1


Try it online! This outputs 1.

Try it doubled! This outputs 02.

## Explanation

The ungolfed form of the singular program 00 1+1*1-8*1 is

0   0     # set the 0th cell to 0 (this has no effect).
1   +1*1  # set the 1st cell (initialized to 0) to itself plus one (i.e. 1).
-8  *1    # output the value of the first cell as a decimal (1).


The ungolfed version of the doubled program 00 1+1*1-8*100 1+1*1-8*1 is

0   0     # set the 0th cell to 0.
1   +1*1  # increment the 1st cell.
-8  *100  # output the value of the 100th cell (0).
1   +1*1  # increment the 1st cell.
-8  *1    # output the value of the 1st cell (2).


This submission hinges on Momema's syntax: in particular, it allows leading 0s in a numeric literal to be parsed as separate numbers. This allows the leading 00 in the program to be parsed as a pointless assignment statement.

When the program is doubled, however, the 0s are no longer leading a numeric literal—they are a continuation of the literal 1 at the end of the program, forming 100.

# Labyrinth, 8 5 bytes

-3 bytes thanks to @MartinEnder

Single version

^)!@



Try it online!

Double version

^)!@
^)!@



Try it online!

• Nice work, this is a really neat solution! :) I got curious and set a brute force solution on this problem and it found a whole bunch of 5-byte solutions (all of which print 1 and 2, some of them with leading zeros). Feel free to add any of them to your answer pastebin.com/RRFxZEuN (I don't care about posting them myself, since you clearly put more effort into your 8-byte solution than I did into just brute forcing the optimal ones.) – Martin Ender Feb 18 '18 at 16:55

# bash builtins only - 21 bytes

The X file:

trap "echo $[++i]" 0;  EXIT –> 0 $((...)) -> $[...] ;-) # bash builtins only - 26 bytes Straight forward while using only bash builtins. IOW: No external helpers. The X file: trap "echo$((++i))" exit;


(No final newline.)

Proof of the pudding:

$bash <(cat X) 1$ bash <(cat X X)
2
$bash <(cat X X X) 3  Concat with or without newline inbetween: $ cat X X ; echo # added echo compensates missing \n only
trap "echo $((++i))" exit;trap "echo$((++i))" exit;
$bash <(cat X X) 2$ cat X <(echo) X ; echo
trap "echo $((++i))" exit; trap "echo$((++i))" exit;
$bash <(cat X <(echo) X) 2  # Ruby, 10 bytes +1;a||=p 1  Try it online! Try it online!Try it online! Explanation: • Single version (+1;a||=p 1): • +1 is ignored • a is assigned to 1 (printing 1 in the process) because a was previously undefined • Double version (+1;a||=p 1+1;a||=p 1) • +1 is ignored • a is set to 1+1 (2) (printing 2 in the process) because a was undefined • The next part is not executed because a is already defined # CJam, 4 bytes 1]:+  Try it online! Explanation: 1]:+ 1 push 1 to stack ]:+ sum stack  # CJam, 1 byte )  Try it online! I'd say this uses 2 bytes since there is a 0 in the header. I don't know if this is a valid loophole or forbidden. • The second one isn't valid since it doesn't work without the 0 – Jo King Jan 19 '19 at 4:16 # K (ngn/k), 3 bytes +/1  Try it online! +/1 - sum 1. returns 1 +/1+/1 - rightmost +/1 evaluates to 1 (as above), giving +/1 1, i.e. sum the vector 1 1. returns 2 # GAS x64 (Linux, GCC 9.2): 143 bytes Single one segfaults, effectively printing a 1 to the terminal with 1 \b where \b is the backspace character. mov$a,%rsi
mov $3,%dx mov$1,%di
mov $1,%ax syscall .ifndef b jmp b c=. .else b=c .endif mov$60,%ax
syscall
a=.
.ascii "1 \b\b2\n"
a=a+3
b=.


1[1]    74584 segmentation fault (core dumped)  ./a.out
^ -- this one


Double one takes advantage of the previous backspace, inserting another one and overwriting it with a 2 and a line feed in the terminal. Perhaps a byte could be saved if a space was used instead of a line feed, but that output didn't seem as clean.

mov $a,%rsi mov$3,%dx
mov $1,%di mov$1,%ax
syscall
.ifndef b
jmp b
c=.
.else
b=c
.endif
mov $60,%ax syscall a=. .ascii "1 \b\b2\n" a=a+3 b=. mov$a,%rsi
mov $3,%dx mov$1,%di
mov $1,%ax syscall .ifndef b jmp b c=. .else b=c .endif mov$60,%ax
syscall
a=.
.ascii "1 \b\b2\n"
a=a+3
b=.


2


# Pip, 2 bytes

+1


Try it online!

Try the double version!

# Ohm, 2 bytes

1Σ


Try it online!

Try the double version!

aaaaaaaaaaaaaaa i can't find a language where this is one byte aaaaaaaaaaaaaaa

• What does the sigma do? – Cyoce Jul 17 '17 at 4:36
• It sums an array or the stack depending on what type the TOS is – Roman Gräf Jul 17 '17 at 11:59

# LOGO, 16 bytes

 ct pr bf gensym


output 1.

When doubled, it becomes

 ct pr bf gensym ct pr bf gensym


output 2.

Explanation:

• ClearText clears all the output.
• Print just print whatever it is given.
• ButFirst return the input with the first item removed.
• GenSym (perhaps GenerateSymbol) return g1 the first time it is invoked, g2 the second time, etc.

Because the output of GenSym depends on previous outputs, the interpreter should be reset between runs.

# JavaScript (Node.js), 60 bytes

console.log(require('fs').readFileSync(__filename).length)//


Inspired by this Python 2 answer, this reads the length of the current file and prints it, preventing any duplicates of this code from being executed with the trailing comment.

Try it online!

Try it online doubled!