Homework 14: Quines

A quine is a program that reproduces its source code as output. The trick is that the program can’t open itself for reading - it must produce output with no input and the output it produces must match itself.

For example:

$ gcc quine.c -o quine
$ ./quine > quine.out
$ diff quine.out quine.c

There should be no difference between the output and the source code.

Your task is to write two quines on your own.

The first quine should be simple - do what I’ve demonstrated above and write a program in your favorite language that produces itself as output (without reading its own code as input).

The second quine is trickier - write a program that produces as output a second program; that second program, when run, should produce the first program again as output.

For example:

$ gcc quine1.c -o quine1
$ ./quine1 > quine2.c
$ gcc quine2.c -o quine2
$ ./quine2 > quine3.c
$ diff quine1.c quine3.c

There should be no difference between quine1.c and quine3.c.

For bonus points, try a quine that repeats after 3 or more iterations.

Bring your solution to our May meeting.

Feel free to discuss the problem or any other topics you are interested in on our mailing list (accessible via our Meetup Group).

Provided Solutions

Frank W.