In this challenge, you will write a program which outputs a program which is the double the length of the original program. The outputted program should output a new program a program double its length.
Examples
If my program is a:
< a
> aa
< aa
> aaaa
< aaaa
> aaaaaaaa
Rules
- No quine built-ins
- The original program must be at least one byte
- The sequence should theoretically work infinitely
- Your program is not allowed to read from anything (file, stdio)
Your score is the size of the original program.
-
\$\begingroup\$ How about accessing the file by reading itself/accessing the place where the code is contained? \$\endgroup\$Conor O'Brien– Conor O'Brien2016年01月15日 23:36:57 +00:00Commented Jan 15, 2016 at 23:36
-
\$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ that's not allowed as that would violate rules I believe. \$\endgroup\$Downgoat– Downgoat2016年01月15日 23:37:37 +00:00Commented Jan 15, 2016 at 23:37
-
2\$\begingroup\$ @Doᴡɴɢᴏᴀᴛ the quine rules are recommended by the tag wiki but don't apply automatically \$\endgroup\$Martin Ender– Martin Ender2016年01月16日 00:27:45 +00:00Commented Jan 16, 2016 at 0:27
-
1\$\begingroup\$ Related: codegolf.stackexchange.com/questions/21831/… \$\endgroup\$Christopher King– Christopher King2016年01月16日 01:35:50 +00:00Commented Jan 16, 2016 at 1:35
-
2\$\begingroup\$ Related. Related. Related. \$\endgroup\$Martin Ender– Martin Ender2016年01月16日 11:39:33 +00:00Commented Jan 16, 2016 at 11:39
7 Answers 7
CJam, 10 bytes
{"_~"+_}_~
Explanation
{"_~" e# Generalised quine framework, leaves the block and the string "_~"
e# on the stack.
+ e# Prepend the block to the string.
_ e# Duplicate the resulting array.
}_~
-
\$\begingroup\$ GolfScript isn't dead! Ilmari has an answer to a similar question that does this in exactly 10 bytes. \$\endgroup\$Justin– Justin2016年01月17日 09:19:19 +00:00Commented Jan 17, 2016 at 9:19
JavaScript, (削除) 62 (削除ここまで) (削除) 61 (削除ここまで) 37 bytes
Thanks to @Doᴡɴɢᴏᴀᴛ for the help!
Original [37 bytes]:
f=_=>'f='+'_'.repeat((10+f).length)+f
Child [74 bytes]:
f=______________________________________=>'f='+'_'.repeat((10+f).length)+f
Grandchild [148 bytes]:
f=________________________________________________________________________________________________________________=>'f='+'_'.repeat((10+f).length)+f
Alternate (with printing to console, and as a full program):
Original [61 bytes]:
f=_=>console.log(`f=${'_'.repeat((0+f).length+5)+f};f()`);f()
Child [122 bytes]:
f=______________________________________________________________=>console.log(`f=${'_'.repeat((0+f).length+5)+f};f()`);f()
Grandchild [244 bytes]:
f=________________________________________________________________________________________________________________________________________________________________________________________=>console.log(`f=${'_'.repeat((0+f).length+5)+f};f()`);f()
How it works!
1. f=_=> Define function f as console.log(...)
2. ;f() Run function f.
3. (in function f)
console.log(...)Print the following:f=literal text "f="${'_'.repeat((0+f).length+5)"_" repeated for the length of f, altered to account for characters not included in the stringification of f+f}The stringification of function f;f()literal text ";f()"
Notes
console.logis necessary instead ofalertbecausealertdoesn't seem to play well with really long strings (at least on my machine/browser configuration)- The
_'s are inserted into the name of the (unused) parameter of function f, to ensure that they are included in the stringification of f. - Main improvement (aside from getting rid of the
console.log) of the first solution over the second: adding10to the function instead of0to cast it to string makes it one byte longer, eliminating the need to add 1 to the length afterwards, saving a byte.
-
\$\begingroup\$
0+fshould also work to cast the function to a string \$\endgroup\$Downgoat– Downgoat2016年01月16日 01:41:48 +00:00Commented Jan 16, 2016 at 1:41 -
\$\begingroup\$ 48 bytes:
(f=_=>`(f=${'_'.repeat((0+f).length+5)+f})()`)()\$\endgroup\$Downgoat– Downgoat2016年01月16日 02:29:15 +00:00Commented Jan 16, 2016 at 2:29 -
\$\begingroup\$ @Doᴡɴɢᴏᴀᴛ Forgot that returning the result is usually acceptable. Will update. \$\endgroup\$jrich– jrich2016年01月16日 02:51:45 +00:00Commented Jan 16, 2016 at 2:51
-
\$\begingroup\$ @Doᴡɴɢᴏᴀᴛ Actually, since writing answers as functions is generally accepted, does the solution even have to call the function? \$\endgroup\$jrich– jrich2016年01月16日 02:53:56 +00:00Commented Jan 16, 2016 at 2:53
-
\$\begingroup\$ sure, you could do \$\endgroup\$Downgoat– Downgoat2016年01月16日 02:55:18 +00:00Commented Jan 16, 2016 at 2:55
Minkolang 0.15, (削除) 19 (削除ここまで) 14 bytes
"66*2-rIDdr$O.
Explanation
"66*2- $O. Standard quine formulation
r Reverse stack
I Push length of stack
D Pop n and duplicate top of stack n times
d Duplicate top of stack
r Reverse stack
What the bit in between rs does is duplicate the ending period enough times to fulfill the doubling criterion. . is the "stop program" character, so the many periods at the end do nothing except be there.
CJam, 12 bytes
{"_~"11ドル$}_~
When run, this will print
{"_~"11ドル$}_~{"_~"11ドル$}_~
which, in turn, will print
{"_~"11ドル$}_~{"_~"11ドル$}_~{"_~"11ドル$}_~{"_~"11ドル$}_~
and so on.
Python 3, 51 bytes
x=r"print('x=r\"'+x+'\"'+';exec(x*2)');";exec(x*2)
This includes a trailing newline.
Which outputs:
x=r"print('x=r\"'+x+'\"'+';exec(x*2)');";exec(x*2)
x=r"print('x=r\"'+x+'\"'+';exec(x*2)');";exec(x*2)
GolfScript, 11 bytes
{: ".~"]}.~
How the source code works
{: ".~"]}.~
{ } Define and push a code block.
.~ Push a copy and execute it.
: Save the code block in the space character.
Every subsequent space will now execute the code block.
".~" Push that string.
] Wrap the entire stack in an array.
If the above source code is executed once, the stack will end up as
["" {: ".~"]} ".~"]
where the empty string at the beginning corresponds to the initial state of the stack (empty input).
Two copies of the source code would leave a final state of
[["" {: ".~"]} ".~"] {: ".~"]} ".~"]
and so on.
What happens next
After executing the source code, the interpreter does the following:
It wraps the entire stack in an array, and pushes that array on the stack.
For a single copy of the source code, the stack now contains
["" {: ".~"]} ".~"] [["" {: ".~"]} ".~"]]It executed
putswith the intention of printing the wrapped stack, followed by a linefeed.putsis defined as{print n print}, so it does the following.printprints the wrapped up copy of the stack without inspecting it (i.e., without converting it to its string representation). This sends{: ".~"]}.~to STDOUT and pops the stack copy from the top of the stack.
The stack now contains
["" {: ".~"]} ".~"]executes the code block we defined previously.
:begins by saving["" {: ".~"]} ".~"]in the space character, then".~"pushes itself and]wraps the stack in an array.npushes a string consisting of a single linefeed.The stack now contains
[["" {: ".~"]} ".~"] ".~"] "\n"is executed once more. However, it was redefined when we called it for the first time and now contains an array, not a code block.
In fact, it pushes
["" {: ".~"]} ".~"], leaving the stack as[["" {: ".~"]} ".~"] ".~"] "\n" ["" {: ".~"]} ".~"]Finally,
printprints the topmost stack item without inspecting it, sending{: ".~"]}.~to STDOUT.
ESMin, 26 chars / 36 bytes
⟮ô`\u27ee⦃ᶈ0}\u27ef
`ď2)⟯
Note that there is a trailing newline.
Explanation
Standard quine: ⟮CX222+ᶈ0
Modifications:
- Use
ôfunction to output the quines all results instead last result (as done using implicit output) - Use
\u27ee⦃ᶈ0}\u27ef\nand close copy block to prevent conflicts with later copy blocks. - Use
ď2to repeat string twice.
Explore related questions
See similar questions with these tags.