# Just Twice Is Nice

Let's have string multiplication to take a string and a positive number and concatenate that string that many times. In Haskell:

0 * s = ""
n * s = s ++ ((n - 1) * s)


Your task is to write a program that outputs a number $$\n > 0\$$. When you double your program (with the described procedure) the new program should output $$\2n\$$. When your program is tripled or quadrupled it should once again output the original $$\n\$$.

This is so your answers will be scored in bytes with fewer bytes being better.

• All the solutions so far are trivial modifications of solutions to existing challenges so I wouldn't be surprised to see this dupe-hammered by someone. Dec 20, 2018 at 22:14
• @Shaggy I disagree. For some the best approach is from the 3rd times the charm from some its the doubling, for some still (klein, lost etc.) a port wouldn't even work. Dec 24, 2018 at 22:03

# Python 2, 9 bytes

Outputs via exit code. Full credit goes to Anders Kaseorg for this answer to I double the source, you double the output!.

';exit(2)


Regularly, this raises a Syntax Error because the string is not closed properly and exists with exit code 1, then, when doubled, it becomes ';exit(2)';exit(2) which simply exits with code 2 because the string literal is now quoted properly and has no effect at all, and when repeated any other arbitrary number of times, it raises Syntax Errors.

# R, 27 25 bytes

1+!1-length(readLines())



Try it online!

Inspired by rturnbull's answer to Third time the charm, but the simplicity of the check makes it shorter.

### Why this works:

readLines() will actually reads the source file itself rather than stdin. Hence, adding lines just increments the length() of the vector returned by readLines(). Therefore, we compute !(1-length()) to obtain 1 whenever length()==1 and 0 when length()!=1, adding one to have the desired effect.

# 05AB1E, 76 5 bytes, n=3

₆¾Θè¼


-1 byte thanks to @CommandMaster.

Explanation:

₆      # Push 36
¾     # Push the counter_variable (0 by default)
Θ    # 05AB1E-style truthify: ==1 (results in 1 if 1; 0 for everything else)
è   # Use that to index into the 36
¼  # Increase the counter_variable by 1
# (and output the indexed digit implicitly as result)

• 5 bytes: ₆¾¼Θè (uses the counter variable and thruthifies, so it doesn't have to compare to two) Try it online; Doubled; Tripled; quadrupled Nov 14, 2020 at 9:47
• @CommandMaster Smart! Thanks for the -1. :) Nov 14, 2020 at 11:24

# Runic Enchantments, 29 bytes

^w '\
/1@ 3
/ ~!4
2'51\w
/yyy


Try it online!

Slight alteration from the Third Times A Charm entry, using Jo King's compressed version and swapping the two reflection locations.

Twice

Thrice

And frice for good measure.

• What did you name the language after? God of War? Dec 25, 2018 at 0:39
• @Riker I named it because I want to (eventually) have a RPG-esque game where enchantments on gear can be created by players using a runic script (that, to the best of my ability) mapped to the 95 keyboard printable characters. I've added some extension symbols utilizing unicode combining characters (as well as a handful of other unicode characters that more readily represent the given action, such as π and several arrows). Once I have that game I'll end up making "runic 2.0" that has those game-specific commands (and probably remaps a lot of mathy stuff). Dec 25, 2018 at 2:10
• Draco: huh, interesting, I'd love to see the final result. Dec 25, 2018 at 4:26

# Klein 000, 6 bytes

\1+@
\


Try it online!

## Explanation

The idea here is pretty simple, the first 2 copies require the ip to move off the edge of the program but the third does not, that way adding anything extra after the third doesn't change anything.

The first program reflects off of both edges the

\
\


At the begining is just a fancy noop. So we just run 1+@.

For the two copies this gets interrupted

\1+@
\\1+@
\


We hit both 1s and a + to get 2.

On the three we interrupt the path even further

\1+@
\\1+@
\\1+@
\


At this point it might not be the clearest, but the ip bounces around in the mirrors a bit before it hits runs the 3rd line.

This is what it runs

\
\\
\1+@


Since this doesn't go off an edge further additions don't make any change to the program.

# Charcoal, 10 bytes

ＰＩ⊕⁼²Ｌ⊞Ｏυω


Try it online! Based on my answer to I double the source, you double the output! but compares the length to 2. Try it doubled. Try it tripled. Try it quadrupled. In verbose syntax this is Multiprint(Cast(Incremented(Equals(2, Length(PushOperator(u, w)))))));.

# MATL, 9 bytes

vxHQXH4=Q


Uses n = 1.

Try it online! Original, doubled, tripled, quadrupled.

### Explanation

This uses clipboard H to store state information. Function H pastes the clipboard contents onto the stack. Function XH copies the top of the stack into the clipboard. The clipboard initially contains the number 2.

Each time the snippet vxHQXH4=Q is run it does the following. The stack contents, if any, are deleted (vx). The clipboard contents are pushed (H) and incremented (Q), and the result is copied back into the clipboard (XH). This gives 4 the second time, and only that time.

The number in the stack is tested for equality with 4 (4=) and incremented (Q). This gives 2 for 4 (second time), and 1 otherwise (any other time).

# Brain-Flak, 36 bytes

(({}<>{})[()]<>)(()(){[()](<{}>)}{})


Try it online!

Uses a counter off stack. Each time we run the counter is incremented by the result of the last run. If the counter is equal to exactly 1 (which it is when the program is doubled) then we push 2, otherwise 1 is pushed.

The selection is done using a bit of a weird if statement. It is 2 normally but if the if is entered (which it is when the count is not 1) we subtract 1 from it.

# Bash, 8 bytes

This is a rip-off of the Python 2 answer. Output is via exit code.

Unlike the Python 2 answer, this uses $$\n=2\$$.

";exit 4


# Jelly, 6 4 bytes (n=1)

»ɼḂ‘


Try it online!

## Explanation

Initial value = 0
Initial register value = 0
ɼ     Set the register and current value to
»        maximum of the register and current value
Ḃ    Parity
‘   Increment


# PowerShell, 24 bytes

';\$host.setshouldexit(2)


Outputs through exit code, Shameless port of python answer ;-)

# 05AB1E, 4 bytes (n=1)

.gΘ>


Try it online!

## Explanation

.gΘ>
.g   Push length of stack
Θ  Equals 1?
> Increment


# Vyxal, 4 bytes (n=1)

∴:∷›


Try it Online!

Same approach as my Jelly answer, but much more aesthetic.

∴:∷›
∴    Pop a, b; push max(a,b)
:   Duplicate
∷  Parity
› Increment