FoxFF

16 Reputation

5 Badges

13 years, 127 days

MaplePrimes Activity


These are replies submitted by FoxFF

Since I am not an advanced user, this code is far from being really efficient, but it works.

> restart; with(combinat); randomize();
> h, j, k, l, n, o, p, z := A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8];
> B := map(rhs, op(solve([a+b+c+d-z = 0, e+f+g+h-z = 0, i+j+k+l-z = 0, m+n+o+p-z = 0, a+e+i+m-z = 0, b+f+j+n-z = 0, c+g+k+o-z = 0, a+f+k+p-z = 0, d+g+j+m-z = 0], [a, b, c, d, e, f, g, i, m]))); a, b, c, d, e, f, g, i, m := B[1], B[2], B[3], B[4], B[5], B[6], B[7], B[8], B[9];
> do A := randcomb(16, 8); if nops({a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p}) = 16 then break end if end do;
> Matrix(4, 4, [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]);
 

Since I am not an advanced user, this code is far from being really efficient, but it works.

> restart; with(combinat); randomize();
> h, j, k, l, n, o, p, z := A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8];
> B := map(rhs, op(solve([a+b+c+d-z = 0, e+f+g+h-z = 0, i+j+k+l-z = 0, m+n+o+p-z = 0, a+e+i+m-z = 0, b+f+j+n-z = 0, c+g+k+o-z = 0, a+f+k+p-z = 0, d+g+j+m-z = 0], [a, b, c, d, e, f, g, i, m]))); a, b, c, d, e, f, g, i, m := B[1], B[2], B[3], B[4], B[5], B[6], B[7], B[8], B[9];
> do A := randcomb(16, 8); if nops({a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p}) = 16 then break end if end do;
> Matrix(4, 4, [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]);
 

I just realized that the code does not output a magic square. The required values are good, but they are listed in numerical order, which does not make a magic square.

I just realized that the code does not output a magic square. The required values are good, but they are listed in numerical order, which does not make a magic square.

Thanks for the simplification!

I had been searching for a long time for a way to reseed the random number generator. The only way I found was to not use restart and use unassign(anames(user)). And all that time there was a simple command to do just that!

Again, thanks a lot!

Thanks for the simplification!

I had been searching for a long time for a way to reseed the random number generator. The only way I found was to not use restart and use unassign(anames(user)). And all that time there was a simple command to do just that!

Again, thanks a lot!

Thanks a lot ! It works now.

I added some code since I needed the variables from a to p to be assigned. There is most probably a more efficient way, but I am not aware of it,

I also replaced restart with unassign(anames(user)) so that the results are different on each execution.

Here's my modified code

unassign(anames(user)); with(combinat);v := [h, j, k, l, n, o, p, z];
w := unapply(`union`(map(rhs, solve({a+b+c+d = z, a+e+i+m = z, a+f+k+p = z, b+f+j+n = z, c+g+k+o = z, d+g+j+m = z, e+f+g+h = z, i+j+k+l = z, m+n+o+p = z}, {a, b, c, d, e, f, g, i, m})), {v[]}), v);
do A := randcomb(100, 8); if nops(w(A[])) = 16 then break end if end do;
x := w(A[]);
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p := x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11], x[12], x[13], x[14], x[15], x[16];
matrix(4, 4, [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]);

This code creates a random 4x4 m a g i c  square.

Thanks again!

Thanks a lot ! It works now.

I added some code since I needed the variables from a to p to be assigned. There is most probably a more efficient way, but I am not aware of it,

I also replaced restart with unassign(anames(user)) so that the results are different on each execution.

Here's my modified code

unassign(anames(user)); with(combinat);v := [h, j, k, l, n, o, p, z];
w := unapply(`union`(map(rhs, solve({a+b+c+d = z, a+e+i+m = z, a+f+k+p = z, b+f+j+n = z, c+g+k+o = z, d+g+j+m = z, e+f+g+h = z, i+j+k+l = z, m+n+o+p = z}, {a, b, c, d, e, f, g, i, m})), {v[]}), v);
do A := randcomb(100, 8); if nops(w(A[])) = 16 then break end if end do;
x := w(A[]);
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p := x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11], x[12], x[13], x[14], x[15], x[16];
matrix(4, 4, [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]);

This code creates a random 4x4 m a g i c  square.

Thanks again!

Thank you so much! You just save me a couple of hours of repeatedly typing fg().

 

Thank you so much! You just save me a couple of hours of repeatedly typing fg().

 
Page 1 of 1