1
\$\begingroup\$

This question already has an answer here:

Your job is to write a program that rewrites and makes itself longer on each run of the program.

Challenge

The program will print Hello, World!\n (\n denotes a newline). Then, it will modify its own file so that the new program length is exactly 2x the length at runtime. This modified file when run, will print Hello, World!\nHello, World!\n. When run a third time, the file will print Hello, World!\nHello, World!\nHello, World!\nHello, World!\n.

More simply, on the nth run of the program, it will output 2^(n-1) instances of Hello, World!\n, and have a program length of 2^(n-1) the length of the original.

This is , so shortest code (before first run) wins. Please comment if you need something is not clear.

Edit: To get past the restriction of file-rewriting, your program may print the modified source along with the Hello, World!\ns at a +40% penalty.

\$\endgroup\$

marked as duplicate by LegionMammal978, Blue, cat, NoOneIsHere, Digital Trauma code-golf May 14 '16 at 15:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ "it will modify its own file" can you provide a way to circumvent this? \$\endgroup\$ – Leaky Nun May 14 '16 at 9:48
  • \$\begingroup\$ What do you mean by circumvent? \$\endgroup\$ – ericw31415 May 14 '16 at 9:49
  • \$\begingroup\$ Well, we can't exactly modify our own file in online compilers... \$\endgroup\$ – Leaky Nun May 14 '16 at 9:51
  • \$\begingroup\$ @KennyLau How about printing something that theoretically would work offline? \$\endgroup\$ – ericw31415 May 14 '16 at 10:02
  • 2
    \$\begingroup\$ @LegionMammal978 it's more like codegolf.stackexchange.com/q/69504/8478 I guess. \$\endgroup\$ – Martin Ender May 14 '16 at 12:32
0
\$\begingroup\$

PHP, 100

More efficient, 101

<?$a=$argv[0];$b=file_get_contents($a);file_put_contents($a,$b.substr($b,81,20));echo"Hello World\n";

This script is appending an echo"Hello World";, but because you said the size should also vary, and because it's shorter, I'm also having this script:

Shorter, 100

<?$a=$argv[0];$b=file_get_contents($a);file_put_contents($a,$b.substr($b,2,98));echo"Hello World\n";

This script is appending itself, in a way it adds one Hello World, but this version will overall take more space and takes longer to execute.

Example

But the scripts don't vary when it comes to the output:

root@raspberrypi:~/stack# php hello_expand.php
Hello World
root@raspberrypi:~/stack# php hello_expand.php
Hello World
Hello World
root@raspberrypi:~/stack# php hello_expand.php
Hello World
Hello World
Hello World
\$\endgroup\$
  • \$\begingroup\$ Umm, the third run of the program should have 4 Hello, Worlds!, not 3. \$\endgroup\$ – ericw31415 May 14 '16 at 16:19

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