Fibonacci numbers "the other way"

Posted on January 24, 2011 by phimuemue

Every programmer comes to the point where he or she must/should/wants to implement a programm for calculating fibonacci numbers. But I think my way is quite a new one.

I put the calculation into a recursive quine, that first reproduces its own source code (except some modified constants for calculation) to a file (and that one can see something to the screen also), compiles the new source code, runs the new program. The new program reproduces its own source code (except some modified constants for calculation) to a file (and that one can see something to the screen also), compiles the new new source code, runs the new new program. The new new program reproduces ... (and so on).

The program is written in C and tested under Ubuntu 10.04. I suppose it does not work on windows. If anybody has tried it, plase write me a short note if it worked or not. (I know that it can be made working with some little effort.)

So here it is (please excuse the improper formatting. Try using the "view source" button appearing in the source code field when mouse is over the field.

#include<stdio.h>
#define N 0
#define FNMINUSONE 1
#define FNMINUSTWO 0
char*c="#include<stdio.h>%c#define N 0%c#define FNMINUSONE %d%c#define FNMINUSTWO %d%cchar*c=%c%s%c;%cint main(int argc, char*argv[]){%c  int n=atoi(argv[1]);FILE*f=fopen(%cfibo.c%c,%cw%c);%c  printf(c,10,10,FNMINUSONE+FNMINUSTWO,10,FNMINUSONE,10,34,c,34,10,10,34,34,34,34,10,10,34,34,34,34,10,10);fprintf(f,c,10,10,FNMINUSONE+FNMINUSTWO,10,FNMINUSONE,10,34,c,34,10,10,34,34,34,34,10,10,34,34,34,34,10,10);%c  fclose(f);if(n>1){system(%cgcc -o fibo fibo.c%c); char s[80];sprintf(s,%c./fibo %%d%c,n-1);system(s);}%c}%c";
int main(int argc, char*argv[]){
  int n=atoi(argv[1]);FILE*f=fopen("fibo.c","w");
  printf(c,10,10,FNMINUSONE+FNMINUSTWO,10,FNMINUSONE,10,34,c,34,10,10,34,34,34,34,10,10,34,34,34,34,10,10);fprintf(f,c,10,10,FNMINUSONE+FNMINUSTWO,10,FNMINUSONE,10,34,c,34,10,10,34,34,34,34,10,10,34,34,34,34,10,10);
  fclose(f);if(n>1){system("gcc -o fibo fibo.c"); char s[80];sprintf(s,"./fibo %d",n-1);system(s);}
}

Pierce Brown sent me a nice illustration of the fibonacci numbers that can be seen over here. Thanks for the hint.