48
\$\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\$
  • \$\begingroup\$ Is our code allowed to read its own source code? \$\endgroup\$ – AdmBorkBork Mar 12 '18 at 13:45
  • \$\begingroup\$ @AdmBorkBork I'd assume so, since this isn't tagged as quine. \$\endgroup\$ – Erik the Outgolfer Mar 12 '18 at 13:47
  • \$\begingroup\$ @AdmBorkBork Yes. \$\endgroup\$ – workoverflow Mar 12 '18 at 14:06
  • 9
    \$\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 Mar 13 '18 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\$ – workoverflow Mar 16 '18 at 16:54

47 Answers 47

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\$
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\$
  • \$\begingroup\$ $[...] instead of $((...)) saves 2 bytes. I don't know if this works for zsh too... \$\endgroup\$ – Digital Trauma Mar 14 '18 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\$
  • \$\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\$ – Martin Ender Mar 14 '18 at 19:14
  • \$\begingroup\$ Ahh that's probably it \$\endgroup\$ – chuy Mar 14 '18 at 20:33
  • \$\begingroup\$ Back to the drawing board \$\endgroup\$ – chuy Mar 14 '18 at 20:33
  • \$\begingroup\$ According to Wolfram Sandbox, directly executing the tripled line outputs a 1. \$\endgroup\$ – Weijun Zhou Mar 15 '18 at 0:34
  • \$\begingroup\$ Yes I need to modify \$\endgroup\$ – chuy Mar 15 '18 at 0:48
1
\$\begingroup\$

Ruby, 31 bytes

$><<?\b;a||=0;a+=1;$><<a/3*2+1;

This uses \b which doesn't work everywhere (tio).

\$\endgroup\$
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\$

Lost, 27 bytes

\\<<<<//<<<>
2>((1+@>((%^
v

Try it online!

Try it doubled!

Try it tripled!

This is mostly an improvement of Post Left Garf Hunter's answer that removes all the pesky no-ops from that last line,

To do this, we have to remove the !, otherwise the program always gets stuck in a loop. So we change the clearing section from >?!^%^ to >))%^' since there can only be a max of two elements on the stack anyway. Unfortunately, this means that the path from the extra copies to the original is broken, so we place an extra / in the first line to compensate. This forces the pointer to go through the clearing section for all copies of the program until it reaches the first one, where the ^ instead redirects it to the > instead.

\$\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\$

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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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