51
\$\begingroup\$

Your task, if you wish to accept it, is to write a program that outputs a positive integer (higher than 0). If the source code is duplicated the output must remain the same. The tricky part is that if the source code is typed three times (triplicated?) the output will be multiplied by 3.

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.

  • Leading Zeroes are allowed only if the numbers of digits is consistent eg: 001 - 001 - 003 or 004 - 004 - 012

  • 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 still be 4. However if I write AbcAbcAbc and run it, the output must be 12.


Shamelessly stolen Derived from Mr. Xcoder's challenge

\$\endgroup\$
16
  • \$\begingroup\$ Is our code allowed to read its own source code? \$\endgroup\$ Commented Mar 12, 2018 at 13:45
  • \$\begingroup\$ @AdmBorkBork I'd assume so, since this isn't tagged as quine. \$\endgroup\$ Commented Mar 12, 2018 at 13:47
  • \$\begingroup\$ @AdmBorkBork Yes. \$\endgroup\$ Commented Mar 12, 2018 at 14:06
  • 10
    \$\begingroup\$ I don't think the 1 byte restriction is needed since it wouldn't be possible to tell the difference between nothing and nothing repeated 3 times. \$\endgroup\$
    – 12Me21
    Commented Mar 13, 2018 at 1:42
  • 1
    \$\begingroup\$ @r12 "Abc" is an example for any programming language program, say if your code is (int i=1;print i;) then the duplicated code of (int i=1;print i;int i=1;print i;) must output the same number as the original code, and when the code is triplicated to (int i=1;print i;int i=1;print i;int i=1;print i;) it must show the number multiplied by 3 \$\endgroup\$ Commented Mar 16, 2018 at 16:54

51 Answers 51

1
2
1
\$\begingroup\$

Perl 5, 28 25 bytes

-3 bytes thanks to @neil!

print"\e[D$a"if(++$a!=2);

Moves the cursor backwards (does nothing on start of line) and prints the value of $a, first and third time (i.e., the third time a 1 is printed, the cursor moves, and a 3 is printed on the position of the 1).

\$\endgroup\$
1
  • 1
    \$\begingroup\$ print"\e[D$a"if(++$a!=2); perhaps? \$\endgroup\$
    – Neil
    Commented Mar 12, 2018 at 17:15
1
\$\begingroup\$

QBasic, 19 bytes

CLS
x=2^x
?x-1OR 1

The source should include a trailing newline.

Explanation

We want output of 1, 1, 3. Observe that these numbers are one less than powers of 2. So:

CLS      ' CLear Screen of any output from previous copies of the code

x        ' Numeric variables are preset to 0...
 =2^x    ' so as this statement is repeated, the value of x goes 1, 2, 4

 x-1     ' Therefore x-1 goes 0, 1, 3...
    OR 1 ' and we bitwise OR it with 1 to change the 0 to 1...
?        ' and print.
\$\endgroup\$
1
\$\begingroup\$

DOS .BAT, 60 Bytes

@cls&find /n " " %0|findstr /b .^3>nul&&set/p=^1<nul&echo 5
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 17 bytes

0 x 0;say .++ +|1

Try it online!

Try it doubled!

Try it tripled!

\$\endgroup\$
1
\$\begingroup\$

PHP, 29 bytes

<?=filesize(__FILE__)<86?:3;#

Try it online!

Try it doubled!

Try it tripled!

How it works

  • <?= -- Opens a PHP tag and echos whatever's inside. It's a shorthand for <?php echo.
  • filesize(__FILE__) -- Gets the size of the current file.
  • <86 -- Checks that the size is less than 86 since when the code is triplicated, it's 87 bytes long.
  • ?:3; -- If the condition is true, it outputs 1 since the middle of the ternary is omitted. If not, then output 3.
  • ;# -- PHP supports shell-style comments (#) and they're less bytes than the C++ style comment (//). This just makes the compiler ignore the other two copies of code.
\$\endgroup\$
1
\$\begingroup\$

Haskell (runhaskell), 46 bytes

-8 bytes thanks to BMO.

main=do x<-readFile"a";print$length x`gcd`3 --

Try it online!

Needs the file name to be a. Apparently GHC needs the file to end with .hs, but runhaskell doesn't care.

\$\endgroup\$
0
1
\$\begingroup\$

Klein 000 or 010, 8 bytes

