# 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 {
}
<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

Try it online!

Try it doubled

# Python 3, 28 bytes

print(len(*open(__file__)))#

Try it online!

Try it doubled

## Explanation

This opens up the source code using open(__file__) and gets its length using len the # prevents any additional code from being read. When the source is doubled so is the length.

• Wow, I'm stunned... That's so brilliant! – Mr. Xcoder Jul 15 '17 at 15:24
• 32 bytes. Works by using append mode, setting the current position to the end of the file. tell() returns the current position in the file – Halvard Hummel Aug 29 '17 at 5:34
• @HalvardHummel Nice. However I have no intention of updating this answer. If you would like to post it on your own it is substantially different in my opinion. – Wheat Wizard Aug 29 '17 at 5:44
• @WheatWizard That is understandable, I made a separate answer – Halvard Hummel Aug 29 '17 at 5:48

# Jelly, 1 byte

Try it online!

I have no idea how this works, but apparently it does.

• That moment when you have no idea what you have written... – Mr. Xcoder Jul 15 '17 at 15:31
• Darn it, I just thought of this 8 minutes too late. – HyperNeutrino Jul 15 '17 at 15:40
• All links need an argument. If the first element of the chain is a nilad, its result becomes the argument and the link is executed monadically. If there is no leading nilad, 0 is used instead. – Dennis Jul 15 '17 at 20:03

# Google Sheets, 11 5 Bytes

Anonymous worksheet formula that takes no input and outputs into the cell which holds the formula

