Jump to content
BrainDen.com - Brain Teasers
  • 0
Sign in to follow this  
Prime

Question

Write a program that writes an exact copy of itself without using any outside references.

You can specify your own programming language.

It can define and use variables, assignment, and arithmetic: X = Y + 5, or X = "Myself".

It can use decision statements, such as IF, ELSE, SWITCH.

It can use any sequence of execution controls, like GOTO, or loops like DO WHILE, DO UNTIL, REPEAT x TIMES.

It can use WRITE, or PRINT to produce the output (its own text).

However, it cannot READ, nor it can refer to an absolute, or any outside address in memory. For that matter, let's prohibit the use of pointer variables.

No solutions over 10 lines long, please.

Perhaps, it is harder for people unfamiliar with computer programming to solve the problem. But I think, most anyone would easily understand the correct solution.

Share this post


Link to post
Share on other sites

Recommended Posts

  • 0
Guest
Write a program that writes an exact copy of itself without using any outside references.

You can specify your own programming language.

It can define and use variables, assignment, and arithmetic: X = Y + 5, or X = "Myself".

It can use decision statements, such as IF, ELSE, SWITCH.

It can use any sequence of execution controls, like GOTO, or loops like DO WHILE, DO UNTIL, REPEAT x TIMES.

It can use WRITE, or PRINT to produce the output (its own text).

However, it cannot READ, nor it can refer to an absolute, or any outside address in memory. For that matter, let's prohibit the use of pointer variables.

No solutions over 10 lines long, please.

Perhaps, it is harder for people unfamiliar with computer programming to solve the problem. But I think, most anyone would easily understand the correct solution.

START;

x = "START;  PRINT \"x=\\\"\" . x . \"\\\"\"; PRINT x; END; ";


PRINT "x=\"" . x . "\"";

PRINT x;

END;

Edited by taliesin

Share this post


Link to post
Share on other sites
  • 0
Guest

START;

WRITE "an exact copy of itself without using any outside references.";

END;

Edited by GIJeff

Share this post


Link to post
Share on other sites
  • 0
Guest

#include<stdio.h>

main()

{

printf("an exact copy of itself without using any outside references");

}

I didn't understand your question so i printed the same sentence. dunno if that's an answer.

Share this post


Link to post
Share on other sites
  • 0

START;

x = "START;  PRINT \"x=\\\"\" . x . \"\\\"\"; PRINT x; END; ";


PRINT "x=\"" . x . "\"";

PRINT x;

END;

Your program starts with the word START, whereas the first thing on the printout is x=

That's not an exact copy.

Share this post


Link to post
Share on other sites
  • 0
#include<stdio.h>

main()

{

printf("an exact copy of itself without using any outside references");

}

I didn't understand your question so i printed the same sentence. dunno if that's an answer.

If your program were correct, it would print everything you wrote inside the spoiler exactly.

As is, its output would be missing the words #include<stdio.h>, main(), printf, not to mention all those quotes and brakets.

Share this post


Link to post
Share on other sites
  • 0
Guest
If your program were correct, it would print everything you wrote inside the spoiler exactly.

As is, its output would be missing the words #include<stdio.h>, main(), printf, not to mention all those quotes and brakets.

oh..i get it now...the output should include the whole program -_-

jeez...that's difficult i guess.... :o

btw I am not a programmer...learning C as it is included in the syllabus :D

Share this post


Link to post
Share on other sites
  • 0
Guest

I am aware of the fact that no pointer variables are allowed, but here is an interesting method:

If I write my code in the file main.cs (C# or any extension)

Then I will write the code to open the file and dump it onto a console (command line).

Sorry, too busy to write the code..... but I think I pointed out my concept...

Write the code and just copy/paste the code in a print statement.

When it executes, it will print the statement which consists of the entire code.

Share this post


Link to post
Share on other sites
  • 0
Guest

Specify a programming language that has a special operator '$' which if passed as parameter to a function is replaced by a string formed by appending the name of the function, a space and the '$' symbol

so in this programming language using print $ will produce the output print $

Share this post


Link to post
Share on other sites
  • 0
Specify a programming language that has a special operator '$' which if passed as parameter to a function is replaced by a string formed by appending the name of the function, a space and the '$' symbol

so in this programming language using print $ will produce the output print $

If we go that route, why not define PRINTI statement, which can read our intentions and print what we meant? The suggestion to specify your own language was meant to make the problem more general and suitable to non-programmers. The language must not be geared specifically to solving this problem.

Assume, PRINT statement simply prints the argument(s) immediately following it. Treat PRINT as a language statement, not a function. Variables should be of traditional kind too. They should allow assisgnment of different values. All constants must be defined within the program.

Although your solution would produce the output as required, I have to reject it, because the programming language used is extraordinarily specialized. And the "$" variable in your language amounts to a pointer (pointing to the name of the function), or an outside reference, which were explicitly prohibited. :)

