Jump to content
Science Forums

Codes


A23

Recommended Posts

what would happen if i let run a program that writes and run all possible program in the order ?

You’d find something similar, I’m pretty confident, to what Roger Penrose reported in his 1989 book The Emperor’s New Mind. I wrote a little bit about this in Resolving ambiguous meaning of “algorithm” & Penrose’s Turing machine treatment.

 

In short, what Penrose did was write a Universal Turing Machine – not too unusual, in the 1980s or now, though challenging – with a binary (0s and 1s) alphabet – not usual, but not too unusual, either – then see what it did when given an initial tape (for a UTM, this consists of both the program to run, and its initial data) of every integer starting with 1. Most of them, as you might expect, don’t do anything useful, but some do.

 

The approach of systematically trying every possible program to find one that does a specific thing – or simply anything interesting – is an enticing one – give enough time, it must write every possible program – but suffers from the basic problem that even on very fast and massively parallel computers, it takes an impractically long time. For instance, assume that the shortest version of program you want is a modest 8196 bits long, and you have 1 trillion ([imath]10^{12}[/imath]) computers each capable of running and checking to see if it’s interesting 1 program in [imath]10^{16}[/imath] seconds (corresponding to something like 10 petaflops, the fastest yet built), this approach would take [imath]2^{8196} / 10^{28} \dot= 10^{280}[/imath] s to find . As the universe is less than about 13 billion years (on the order of [imath]10^{17}[/imath] s) old, this is a mind-bogglingly long time.

 

So, in short, if you let a program that writes and runs all possible programs in sequential order, it would likely not write a very interesting program your lifetime. If you let it generate large programs randomly, it might, but would be very unlikely, to.

Link to comment
Share on other sites

It's a technical curiosity but it seems has no practical use. It's C :

#include<stdio.h>

#include<stdlib.h>

#include<strings.h>

#include<string.h>

#include<sys/wait.h>

 

int main(int, char**);

 

// Generate all possible programs : open file, char addition +1, close, try to run

 

int main(int argc, char **argv)

{

FILE *prog;

bool finish=false;

unsigned char c[20];

 

if(argc==1)

{

printf("Generate all possible programsn Usage : %s filenamen",argv[0]);

printf("Write programs in 'filename'n");

}

else if(argc==2)

{

//test if file argv[1]

prog=fopen(argv[1],"rb");

if(!prog)

{

printf("File %s does not exist.nCreate it empty (y/n) ? ",argv[1]);

scanf("%2s",&c);

if(c[0]=='y')

{

prog=fopen(argv[1],"wb");

char start=0;

fwrite((char *)&start,sizeof(char),1,prog);

char cmd[256]="chmod u+x ";

strcat(cmd,argv[1]);

strcat(cmd,"n");

system(cmd);

fclose(prog);

 

printf("File %s was created.n Re-run %s %s to begin.n",argv[1],argv[0],argv[1]);

exit(0);

}

}

else

{

fclose(prog);

while(!finish)

{

 

//execute file

 

printf("Execute %s.n",argv[1]);

char cmd[256]="./";

strcat(cmd,argv[1]);

strcat(cmd,"n");

wait((void *)system(cmd));

//open file, add +1

 

char rest=(char)1;

prog=fopen(argv[1],"r+b");

while(!feof(prog))

{

fread(&c[0],1,sizeof(c[0]),prog);

printf("%1d->",(int)c[0]);

if((int)rest==1&&(int)c[0]<=254)

{

c[0]=(char)((int)c[0]+1);

rest=0;

}

if((int)rest==1&&(int)c[0]>254)

{

c[0]=0;

rest=1;

}

 

printf("%1d ",(int)c[0]);

fseek(prog,-sizeof(char),SEEK_CUR);

fwrite((char *)&c[0],1,sizeof(c[0]),prog);

fread(&c[0],1,sizeof(c[0]),prog);

}

if(rest==1)

fwrite((char *)&rest,1,sizeof(rest),prog);

fclose(prog);

//finish=true;

}

}

}

}

Link to comment
Share on other sites

I dont know much about C, very little in fact, but there are other members who excel at this stuff. I was attempting to run your code in Visual studio 2008 because I am curious and a bunch of stuff doesnt work. What are the files you are trying to include at the top? strcat and wait don't seem to be valid commands. Are you using a certain c language like C#?

Link to comment
Share on other sites

not at all, visual studio is not quite iso compliant, and nor really is your code, but that aside, i don't see how that will generate "all the possible programs", you could make that a lot more efficient, because C code wording standard is defined fairly well... At this same rate you can write a function that will generate all the possible binaries, but it won't make it practical, nor will you, within your lifetime, find generate nearly enough programs, or be able to identify them to be able to say that you generated a useful program, a program that does something... that said, genetic algorithms and AI code is much, much more interesting and likely to generate useable programs for you, 1000 generations of self-evolving programs will take a few hours or maybe days to run, and a few months to go through, but you are many times more likely to find programs that actually do stuff, then what you propose above....

 

so let's not ponder this too much, and just write pretty code...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...