Created
May 12, 2011 06:19
-
-
Save Ming-Tang/968033 to your computer and use it in GitHub Desktop.
Factorial quine program (outputs its own source code with variables changed, compiles new source, repeat)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import javax.tools.*; | |
class Factorial12 { | |
static final char Q=34; | |
static final int N=12; | |
static final int M=1; | |
public static void main(String[]a)throws Exception{ | |
if (N == 0) { | |
System.out.println(M); | |
System.exit(0); | |
} | |
String fn="Factorial"+(N-1)+".java"; | |
BufferedWriter out=new BufferedWriter(new FileWriter(fn)); | |
String s="import java.io.*;import javax.tools.*;class Factorial%d{static final char Q=34;static final int N=%d;static final int M=%d;public static void main(String[]a)throws Exception{if(N==0){System.out.println(M);System.exit(0);}String fn=%cFactorial%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,N*M,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(%cFactorial%c+(N-1)).getMethod(%cmain%c,a.getClass()).invoke(null,new Object[]{new String[]{}});}}"; | |
out.write(String.format(s,N-1,N-1,N*M,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("Factorial"+(N-1)).getMethod("main",a.getClass()).invoke(null,new Object[]{new String[]{}}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment