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 code-golf, 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.
Leaderboard
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 ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8349457; // This should be the user ID of the challenge author.
/* App */
var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;
function answersUrl(index) {
return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
jQuery.ajax({
url: answersUrl(answer_page++),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
answers.push.apply(answers, data.items);
answers_hash = [];
answer_ids = [];
data.items.forEach(function(a) {
a.comments = [];
var id = +a.share_link.match(/\d+/);
answer_ids.push(id);
answers_hash[id] = a;
});
if (!data.has_more) more_answers = false;
comment_page = 1;
getComments();
}
});
}
function getComments() {
jQuery.ajax({
url: commentUrl(comment_page++, answer_ids),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(c) {
if (c.owner.user_id === OVERRIDE_USER)
answers_hash[c.post_id].comments.push(c);
});
if (data.has_more) getComments();
else if (more_answers) getAnswers();
else process();
}
});
}
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(-?\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var OVERRIDE_REG = /^Override\s*header:\s*/i;
function getAuthorName(a) {
return a.owner.display_name;
}
function process() {
var valid = [];
answers.forEach(function(a) {
var body = a.body;
a.comments.forEach(function(c) {
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],
link: a.share_link,
});
});
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;
var answer = jQuery("#answer-template").html();
answer = answer.replace("{{PLACE}}", lastPlace + ".")
.replace("{{NAME}}", a.user)
.replace("{{LANGUAGE}}", a.language)
.replace("{{SIZE}}", a.size)
.replace("{{LINK}}", a.link);
answer = jQuery(answer);
jQuery("#answers").append(answer);
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)
.replace("{{LINK}}", lang.link);
language = jQuery(language);
jQuery("#languages").append(language);
}
}
body { text-align: left !important}
#answer-list {
padding: 10px;
width: 290px;
float: left;
}
#language-list {
padding: 10px;
width: 290px;
float: left;
}
table thead {
font-weight: bold;
}
table td {
padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/Sites/codegolf/all.css?v=617d0685f6f3">
<div id="answer-list">
<h2>Leaderboard</h2>
<table class="answer-list">
<thead>
<tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
</thead>
<tbody id="answers">
</tbody>
</table>
</div>
<div id="language-list">
<h2>Winners by Language</h2>
<table class="language-list">
<thead>
<tr><td>Language</td><td>User</td><td>Score</td></tr>
</thead>
<tbody id="languages">
</tbody>
</table>
</div>
<table style="display: none">
<tbody id="answer-template">
<tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr>
</tbody>
</table>
<table style="display: none">
<tbody id="language-template">
<tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr>
</tbody>
</table>
162 Answers 162
Python 2, 33 bytes
print len(open(__file__).read())#
Python 3, 28 bytes
print(len(*open(__file__)))#
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.
-
37\$\begingroup\$ Wow, I'm stunned... That's so brilliant! \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月15日 15:24:20 +00:00Commented Jul 15, 2017 at 15:24
-
\$\begingroup\$ @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. \$\endgroup\$2017年08月29日 05:44:33 +00:00Commented Aug 29, 2017 at 5:44
-
64\$\begingroup\$ That moment when you have no idea what you have written... \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月15日 15:31:50 +00:00Commented Jul 15, 2017 at 15:31
-
5\$\begingroup\$ Darn it, I just thought of this 8 minutes too late. \$\endgroup\$2017年07月15日 15:40:02 +00:00Commented Jul 15, 2017 at 15:40
-
17\$\begingroup\$ 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. \$\endgroup\$Dennis– Dennis2017年07月15日 20:03:17 +00:00Commented Jul 15, 2017 at 20:03
Google Sheets, 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
-
21\$\begingroup\$ Never expected Google Sheets would have use in code golf. Clever solution \$\endgroup\$hucancode– hucancode2017年07月17日 03:11:40 +00:00Commented Jul 17, 2017 at 3:11
-
12\$\begingroup\$ @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 \$\endgroup\$Taylor Raine– Taylor Raine2017年07月19日 13:18:34 +00:00Commented Jul 19, 2017 at 13:18 -
1\$\begingroup\$ LibreOffice works. \$\endgroup\$Joao-3– Joao-32022年03月15日 13:59:19 +00:00Commented Mar 15, 2022 at 13:59
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;//*///
-
3\$\begingroup\$ Can you please explain how this works? why would the comment ever be uncommented? \$\endgroup\$phil294– phil2942017年07月15日 20:29:32 +00:00Commented Jul 15, 2017 at 20:29
-
11\$\begingroup\$ When you double the source code, the
/*is commented out by the//, which means the followingi=1is uncommented. This is easier to see if you put the doubled version of the code in a syntax highlighter \$\endgroup\$musicman523– musicman5232017年07月15日 20:50:46 +00:00Commented Jul 15, 2017 at 20:50 -
1\$\begingroup\$ Whoa, a tentative definition trick. Nice. \$\endgroup\$aschepler– aschepler2017年07月18日 11:18:38 +00:00Commented Jul 18, 2017 at 11:18
-
3\$\begingroup\$ Crazily creative! Congrats! \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月16日 07:50:16 +00:00Commented Jul 16, 2017 at 7:50
-
\$\begingroup\$ What if it is doubled with a linefeed inbetween? \$\endgroup\$user19214– user192142018年03月17日 02:45:15 +00:00Commented Mar 17, 2018 at 2:45
Hexagony, 7 bytes
/)!@.).
Prints 1 regularly then 2 doubled.
Try it online! or Try it doubled online!
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.
-
8\$\begingroup\$ 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. \$\endgroup\$Martin Ender– Martin Ender2017年07月16日 08:16:35 +00:00Commented Jul 16, 2017 at 8:16 -
1\$\begingroup\$ If you're interested, here is the full list including the number that is printed: pastebin.com/TtRujjA4 \$\endgroup\$Martin Ender– Martin Ender2017年07月16日 08:24:03 +00:00Commented Jul 16, 2017 at 8:24
-
53\$\begingroup\$ XOXO, nice solution. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月15日 15:36:57 +00:00Commented Jul 15, 2017 at 15:36
-
4\$\begingroup\$ You did that on purpose, while you knew you could've also used
1O! \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月15日 19:00:33 +00:00Commented Jul 15, 2017 at 19:00 -
15\$\begingroup\$ Where do you guys find these ridiculous languages? \$\endgroup\$DaveTheMinion– DaveTheMinion2017年07月17日 04:01:52 +00:00Commented Jul 17, 2017 at 4:01
-
9\$\begingroup\$ @DavidB Usually, they write them. \$\endgroup\$Federico Poloni– Federico Poloni2017年07月20日 06:56:28 +00:00Commented Jul 20, 2017 at 6:56
-
7\$\begingroup\$ @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. \$\endgroup\$Esolanging Fruit– Esolanging Fruit2018年01月10日 04:03:48 +00:00Commented Jan 10, 2018 at 4:03
Braingolf, 1 byte
+
Now we're talkin'!
Outputs 20, or 40 when source is doubled.
Explanation
+ is of course the "sum", "add" or "plus" operator, in Braingolf, however it has dyadic, monadic and niladic functions.
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
+ Niladic sum, Push 20
Implicit output
And when doubled up:
++ No input
+ Niladic sum, Push 20
+ Monadic sum, Double top of stack
Implicit output
Haskell, (削除) 26 (削除ここまで) 18 bytes
main=print0ドル
+1--
Doubled:
main=print0ドル
+1--main=print0ドル
+1--
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)
-
3\$\begingroup\$ Does
(2)work? \$\endgroup\$geokavel– geokavel2017年07月15日 15:29:33 +00:00Commented Jul 15, 2017 at 15:29 -
9\$\begingroup\$ @geokavel
2and a space would work, as would+1but it should be noted that all of these assume Mathematica's notebook environment \$\endgroup\$Martin Ender– Martin Ender2017年07月15日 15:32:25 +00:00Commented Jul 15, 2017 at 15:32 -
1\$\begingroup\$ If you run it in the opencloud Mathematica Sandbox, a single space becomes
x, such that2outputs2and2 2becomes2x2that prints 4. You can add that as an alternative solution. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月15日 15:51:42 +00:00Commented Jul 15, 2017 at 15:51 -
2\$\begingroup\$
(2)works as well. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月15日 15:53:02 +00:00Commented Jul 15, 2017 at 15:53 -
10\$\begingroup\$
2works as well. \$\endgroup\$alephalpha– alephalpha2017年07月16日 05:38:50 +00:00Commented Jul 16, 2017 at 5:38
Brain-Flak, 6 bytes
({}())
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
Try it online!
Try it doubled!
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
-
5\$\begingroup\$ Welcome to PPCG! This is a great first answer, with a very well thought explanation and good code. You earned my upvote! \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月23日 11:46:07 +00:00Commented Jul 23, 2017 at 11:46
-
1\$\begingroup\$ 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. :) \$\endgroup\$Teal pelican– Teal pelican2017年07月25日 16:22:44 +00:00Commented Jul 25, 2017 at 16:22
-
1\$\begingroup\$ @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. \$\endgroup\$user58288– user582882017年07月25日 20:45:13 +00:00Commented Jul 25, 2017 at 20:45
-
1\$\begingroup\$ 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.) \$\endgroup\$2017年07月26日 18:52:03 +00:00Commented Jul 26, 2017 at 18:52
Python REPL, 2 bytes
Also works in Pip, Dyalog APL, JavaScript REPL, J, and R
+1
(削除) I'm making a TIO right now (削除ここまで) I couldn't get python repl to work on TIO
-
3\$\begingroup\$
+1is exactly what I will do :P but darn, you ninja'd me by about a minute >< \$\endgroup\$2017年07月15日 15:29:56 +00:00Commented Jul 15, 2017 at 15:29 -
2\$\begingroup\$ This also works in Dyalog APL. \$\endgroup\$user41805– user418052017年07月15日 15:34:57 +00:00Commented Jul 15, 2017 at 15:34
-
1\$\begingroup\$ Couldn't you use this and this for a REPL? \$\endgroup\$totallyhuman– totallyhuman2017年07月15日 21:46:02 +00:00Commented Jul 15, 2017 at 21:46
-
2\$\begingroup\$ You have my +1. \$\endgroup\$Federico Poloni– Federico Poloni2017年07月20日 06:57:47 +00:00Commented Jul 20, 2017 at 6:57
-
1\$\begingroup\$ To be clear, it works in some JavaScript REPLs, it's not "a complete program" as requested. \$\endgroup\$Steve Bennett– Steve Bennett2020年09月20日 13:32:31 +00:00Commented Sep 20, 2020 at 13:32
JavaScript, 38 bytes
setTimeout('alert(i)',i=1)/*
i++//*///
setTimeout('alert(i)',i=1)/*
i++//*///setTimeout('alert(i)',i=1)/*
i++//*///
-
1\$\begingroup\$ Nice one! Could you just do
i++? \$\endgroup\$Arnauld– Arnauld2017年07月17日 13:36:03 +00:00Commented Jul 17, 2017 at 13:36
Python 2, 32 bytes
print open(__file__,"a").tell()#
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()#
Retina, 3 bytes
1
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.
Java8, (削除) 135 (削除ここまで) (削除) 118 (削除ここまで) 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;}/**///
Previews answer, 118 bytes
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.
Orginal Answer, 135 bytes
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{}/**///
-
\$\begingroup\$ What you did with the comment is really cool. But do you really need the new line? \$\endgroup\$vikarjramun– vikarjramun2017年07月19日 14:14:00 +00:00Commented Jul 19, 2017 at 14:14
-
\$\begingroup\$ Oh nvm, didn't notice the single line comment on second line \$\endgroup\$vikarjramun– vikarjramun2017年07月19日 14:14:29 +00:00Commented Jul 19, 2017 at 14:14
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.
An alternative solution:
ᛖ
Adds 2, instead of 1.
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
-
2\$\begingroup\$ Ooh, nice to see Shenzhen I/O here. \$\endgroup\$Bubbler– Bubbler2021年03月04日 05:19:08 +00:00Commented Mar 4, 2021 at 5:19
-
1\$\begingroup\$ Why not just
@add 7/@mov acc x1? \$\endgroup\$l4m2– l4m22022年02月14日 18:01:13 +00:00Commented Feb 14, 2022 at 18:01 -
\$\begingroup\$ @l4m2 huh, I don't know. Seems like that would work. \$\endgroup\$Zack C.– Zack C.2022年02月15日 05:00:56 +00:00Commented Feb 15, 2022 at 5:00
Japt, 1 byte
Ä
Try it online!
Try it doubled!
Repeats even longer, too!
Rather simple. Japt transpiles to JS, and Ä transpiles to + 1, so ÄÄ transpiles to + 1 + 1, and so on.
-
\$\begingroup\$ I was just about to post the same thing with
É- luckily I scanned the answers first. \$\endgroup\$Shaggy– Shaggy2017年07月25日 16:18:31 +00:00Commented Jul 25, 2017 at 16:18
Husk, 3 bytes
|1"
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.
CJam, 3 bytes
5],
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
Doubled:
+1
p&&exit
p=p 1+1
p&&exit
p=p 1
Braingolf, 1 byte
+
I don't know how this works, most important it does!
-
\$\begingroup\$ As far as I can see, this is a late duplicate of the well explained codegolf.stackexchange.com/a/132573/72726, so this can be deleted. \$\endgroup\$Philippos– Philippos2023年09月20日 08:38:14 +00:00Commented Sep 20, 2023 at 8:38
-
\$\begingroup\$ @Philippos Duplicate answers are allowed \$\endgroup\$The Fifth Marshal– The Fifth Marshal2023年11月19日 01:20:16 +00:00Commented Nov 19, 2023 at 1:20
Befunge-98, 5 bytes
90g.@
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.
-
\$\begingroup\$ This was not what i expected the best Befunge-98 answer to look like... really creative answer! \$\endgroup\$MercyBeaucou– MercyBeaucou2017年09月07日 04:21:35 +00:00Commented Sep 7, 2017 at 4:21
-
\$\begingroup\$ I spoke too soon ;) \$\endgroup\$MercyBeaucou– MercyBeaucou2017年09月07日 04:36:26 +00:00Commented Sep 7, 2017 at 4:36
Wumpus, 4 bytes
" O@
" O@" O@
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.
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)
-
\$\begingroup\$ Can you provide a working link for me to test it on my mac as well (the classic problem)? EDIT: Thanks. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月15日 15:19:06 +00:00Commented Jul 15, 2017 at 15:19
-
\$\begingroup\$ Am I missing something or wouldn't that be 3 for a doubled source code? \$\endgroup\$Taylor Raine– Taylor Raine2017年07月15日 15:53:26 +00:00Commented Jul 15, 2017 at 15:53
-
\$\begingroup\$ @TaylorScott I'm sorry, I should've noted that
∑pops all of the elements on the stack. \$\endgroup\$totallyhuman– totallyhuman2017年07月15日 15:54:14 +00:00Commented Jul 15, 2017 at 15:54 -
1\$\begingroup\$ That makes much more sense - thanks for the clarification \$\endgroup\$Taylor Raine– Taylor Raine2017年07月15日 15:55:01 +00:00Commented Jul 15, 2017 at 15:55
-
\$\begingroup\$ Sigma is two bytes, no? \$\endgroup\$nishantjr– nishantjr2017年07月21日 02:41:51 +00:00Commented Jul 21, 2017 at 2:41
-
1\$\begingroup\$ @nishantjr ,,, uses its own code page, which is linked in the header. \$\endgroup\$totallyhuman– totallyhuman2017年07月21日 11:47:50 +00:00Commented Jul 21, 2017 at 11:47
JavaScript (ES6), 63 bytes
Prints either 1 or 2 through an alert dialog.
Original
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})`);
-
1\$\begingroup\$ I guess this would work also:
window.i=++window.i||1;in the browser console. It ouputs1. Browser refresh,window.i=++window.i||1;window.i=++window.i||1;ouputs 2. \$\endgroup\$Christiaan Westerbeek– Christiaan Westerbeek2017年07月17日 13:13:23 +00:00Commented Jul 17, 2017 at 13:13 -
1\$\begingroup\$ @ChristiaanWesterbeek True. But then it's a REPL answer (and you can actually just do
this.i=++this.i||1;). \$\endgroup\$Arnauld– Arnauld2017年07月17日 13:23:56 +00:00Commented Jul 17, 2017 at 13:23 -
1\$\begingroup\$ I don't know what that means, REPL answer \$\endgroup\$Christiaan Westerbeek– Christiaan Westerbeek2017年07月17日 13:26:55 +00:00Commented Jul 17, 2017 at 13:26
-
\$\begingroup\$ @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). \$\endgroup\$Arnauld– Arnauld2017年07月17日 13:31:07 +00:00Commented Jul 17, 2017 at 13:31
-
1\$\begingroup\$ @ChristiaanWesterbeek (Actually, just
+1would work in REPL -- like this Python REPL answer does.) \$\endgroup\$Arnauld– Arnauld2017年07月17日 13:33:31 +00:00Commented Jul 17, 2017 at 13:33
5. If you double it, your source is `` (empty program) and that produces5as 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. \$\endgroup\$