Share this post


Link to post
Share on other sites
  • 0
Guest
Your program starts with the word START, whereas the first thing on the printout is x=

That's not an exact copy.

Good point

x = "PRINT \"x=\\\"\" . x . \"\\\"\"; PRINT x; ";

PRINT "x=\"" . x . "\"";

PRINT x;

Share this post


Link to post
Share on other sites
  • 0
Good point

x = "PRINT \"x=\\\"\" . x . \"\\\"\"; PRINT x; ";

PRINT "x=\"" . x . "\"";

PRINT x;

There is a correct solution somewhere there, hiding deep in the forest of backslashes. All essential elements are there. Printing head and tail of the program and avoiding infinite regress.

Alas, it is not easy to understand for non-programming folk and for programmers as well. And it does not seem to produce an exact copy, on the account of those backslashes and quotes.

If I understand right, backslash is an "escape" character allowing to assigh values of special characters to variables.

Then I see the output of the program as following:

x = "PRINT "x=\"".x. "\""; PRINT x; and so on. If we compare that to the first line of the program, we find some slashes are missing.

Let me try and make a simplifed form of that solution.

Other than language statements and operators, such as PRINT, or + * =, things appearing inside quotes are literal values, non-quoted names could be names of variables. Thus "x" is anctual letter, whereas x is a variable, which may contain text and/or numbers.

Taliesin, first defined variable x and assigned it a value, then used it in PRINT statement twice.

x = "PRINT x = x PRINT x" (the entire text inside quotes is a value assigned to x)

PRINT "x = " x (print characters x = followed by contents of the variable x)

PRINT x (print contents of the variable x)

Thus the otput from the two PRINT commands would be:

x = PRINT x = x PRINT x

PRINT x = x PRINT x

(Assuming, each PRINT sends its output to a new line.)

As we can see, the output is identical to the original program with exception of missing "" and skipping to a new line (lines 2 and 3 of the original program printed on the same line).

The line issue is no big deal. However, missing quotes render the new copy of the original program unexecutable, incapable of producing the same result again.

Statement delimeter issue and differentiating literal values from variable names by use of quotes, are difficult technical problems to overcome. Ohter than that, the above program has solved the "puzzle" portion of the problem.

Minding all those technical difficulties with quotes and statement delimeters, let me narrow down the OP some. Let's write a program, which makes an exact copy of itself WITHOUT USE OF VARIABLES as well. B))

Share this post


Link to post
Share on other sites
  • 0
Guest
There is a correct solution somewhere there, hiding deep in the forest of backslashes. All essential elements are there. Printing head and tail of the program and avoiding infinite regress.

Alas, it is not easy to understand for non-programming folk and for programmers as well. And it does not seem to produce an exact copy, on the account of those backslashes and quotes.

If I understand right, backslash is an "escape" character allowing to assigh values of special characters to variables.

Then I see the output of the program as following:

x = "PRINT "x=\"".x. "\""; PRINT x; and so on. If we compare that to the first line of the program, we find some slashes are missing.

Let me try and make a simplifed form of that solution.

Other than language statements and operators, such as PRINT, or + * =, things appearing inside quotes are literal values, non-quoted names could be names of variables. Thus "x" is anctual letter, whereas x is a variable, which may contain text and/or numbers.

Taliesin, first defined variable x and assigned it a value, then used it in PRINT statement twice.

x = "PRINT x = x PRINT x" (the entire text inside quotes is a value assigned to x)

PRINT "x = " x (print characters x = followed by contents of the variable x)

PRINT x (print contents of the variable x)

Thus the otput from the two PRINT commands would be:

x = PRINT x = x PRINT x

PRINT x = x PRINT x

(Assuming, each PRINT sends its output to a new line.)

As we can see, the output is identical to the original program with exception of missing "" and skipping to a new line (lines 2 and 3 of the original program printed on the same line).

The line issue is no big deal. However, missing quotes render the new copy of the original program unexecutable, incapable of producing the same result again.

Statement delimeter issue and differentiating literal values from variable names by use of quotes, are difficult technical problems to overcome. Ohter than that, the above program has solved the "puzzle" portion of the problem.

Minding all those technical difficulties with quotes and statement delimeters, let me narrow down the OP some. Let's write a program, which makes an exact copy of itself WITHOUT USE OF VARIABLES as well. B))

can't be done, I cant prove this but I have a very strong belief

Share this post


Link to post
Share on other sites
  • 0

Ok, here's one in awk:

<BEGIN>

if ($0 > 0) {

printf $0;

}

<END>

The caveat is, that it has to be executed with a specific command:

