2
\$\begingroup\$

Create a self-replicating Hello World Program. (The program has the ability to use the compiler, if you want).

For example:

void main()
{
    printf("Hello, World!");
    DuplicateFile("main.exe", "main2.exe");
}

Alternate challenge: A "quine" Hello World Program.

\$\endgroup\$
  • \$\begingroup\$ Whose computer do you suggest the binary be prepared for?!? And who do you suppose is going to run a batch of executable they simply downloaded from the internet?!? \$\endgroup\$ – dmckee --- ex-moderator kitten Apr 30 '11 at 1:48
  • \$\begingroup\$ @dmckee I meant to post the source code. When compiled, it should make a self replicating program. \$\endgroup\$ – Mateen Ulhaq Apr 30 '11 at 1:54
  • \$\begingroup\$ Ah!. I think I can show you an interpreted program that meets the spec if I have time. At least on a unix system where scripts can be executable. \$\endgroup\$ – dmckee --- ex-moderator kitten Apr 30 '11 at 2:05
  • 2
    \$\begingroup\$ Do you mean a quine? Or a program that copies its file on disk? \$\endgroup\$ – Casey Apr 30 '11 at 4:42
  • 6
    \$\begingroup\$ This is neither well-specified nor a particular interesting task, imho. −1. \$\endgroup\$ – Joey Apr 30 '11 at 7:05
13
\$\begingroup\$

Bash - 43

echo $(echo Hello, World! >&2)$BASH_COMMAND

This prints "Hello, World!" on standard error, and itself on standard output.

For fun:

$ ./replicate-hello.bash
Hello, World!
echo $(echo Hello, World! >&2)$BASH_COMMAND
$ ./replicate-hello.bash | bash
Hello, World!
Hello, World!
echo $(echo Hello, World! >&2)$BASH_COMMAND
$ ./replicate-hello.bash | bash | bash
Hello, World!
Hello, World!
Hello, World!
echo $(echo Hello, World! >&2)$BASH_COMMAND
$ bash <<< $(echo ./replicate-hello.bash; \
             for ((i=0;i<100;i++)) ; do echo "| bash"; done)
Hello, World!
Hello, World!
Hello, World!
Hello, World!
... snip ...
Hello, World!
Hello, World!
Hello, World!
echo $(echo Hello, World! >&2)$BASH_COMMAND
\$\endgroup\$
  • \$\begingroup\$ lol nice recursion & simplicity +1 \$\endgroup\$ – Garet Claborn May 4 '11 at 7:47
4
\$\begingroup\$

PHP, 13

Hello, world!

The only way to have a program that prints both "Hello, world!" and itself is this.

Edit: note that the question was not quite as clear when I wrote this.

\$\endgroup\$
4
\$\begingroup\$

Windows Batch file

@echo Hello World
@copy "%~dpnx0" "%~dpn02%~x0" >nul 2>&1
\$\endgroup\$
3
\$\begingroup\$

Erlang 1342 chars, 3 lines

Not sure if I've understood the question correctly here, I ended up writing a quine that writes to another file...

-module(hi). 
-export([h/0]).
h()->io:fwrite("Hello World!"),P=[45,109,111,100,117,108,101,40,104,105,41,46,10,45,101,120,112,111,114,116,40,91,104,47,48,93,41,46,10,104,40,41,45,62,105,111,58,102,119,114,105,116,101,40,34,72,101,108,108,111,32,87,111,114,108,100,33,34,41,44,80,61],Q=[44,102,105,108,101,58,119,114,105,116,101,95,102,105,108,101,40,34,104,105,50,46,101,114,108,34,44,80,41,44,102,105,108,101,58,119,114,105,116,101,95,102,105,108,101,40,34,104,105,50,46,101,114,108,34,44,105,111,95,108,105,98,58,102,119,114,105,116,101,40,34,126,119,34,44,91,80,93,41,44,91,97,112,112,101,110,100,93,41,44,102,105,108,101,58,119,114,105,116,101,95,102,105,108,101,40,34,104,105,50,46,101,114,108,34,44,32,34,44,81,61,34,44,91,97,112,112,101,110,100,93,41,44,102,105,108,101,58,119,114,105,116,101,95,102,105,108,101,40,34,104,105,50,46,101,114,108,34,44,105,111,95,108,105,98,58,102,119,114,105,116,101,40,34,126,119,34,44,91,81,93,41,44,91,97,112,112,101,110,100,93,41,44,102,105,108,101,58,119,114,105,116,101,95,102,105,108,101,40,34,104,105,50,46,101,114,108,34,44,81,44,91,97,112,112,101,110,100,93,41,46],file:write_file("hi2.erl",P),file:write_file("hi2.erl",io_lib:fwrite("~w",[P]),[append]),file:write_file("hi2.erl", ",Q=",[append]),file:write_file("hi2.erl",io_lib:fwrite("~w",[Q]),[append]),file:write_file("hi2.erl",Q,[append]).

I'm currently learning Erlang, so it was a good learning experience anyway.

\$\endgroup\$
1
\$\begingroup\$

Javascript, 89 bytes

I'm not quite sure if this is what you're asking, but this program has "hello world" in it and outputs itself.

//hello world
var f=function (){return "//hello world\nvar f="+f.toString()+";f();"};f();
\$\endgroup\$
1
\$\begingroup\$

Java (1045)

I've done this before.

import java.io.*;
import javax.tools.*;
class H1 {
  static final char Q=34;
  static final int N=1;
  public static void main(String[]a)throws Exception{
    String fn="H"+(N+1)+".java";
    BufferedWriter out=new BufferedWriter(new FileWriter(fn));
    String s="import java.io.*;import javax.tools.*;class H%d{static final char Q=34;static final int N=%d;public static void main(String[]a)throws Exception{String fn=%cH%c+(N-1)+%c.java%c;BufferedWriter out=new BufferedWriter(new FileWriter(fn));String s=%c%s%c;out.write(String.format(s,N+1,N+1,Q,Q,Q,Q,Q,s,Q,Q,Q,Q,Q));out.close();JavaCompiler c=ToolProvider.getSystemJavaCompiler();c.run(null,null,null,fn);Class.forName(%cH%c+(N+1)).getMethod(%cmain%c,a.getClass()).invoke(null,new Object[]{new String[]{}});}}";
    out.write(String.format(s,N+1,N+1,Q,Q,Q,Q,Q,s,Q,Q,Q,Q,Q));
    out.close();
    JavaCompiler c=ToolProvider.getSystemJavaCompiler();
    c.run(null,null,null,fn);
    Class.forName("H"+(N+1)).getMethod("main",a.getClass()).invoke(null,new Object[]{new String[]{}});
  }
}

It floods the directory with H*.java and H*.class files. It somehow stops by itself with a huge stack trace after 500 compilations. I don't know how to fix that problem.

Here is what a flooded desktop looks like:

https://www.dropbox.com/s/iy8ggzps7s55z4e/java2.png?dl=0 https://www.dropbox.com/s/jrauw3igjiwrpmd/java1.png?dl=0

\$\endgroup\$

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