=4/(2

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

=4/(2
=4/(2)
=4/2
=2
2

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

=4/(2=4/(2
=4/(2=4/(2)
=4/(2=4/(2))
=4/(2=2)
=4/(True)
=4/True
=4/1
=4
4

Of course, an implication of using this method is that once this is repeated more than once, at the third and all following iterations of the problem, the call stack will reach =4/(2=4) and thus evaluate down to =4/0 and throw a #DIV/0! error

-6 bytes by switching to algebra away from the =DIVIDE(4,2 formula

• Never expected Google Sheets would have use in code golf. Clever solution – hucancode Jul 17 '17 at 3:11
• @hucancode the really interesting bit about this is that because Excel throws an error if you exclude the trailing )'s this answer is the only Google Sheets answer that I've Seen that does not translate into an Excel answer – Taylor Scott Jul 19 '17 at 13:18

# 05AB1E, 2 bytes

Original

XO

Try it online!

Doubled

XOXO

Try it online!

Explanation

X pushes 1 to the stack.
O sums the stack.

• XOXO, nice solution. – Mr. Xcoder Jul 15 '17 at 15:36
• You did that on purpose, while you knew you could've also used 1O! – Erik the Outgolfer Jul 15 '17 at 19:00
• Where do you guys find these ridiculous languages? – DaveTheMinion Jul 17 '17 at 4:01
• @DavidB Usually, they write them. – Federico Poloni Jul 20 '17 at 6:56
• @DavidB Yes, people do invent languages for codegolf, and yes, they can get impressively low scores, but doing silly things like inventing a language after the challenge to solve it in 1 byte are disallowed, and programming in these languages is usually far from easy. – Esolanging Fruit Jan 10 '18 at 4:03

## C (gcc), 37 bytes

i;main(){putchar(i+49);}/*
i=1;//*///

The file does not contain a trailing newline.

Doubled version, for syntax highlighting:

i;main(){putchar(i+49);}/*
i=1;//*///i;main(){putchar(i+49);}/*
i=1;//*///

• Can you please explain how this works? why would the comment ever be uncommented? – phil294 Jul 15 '17 at 20:29
• When you double the source code, the /* is commented out by the //, which means the following i=1 is uncommented. This is easier to see if you put the doubled version of the code in a syntax highlighter – musicman523 Jul 15 '17 at 20:50
• Whoa, a tentative definition trick. Nice. – aschepler Jul 18 '17 at 11:18

# Hexagony, 7 bytes

/)!@.).

Prints 1 regularly then 2 doubled.

### Expanded versions:

Regular:

/ )
! @ .
) .

Doubled:

/ ) !
@ . ) .
/ ) ! @ .
) . . .
. . .

The regular program follows the path: /)!.@ which increments a memory edge (all are initialised to zero) then prints its numeric value. The doubled program follows: /.)/)!@ which increments the edge twice before printing, instead.

• Wow nice work. I assume you found that by hand? Since 6 bytes is in brute force range, I thought I'd give it a go, and there's actually a 4-byte solution: [@!) (and some 570 5-byte solutions). Since you actually went to the trouble of finding a solution by hand, I'm perfectly happy for you to post the 4-byte solution. – Martin Ender Jul 16 '17 at 8:16
• If you're interested, here is the full list including the number that is printed: pastebin.com/TtRujjA4 – Martin Ender Jul 16 '17 at 8:24

# Python 2, 21 bytes

+1
if id:id=0;print 1

Try it online!

Doubled:

+1
if id:id=0;print 1+1
if id:id=0;print 1

Try it online!

• Crazily creative! Congrats! – Mr. Xcoder Jul 16 '17 at 7:50
• What if it is doubled with a linefeed inbetween? – user19214 Mar 17 '18 at 2:45

# Braingolf, 1 byte

+

Try it online!

Now we're talkin'!

Outputs 20, or 40 when source is doubled.

## Explanation

When there are at least 2 items on the stack, it's dyadic, and will sum the top 2 items of the stack.

When there is only 1 item on the stack, it's monadic, and will double the item.

When there are no items on the stack, it's niladic, and pushes 20!

Why does it push 20? Well because an empty Braingolf program simply prints a newline, and the ASCII value of a newline is 10, so I figured I'd make niladic + push 20 so it's like it's actually being monadic on the implicit newline (even though it isn't at all)

Therefore:

+   No input
Implicit output

And when doubled up:

++  No input
+  Monadic sum, Double top of stack
Implicit output

main=print$0 +1-- Try it online! Doubled: main=print$0
+1--main=print\$0
+1--

Try it online!

I found this version while answering the tripple version of the challenge.

### 26 byte version without comment abuse:

main|n<-1,nmain<-2=print n

Try it online! Prints 1.

In the pattern guard the identifier n is set to 1 and nmain to 2, then print n prints 1.

### Double program:

main|n<-1,nmain<-2=print nmain|n<-1,nmain<-2=print n

Try it online! Prints 2.

In the first pattern guard again n is set to 1 and nmain to 2, however the print statement has become print nmain, so 2 is printed. Because identifier declarations in a pattern guard evaluate to true, the second pattern guard can never be reached.

# Mathematica, 5 bytes

(1+1)

outputs 2 and (1+1)(1+1) outputs 4

and of course (as many of you asked)

# Mathematica, 3 bytes

(2)
• Does (2) work? – geokavel Jul 15 '17 at 15:29
• @geokavel 2 and a space would work, as would +1 but it should be noted that all of these assume Mathematica's notebook environment – Martin Ender Jul 15 '17 at 15:32
• If you run it in the opencloud Mathematica Sandbox, a single space becomes x, such that 2 outputs 2 and 2 2 becomes 2x2 that prints 4. You can add that as an alternative solution. – Mr. Xcoder Jul 15 '17 at 15:51
• (2) works as well. – Mr. Xcoder Jul 15 '17 at 15:53
• 2 works as well. – alephalpha Jul 16 '17 at 5:38

# Brain-Flak, 6 bytes

({}())

Try it online!

## Explanation

What this does should be pretty clear. {} grabs a value from the stack, which implicitly zero to begin with, () adds one to it and (...) pushes the value. On the second run since there is already a 1 on the stack this just adds another 1 to it to make two. In fact if you copy the code n times it will always output n.

# ><>, 7 6 bytes

-1 byte thanks to Teal pelican

\ln;
0

# Explanation

I used a 0 but I could have also used 1-9, a-f because they all push a single value onto the stack.

## Not doubled:

\ redirects execution down
0 pushes zero onto stack; STACK: [0]
(IP wraps around the bottom)
\ redirects execution right
l pushes stack length (1) onto stack; STACK: [0, 1]
n pops off the top value (1) and prints it; STACK: [0]
; end of execution

## Doubled:

\ redirects execution down
0 pushes zero onto stack; STACK: [0]
0 pushes zero onto stack; STACK: [0, 0]
(IP wraps around the bottom)
\ redirects execution right
l pushes stack length (2) onto stack; STACK: [0, 0, 2]
n pops off the top value (2) and prints it; STACK: [0, 0]
; end of execution
• Welcome to PPCG! This is a great first answer, with a very well thought explanation and good code. You earned my upvote! – Mr. Xcoder Jul 23 '17 at 11:46
• I've just seen this answer and it's great! Very nice improvement on the couple ><> answers (including mine!) - Have a look at the TIO link here; tio.run/##S8sszvj/PyYnz5rLAEL@/w8A - All I've done is move the 0 to the space and it saves 1 byte. :) – Teal pelican Jul 25 '17 at 16:22
• @Tealpelican Thanks! I can't believe I missed such a simple improvement. (Actually I can, I don't use ><> very much and I'm new to golfing!)To be honest, I didn't really look at the other solutions in detail, I saw they were relatively large and decided to try golfing the problem since it would be easier to compete. – Borka223 Jul 25 '17 at 20:45
• Nice answer! Just wanted to point out that this uses the same method as my Klein answer. (Not accusing you of copying me If anyone was that just pointing out two similar answers.) – Wheat Wizard Jul 26 '17 at 18:52

## Retina, 3 bytes

1

Try it online!

Prints 2. Doubling it prints 4.

The 1 can be replaced with pretty much anything else.

### Explanation

1

Replaces the empty input with 1.

Counts the number of empty matches in 1 which is two (one before the 1 and one after it).

If we double the program, we get an additional stage like the first one. This time it inserts a 1 before and after the initial one, giving 111. When we now count the number of matches of the empty regex we get four of them.

# Python REPL, 2 bytes

Also works in Pip, Dyalog APL, JavaScript, J, and R

+1

I'm making a TIO right now I couldn't get python repl to work on TIO

• +1 is exactly what I will do :P but darn, you ninja'd me by about a minute >< – HyperNeutrino Jul 15 '17 at 15:29
• This also works in Dyalog APL. – Kritixi Lithos Jul 15 '17 at 15:34
• Polygolt – Mr. Xcoder Jul 15 '17 at 16:10
• Couldn't you use this and this for a REPL? – totallyhuman Jul 15 '17 at 21:46
• You have my +1. – Federico Poloni Jul 20 '17 at 6:57

# Neim, 1 byte

>

Simply increments the top of the stack.

The stack can be imagined as an infinite amount of zeroes to start off, so this increments zero to get one, and doubled, increments it again to get two.

Try it online!

An alternative solution:

## JavaScript, 38 bytes

i++//*///

i++//*///

• Nice one! Could you just do i++? – Arnauld Jul 17 '17 at 13:36
• @Arnauld: indeed, thanks for the suggestion! – Benoit Esnard Jul 18 '17 at 22:03

# Java8, 135118 110 bytes

Single, prints 8

interface T{static void main(String[]a){System.out.print(Byte.SIZE);}}/*
class Byte{static int SIZE=16;}/**///

Doubled, prints 16

interface T{static void main(String[]a){System.out.print(Byte.SIZE);}}/*
class Byte{static int SIZE=16;}/**///interface T{static void main(String[]a){System.out.print(Byte.SIZE);}}/*
class Byte{static int SIZE=16;}/**///

Single, prints 1

interface T{static void main(String[]a){System.out.print(T.class.getResource("B.class")==null?1:2);}}/*
enum B{}/**///

Doubled, prints 2

interface T{static void main(String[]a){System.out.print(T.class.getResource("B.class")==null?1:2);}}/*
enum B{}/**///interface T{static void main(String[]a){System.out.print(T.class.getResource("B.class")==null?1:2);}}/*
enum B{}/**///

How this works

The java-compiler creates a single file for every class in the source file. Therefore i can simply check if a resource with the name B.class exists.

Single, prints 1

interface T{static void main(String[]a){int i=1;try{Class.forName("B");i=2;}catch(Exception e){}System.out.print(i);}}/*
enum B{}/**///

Doubled, prints 2

interface T{static void main(String[]a){int i=1;try{Class.forName("B");i=2;}catch(Exception e){}System.out.print(i);}}/*
enum B{}/**///interface T{static void main(String[]a){int i=1;try{Class.forName("B");i=2;}catch(Exception e){}System.out.print(i);}}/*
enum B{}/**///
• What you did with the comment is really cool. But do you really need the new line? – vikarjramun Jul 19 '17 at 14:14
• Oh nvm, didn't notice the single line comment on second line – vikarjramun Jul 19 '17 at 14:14

# Python 2, 32 bytes

print open(__file__,"a").tell()#

Try it online!

Double source code

## Explanation

This opens the source code file in append mode

open(__file__,"a")

We then find the current position in the file, this will be at the end of the file due to opening in append mode

open(__file__,"a").tell()

We print this length

print open(__file__,"a").tell()

And add a comment, so that doubling the source code does not execute more code

print open(__file__,"a").tell()#

# Excel VBA, 12 Bytes

Anonymous VBE immediate window function that takes input from and outputs to range [A1]. The default value of the range [A1] is "" (empty string) and after one execution the following sets this to 1 and increments by 1 with all subsequent executions.

[A1]=[A1+1]:

## Input / Output

Single Version

[A1]=[A1+1]:
?[A1]    ''# display the value of [A1] to the VBE immediate window
1

Doubled Version

[A1]=[A1+1]:[A1]=[A1+1]:
?[A1]    ''# display the value of [A1] to the VBE immediate window
2

# Japt, 1 byte

Ä

Rather simple. Japt transpiles to JS, and Ä transpiles to + 1, so ÄÄ transpiles to + 1 + 1, and so on.

• I was just about to post the same thing with É - luckily I scanned the answers first. – Shaggy Jul 25 '17 at 16:18

# Husk, 3 bytes

|1"

Try it online!

An original idea, for what I have seen in other answers.

### Explanation

| in Husk is an "or" operator which returns its second argument if it is thruthy, otherwise the first argument. When applied to arguments of different types it firstly transform all of them into numbers: the transformation for strings (and lists in general) is done by computing their length.

In the original program we apply | to 1 and an empty string, which gets converted to 0: the result is 1.

In the doubled program we apply | to 1 and the string "|1", which gets converted to 2: the result is 2.

# Braingolf, 1 byte

+

Try it online!

Try it doubled!

I don't know how this works, most important it does!

# CJam, 3 bytes

5],

Try it Online

Encapsulate 5 in array. Return length of array. When you duplicate code, the previously returned length, 1, is already on the stack, so you get an array of [1,5], which returns length 2.

# Ruby, 16 bytes

+1
p&&exit
p=p 1

Try it online!

### Doubled:

+1
p&&exit
p=p 1+1
p&&exit
p=p 1

Try it online!Try it online!

## Wumpus, 4 bytes

" O@

Try it online!

" O@" O@

Try it online!

The normal code prints 32 and the doubled one prints 64.

### Explanation

" works like it does in many other Fungeoids: it toggles string mode, where each individual character code is pushed to the stack, instead of executing the command. However, in contrast to most other Fungeoids, Wumpus's playfield doesn't wrap around, so the IP will instead reflect off the end and bounce back and forth through the code.

So for the single program, the following code is actually executed:

" O@O " O@

The string pushes 32, 79, 64, 79, 32. Then the space does nothing, the O prints 32, and the @ terminates the program.

For the doubled program, the string is instead terminated before the IP bounces back, so the code is only traversed once:

" O@" O@

This time, the string pushes 32, 79, 64, the O prints the 64 and the @ terminates the program.

This appears to be the only 4-byte solution.

# ,,,, 2 bytes

1∑

## Explanation

1∑

1   push 1
∑  pop everything and push the sum of the stack
• Am I missing something or wouldn't that be 3 for a doubled source code? – Taylor Scott Jul 15 '17 at 15:53
• @TaylorScott I'm sorry, I should've noted that pops all of the elements on the stack. – totallyhuman Jul 15 '17 at 15:54
• That makes much more sense - thanks for the clarification – Taylor Scott Jul 15 '17 at 15:55
• Sigma is two bytes, no? – nishantjr Jul 21 '17 at 2:41
• @nishantjr ,,, uses its own code page, which is linked in the header. – totallyhuman Jul 21 '17 at 11:47

## Batch, 13 bytes

@echo %~z0
:

Explanation: %~z0 expands to the length of the source file, so doubling the file simply doubles the length. The second line defines an empty label, which does nothing. When the file is doubled, it becomes a label named @echo %~z0 instead, while the third line is another empty label.

# 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, 5 bytes

90g.@

Try it online!

g gets the character value at coordinate (9, 0) in Funge-Space; . prints it as an integer, and @ halts the program. In the un-doubled version, (9, 0) is out of bounds of the program, and Funge-Space outside the program is initialized to the default value of a space, so we print 32. In the doubled version, (9, 0) is the @ character, so we print 64.

• This was not what i expected the best Befunge-98 answer to look like... really creative answer! – MildlyMilquetoast Sep 7 '17 at 4:21
• – MildlyMilquetoast Sep 7 '17 at 4:36

# 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) – Ton Hospel Feb 17 '18 at 23:19