awk -f file.awk file.awk > result.awk

Share this post


Link to post
Share on other sites
  • 0
Ok, here's one in awk:

<BEGIN>

if ($0 > 0) {

printf $0;

}

<END>

The caveat is, that it has to be executed with a specific command:

awk -f file.awk file.awk > result.awk

That does not print its copy. The command line is not shown, nor it would be printed.

The OP stated: no references outside the program and no use of pointers. Specifically, it said no file reading. Awk is based on file reading.

I'm afraid this puzzle is misunderstood by computer programmers. The problem does not ask to look for some programming language with implied file reading and/or implied pointers.

What problem asks is basically this:

Suppose you have PRINT argument(s) command, wich prints the arguments that follow it. How do you write a program that would print an exact copy of itself? The output should include the name of the command PRINT and all the argumets that followed it. The difficulty is that argument(s) can not be the same as PRINT argument(s). The command name PRINT is on the outside of its arguments.

The correct solution must be understandable to a person not familiar with computer programming.

Share this post


Link to post
Share on other sites
  • 0
Guest

I think cheating is the best way to solve this problem. This program works in several languages, although I wrote it in C++.

Share this post


Link to post
Share on other sites
  • 0
I think cheating is the best way to solve this problem. This program works in several languages, although I wrote it in C++.

Share this post


Link to post
Share on other sites
  • 0
Can you just do a screen dump?

Although I haven't done any computer programming since BASIC (and not visual basic, I'm probably dating myself)

let x = 1

if x = 1 than print screen

That's clearly a reference to outside the program. Besides, how can a program "be sure" about what's on the screen?

All I am asking for is a self-replicating software. A basic building block of a new digital life form, future civilization... It must be simple. The newly created copy must be able to repeat the same.

Do regular live organisms rely on their copy or reflection stored somewhere for reproduction, or do they carry their own "blueprint"?

Share this post


Link to post
Share on other sites
  • 0
Guest

Define a language with the following construct "do(n)"

do(n) executes the statement following it n times.

Let the print statement be defined such that it prints anything following it on the same line as it is.

A space starts a new statment unless it appears after the print statement, otherwise a newline should be used to start a new statment.

a self reproducing program in this language is as follows

do(2) print do(2) print

Edited by vimil

Share this post


Link to post
Share on other sites
  • 0

Define a language with the following construct "do(n)"

do(n) executes the statement following it n times.

Let the print statement be defined such that it prints anything following it on the same line as it is.

A space starts a new statment unless it appears after the print statement, otherwise a newline should be used to start a new statment.

a self reproducing program in this language is as follows

do(2) print do(2) print

That would do(it)! The solution is almost identical to the one I had in mind. The copy is ready to run and produce more copies. B))

Share this post


Link to post
Share on other sites
  • 0
Guest

Define a language with the following construct "do(n)"

do(n) executes the statement following it n times.

Let the print statement be defined such that it prints anything following it on the same line as it is.

A space starts a new statment unless it appears after the print statement, otherwise a newline should be used to start a new statment.

a self reproducing program in this language is as follows

do(2) print do(2) print

Congratulations, vimil - you've just launched Skynet.

If I ever find myself suspended in a fluid tank living in a virtual reality world while robots violate the law of conservation of energy, I'll know who to blame (you).

Alternatively, if I'm ever getting chased around a Wild-West themed amusement park by a Yul Brynner robot, rest assured that I'll be cursing your name.

Share this post


Link to post
Share on other sites
  • 0

awesome vimil!

though if we're still sticking to the OP, what about "start" and "end"? Nobody's program has replicated those yet, and if they're required on the first and they're required on the copies, right? I have a hunch that it's impossible to include those two lines, or any other lines (ie, the actual virus part of a virus), though I could be wrong

if we can make up our own functions, how about printline(n), where n is the program line? :P

1: start

2: for x = 1 to 5

3: printline(x)

4: next x

5: end

hehe ;D

Edited by unreality

Share this post


Link to post
Share on other sites
  • 0
awesome vimil!

though if we're still sticking to the OP, what about "start" and "end"? Nobody's program has replicated those yet, and if they're required on the first and they're required on the copies, right? I have a hunch that it's impossible to include those two lines, or any other lines (ie, the actual virus part of a virus), though I could be wrong

if we can make up our own functions, how about printline(n), where n is the program line? :P

1: start

2: for x = 1 to 5

3: printline(x)

4: next x

5: end

hehe ;D

"x" is a pointer, if I ever saw one. It points to the relative address of a line. At the very least it is a variable.

Let's take this new challenge and try to complement our program with head and tail. For without tail, no life form is complete.

I'll specify conditions in the following post. :)

Edited by Prime

Share this post


Link to post
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
Answer this question...

×   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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...