There have been a couple of challenges involving doubling source code: here and here. The task we have here is a bit harder, but should be doable in almost every language.
In this challenge, you will take in an arbitrary positive integer. Your program must output that integer doubled. When your source code is doubled, it will take in a positive integer, and output it squared.
How is your source code doubled? Well, you can have it your way. That is to say, you can split your source code up into strings of bytes or characters (or tokens in tokenized langs) of any equal length you want, and repeat each chunk twice in succession.
For an initial program of ABCDEFGHIJKL
, (length 12) here are all possible doubled programs:
Chunk length | Doubled source code
-------------+-------------------------
1 | AABBCCDDEEFFGGHHIIJJKKLL
2 | ABABCDCDEFEFGHGHIJIJKLKL
3 | ABCABCDEFDEFGHIGHIJKLJKL
4 | ABCDABCDEFGHEFGHIJKLIJKL
6 | ABCDEFABCDEFGHIJKLGHIJKL
12 | ABCDEFGHIJKLABCDEFGHIJKL
Note that this means programs of prime lengths can only be doubled two ways: every character doubled, or the full program is repeated twice.
Rules:
- Code must be a full program or function.
- Standard loopholes are forbidden.
- Standard I/O methods are allowed.
- All characters/bytes, including whitespace and newlines, are counted in the length of the code and contribute to chunks.
- You may assume that the input and its square can be represented by your language's int/integer type.
- You may not assume a trailing newline or other character.
- Provide your chunk size in the heading after the byte count.
- This is code-golf, so shorter programs are better! If two programs are the same length, the one which uses the smaller chunk length wins. (If you have a longer program which uses a smaller chunk length, that is worth posting too!)
- If your program requires a second input/line, you may make no assumptions on its value. In particular, your program should work if the second input is empty, the same as the first, or a different integer. If your program does not require a second input/line, you may ignore this restriction.
.0
at the end)? \$\endgroup\$5 -> 10
; double code:5 -> 25 25
. \$\endgroup\$