Skip navigation.
Home

Self-writing program

Folks,\\There is an interesting programming problem that was one of the coolest puzzles I took in CS courses. It is about whether you can write a program that writes itself upon execution!\\That is, if the source code is something like that (pseudo-code) {{{ Begin Print(“Something ……”) End }}} The output after execution should be exactly that same! {{{ Begin Print(“Something ……”) End }}} Now, java gurus! who’s gonna provide us with a java program that writes itself? :) \\ Note that the program should not accept any inputs (reading some file or accepting user inputs)

Here it is

Here it is: Instructions *Creat a java file named SelfWriter.java, and paste the following line on it *compile using JDK 1.5, for the printf function *solution is in one line, to save some \n :D *after execution, copy the output to the java file under that line, to compare {{{ package myPack;public class SelfWriter {public static void main(String[]args){StringBuffer a = new StringBuffer();System.out.printf(a.append("package myPack;public class SelfWriter {public static void main(String[]args){StringBuffer a = new StringBuffer();System.out.printf(a.append(%c%s%c).toString(),34,a,34);}}").toString(),34,a,34);}} }}} ------------- Amr Kourany Software Engineer IBM WTC, Egypt Branch

the code provided seems to

the code provided seems to be okay but the explanation is not!\\ What do you mean by "to save some \n!"

Anthor Version

Here is a version with new lines, as u see, the printf is full of \n and \t, and that's what i wanted to save, beside comparasion now is quiet difficult. {{{ package myPack; public class SelfWriter { public static void main(String[]args) { StringBuffer a = new StringBuffer(); System.out.printf(a.append("package myPack;%cpublic class SelfWriter {%c%cpublic static void main(String[]args) {%c%c%cStringBuffer a = new StringBuffer();%c%c%cSystem.out.printf(a.append(%c%s%c).toString(),13,13,9,13,9,9,13,9,9,34,a,34,13,9,13);%c%c}%c}").toString(),13,13,9,13,9,9,13,9,9,34,a,34,13,9,13); } } }}} ------------- Amr Kourany Software Engineer IBM WTC, Egypt Branchb

I got your point now.

I got your point now.\\ Okay now what is the minimum number of code (in terms of chars) that would do that?

May be...

We can get ride of the StringBuffer and make use of args, but there must be at least one argument passed to the program. code will be like this {{{ public class myClass { public static void main(String[]args) { System.out.printf(args[0]=new String("public class SelfWriter {%c%cpublic static void main(String[]args) {%c%c%System.out.printf(args[0]=new String(%c%s%c).toString(),13,9,13,9,9,13,9,9,34,args[0],34,13,9,13);%c%c}%c}").toString(),13,9,13,9,9,13,9,9,34,args[0],34,13,9,13); } } }}} and u will use the default package and of course u wil get ride of all the \n \t ------------- Amr Kourany Software Engineer IBM WTC, Egypt Branchb

replacing StringBuffer a

replacing StringBuffer a with args[0] won't decrease the size becasue you have to replace every a with args[0] \\ Removing the package is quite good. Can't you remove the public too?\\ Now we have Amr's solution with 322 bytes code.\\ Can you do better guys ?

Smaller Version

This version is 201 bytes only {{{ class S{public static void main(String[]x){String a;System.out.printf(a=new String("class S{public static void main(String[]x){String a;System.out.printf(a=new String(%c%s%c),34,a,34);}}"),34,a,34);}} }}} ------------- Amr Kourany Software Engineer IBM WTC, Egypt Branchb

Perfect! The file is even

Perfect! \\ The file is even 200 bytes! This is better than the shortest class that i am aware of.\\ Those types of programs are called Quines. A Quine \\"http://en.wikipedia.org/wiki/Quine" is a program that produces its\\ complete source code as its only output.\\ Any programming language that is turing complete \\"http://en.wikipedia.org/wiki/Turing-complete" which is able \\ to output any string has a quine program.\\ For example it has been proved that XSLT 1.0 is turing complete \\ "http://www.unidex.com/turing/utm.htm" thus there should be some quine \\xsl "http://w3log.server-wg.de/000016.html" \\