Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
How about accessing the file by reading itself/accessing the place where the code is contained? – Cᴏɴᴏʀ O'Bʀɪᴇɴ Jan 15 at 23:36
    
@CᴏɴᴏʀO'Bʀɪᴇɴ that's not allowed as that would violate rules I believe. – Downgoat Jan 15 at 23:37
1  
@Doᴡɴɢᴏᴀᴛ the quine rules are recommended by the tag wiki but don't apply automatically – Martin Ender Jan 16 at 0:27
1  
2  
Related. Related. Related. – Martin Ender Jan 16 at 11:39
up vote 11 down vote accepted

CJam, 10 bytes

{"_~"+_}_~

Test it here.

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.
}_~
share|improve this answer
    
GolfScript isn't dead! Ilmari has an answer to a similar question that does this in exactly 10 bytes. – Justin Jan 17 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.log is necessary instead of alert because alert doesn'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: adding 10 to the function instead of 0 to cast it to string makes it one byte longer, eliminating the need to add 1 to the length afterwards, saving a byte.
share|improve this answer
    
0+f should also work to cast the function to a string – Downgoat Jan 16 at 1:41
    
48 bytes: (f=_=>`(f=${'_'.repeat((0+f).length+5)+f})()`)() – Downgoat Jan 16 at 2:29
    
@Doᴡɴɢᴏᴀᴛ Forgot that returning the result is usually acceptable. Will update. – jrich Jan 16 at 2:51
    
@Doᴡɴɢᴏᴀᴛ Actually, since writing answers as functions is generally accepted, does the solution even have to call the function? – jrich Jan 16 at 2:53
    
sure, you could do – Downgoat Jan 16 at 2:55

Minkolang 0.15, 19 14 bytes

"66*2-rIDdr$O.

Original, child, grandchild.

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.

share|improve this answer

CJam, 12 bytes

{"_~"1$1$}_~

When run, this will print

{"_~"1$1$}_~{"_~"1$1$}_~

which, in turn, will print

{"_~"1$1$}_~{"_~"1$1$}_~{"_~"1$1$}_~{"_~"1$1$}_~

and so on.

Try it online!

share|improve this answer

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)
share|improve this answer

GolfScript, 11 bytes

{: ".~"]}.~

Try it online!

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:

  1. 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

    ["" {: ".~"]} ".~"] [["" {: ".~"]} ".~"]]
    
  2. It executed puts with the intention of printing the wrapped stack, followed by a linefeed.

    puts is defined as {print n print}, so it does the following.

    1. print prints 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

      ["" {: ".~"]} ".~"]
      
    2. executes the code block we defined previously.

      : begins by saving ["" {: ".~"]} ".~"] in the space character, then ".~" pushes itself and ] wraps the stack in an array.

    3. n pushes a string consisting of a single linefeed.

      The stack now contains

      [["" {: ".~"]} ".~"] ".~"] "\n"
      
    4. 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" ["" {: ".~"]} ".~"]
      
    5. Finally, print prints the topmost stack item without inspecting it, sending

      {: ".~"]}.~
      

      to STDOUT.

share|improve this answer

𝔼𝕊𝕄𝕚𝕟, 26 chars / 36 bytes

⟮ô`\u27ee⦃ᶈ0}\u27ef
`ď2)⟯

Try it here (Firefox only).

Note that there is a trailing newline.

Explanation

Standard quine: ⟮ⒸⅩ222+ᶈ0

Modifications:

  • Use ô function to output the quines all results instead last result (as done using implicit output)
  • Use \u27ee⦃ᶈ0}\u27ef\n and close copy block to prevent conflicts with later copy blocks.
  • Use ď2 to repeat string twice.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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