Quote:
>I am trying to make a program that will solve magic squares.
This is really less of a Logo question than a math question!
You are proposing to find magic squares by setting up and solving
a system of eight equations (three horizontal, three vertical,
two diagonal) in ten variables. Since you have fewer equations
than variables, you know there will be lots of possible solutions.
(As there should, since there are lots of magic squares!) But
you want to constrain the solution by saying that each variable
must be a positive integer less than 10, or at least that's what
you want for the usual X=15 magic squares. Certainly you want
each variable to be an integer.
Systems of equations in which the solution values are limited to
integers are called "Diophantine equations". There are several
known methods for solving them, but they're all a bit complicated;
do some research at the library if you want to pursue this route.
But I wouldn't solve your original problem by using equations
at all! Instead I'd use *backtracking*: I'd try assigning
values to variables arbitrarily, and see if I get a consistent
result, and if not, change something. So the structure of the
program would be something like this:
for [A 1 9] [
for [B 1 9] [
if not (:A = :B) [
for [C 1 9] [
if (and (not :C = :A)
(not :C = :B)
(:A + :B + :C = 15)) [
for [D 1 9] [
... etc.
At each step you check all the conditions that must be satisfied by
the variables you know so far. If you get all the values assigned,
in the innermost loop, you print the results.
The structure of the program would be more elegant if you made all
the assignments first and then checked all the conditions:
for [A 1 9] [
for [B 1 9] [
for [C 1 9] [
...
for [I 1 9] [
if (and (none-equal :A :B :C :D :E :F :G :H :I)
(:A + :B + :C = 15)
(:D + :E + :F = 15)
...) [
print (list :A :B :C :D :E :F :G :H :I)]]]]...]]
and it could be made even more elegant by using recursion to assign
all the values instead of having nine FOR invocations. But this
version would run much too slowly, because it would generate and test
many unnecessary sets of values. For example, it'd try having all
nine variables equal to 1, then it'd try all but I equal to 1 and
I equal to 2, and so on.