\((1+@
2

Try it online!

\((1+@
2\((1+@
2

Try it online!

\((1+@
2\((1+@
2\((1+@
2

Try it online!

Explanation

The backbone of our program is the left had side which pushes a number of 2s. Each time we add a copy of the program another 2 gets added to the backbone of the program. Once it goes off the bottom it executes the code

((1+@

Which removes the first 2 stack items and adds one to the remainder. Once our backbone has 3 2s we will add 1 and get 3, if we have any less we will just discard the entire stack and return 1.

Klein 001 or 011, 8 bytes

\(1+@2
.

Try it online!

\(1+@2
.\(1+@2
.

Try it online!

\(1+@2
.\(1+@2
.\(1+@2
.

Try it online!

Explanation

The 010 and 011 topologies work a little differently. When the ip goes of the bottom on the left it comes down on the right. Here we do something very similar except our ending bit is

\(1+@

which will output 1 until the stack is two high. The . causes the 2 to be cut off when we extend the program meaning we only pick up our second 2 when we reach the third iteration of the program.

\$\endgroup\$
1
\$\begingroup\$

Bash, 26 bytes

exit $[`wc -c&lt;$0`&gt;70?3:1];

Includes trailing newline.

Outputs via exit code.

Very similar to my zsh answer.

exit instead of echo means instead of <space># at the end, we only have a single ;.

-2 bytes thanks to Digital Trauma

\$\endgroup\$
1
  • \$\begingroup\$ $[...] instead of $((...)) saves 2 bytes. I don't know if this works for zsh too... \$\endgroup\$ Commented Mar 14, 2018 at 21:41
1
\$\begingroup\$

Zsh, 25 bytes

<<<$[`wc -c<$0`>70?3:1] #

Zsh lets you use here-docs and here-strings in place instead of regular commands, so we can replace a call to echo with <<< which saves two bytes.

-2 thanks to Digital Trauma

\$\endgroup\$
1
\$\begingroup\$

Python 2, 38 bytes

1
id=id<3and-~id
exec"print id|1"*0**0

Try it online! (doubled, tripled)

Outputs 1, 1, or 3.


Python 2, 33 bytes

1
True+=1
exec"print 7%True"*0**0

Try it online! (doubled, tripled)

Outputs 1,1,3. Uses the True-modifying idea by Aidan F. Pierce from Xcoder's solution.

\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 23 bytes

Once[c=0];c++//1-#+#^2&

Try it online!

Try it doubled!

Try it tripled!

Explanation:

In Wolfram Language Once evaluates the contained expression once in each session, always returning the result from the first evaluation. This lets me set an anchor point for c. Now it's just a matter of math.

\$\endgroup\$
5
  • \$\begingroup\$ I think the reason it works locally is that you've already redefined Times on the first prompt, so on the last prompt it's already redefined. Does it still work if you only execute the tripled line? \$\endgroup\$ Commented Mar 14, 2018 at 19:14
  • \$\begingroup\$ Ahh that's probably it \$\endgroup\$
    – chuy
    Commented Mar 14, 2018 at 20:33
  • \$\begingroup\$ Back to the drawing board \$\endgroup\$
    – chuy
    Commented Mar 14, 2018 at 20:33
  • \$\begingroup\$ According to Wolfram Sandbox, directly executing the tripled line outputs a 1. \$\endgroup\$ Commented Mar 15, 2018 at 0:34
  • \$\begingroup\$ Yes I need to modify \$\endgroup\$
    – chuy
    Commented Mar 15, 2018 at 0:48
1
\$\begingroup\$

Perl 5, 10 bytes

say+0|++$i

Try it once!

Try it twice!

Try it thrice!

\$\endgroup\$
1
\$\begingroup\$

Attache, 34 bytes

Echo@Sum@ProperDivisors@`#<|1
'1??

Try it online! Implements A001065. There are probably shorter ways, but this was fun.

Prints A001065(# of repetitions + 1).

Doubled program

Tripled program

\$\endgroup\$
1
\$\begingroup\$

Powershell, 14 bytes

Sum array elements. An operator + can work as a unary and binary operator.

+(1,0,2)[$i++]

Test script:

+(1,0,2)[$i++]

$i=0 # or start new powershell session

+(1,0,2)[$i++]+(1,0,2)[$i++]

$i=0 # or start new powershell session

+(1,0,2)[$i++]+(1,0,2)[$i++]+(1,0,2)[$i++]

Note: $i=0 emulates a new session. You can type three times in a new powershell terminal window to start a pure test instead.

Output:

1
1
3
\$\endgroup\$
1
\$\begingroup\$

Befunge-98 (PyFunge), 12 9 6 bytes (by Jo King)

3g.@
`

Try it once!

Try it doubled!

Try it tripled!


Old solution:

2g' `4\kc.@

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Nice one. Edited the post. \$\endgroup\$
    – david
    Commented Sep 7, 2019 at 3:45
  • \$\begingroup\$ Also just realized, that your answer makes it a valid Befunge-93 program, instead of being limited to Befunge-98. \$\endgroup\$
    – david
    Commented Sep 7, 2019 at 4:05
  • 1
    \$\begingroup\$ Aha! 6 bytes by printing the fetched character value outright. \$\endgroup\$
    – Jo King
    Commented Sep 7, 2019 at 15:51
1
\$\begingroup\$

TI-Basic, 14 bytes


A+1→A
1+2(Ans=3

There is a newline at the beginning.


19 bytes


Ai+not(A→A
2-real(Ans)-imag(Ans

There is a newline at the beginning.

\$\endgroup\$
0
\$\begingroup\$

Batch, 28 bytes

@cmd/cset/a1+2*!(%~z0%%3)
:

Conveniently the file length is not a multiple of 3 until the file is tripled, so it remains to take the remainder modulo 3, take the logical not, double that, and add 1.

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 18 bytes

X=X+1CLS?(X>2)*2+1
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 10 bytes

P§311L⊞Oυω

Try it online! Based on my answer to I double the source, you double the output! but I cyclically look up the appropriate character to print instead of just printing the current length cast to string. (The 3 is first because the iteration numbers are 1-indexed but Charcoal's string indexing is 0-indexed.)

\$\endgroup\$
0
\$\begingroup\$

sed, 37 bytes

g
s/^/1/
H
s/\n//g
s/1111/3/
s/11/1/

Using the pattern space to preserve state is the key here.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 4 bytes

X3Ur

Try it online!, doubled, tripled.

X           # push X on the stack (X defaults to 1)
 3U         # set X = 3
   r        # reverse the stack
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.