#include
#include
#define f (sizeof(int))
int i,j,k,l,m,*t,*u,*v;
int r(x)int x;{return (rand()/256)%x;}
void p(x,y)int *x,y;{for(i=y;--i;k=x[i],x[i]=x[j],x[j]=k)j=r(i+1);}
void o(n,a,q,d)int n,d,*a,*q;
{
for(m=r(2)?n:1,t=q,l=n/2,i=n;
i--&&(i!=l||i--);
*(t++)=i);
for(p(q,n-1),q[n-1]=l,u=a,i=j=0;
i=n&&(j-=n));
for(p(q,n-1),u=a,m=n+1-m,i=0,j=n-1;
i=n&&(j-=n));
}
void s(n,a,q,d)int n,d,*a,*q;
{
for(q[1]=3-(*q=r(n)),q[3]=3-(q[2]=1^q[r(2)]),t=a,i=n,m=3*r(2);
i--;
t+=d)
for(j=n;
j--;
t++[0]=q[j^(i&m)^(i/2)]);
for(q[1]=3-(*q=r(n)),q[3]=3-(q[2]=1^q[r(2)]),t=a,i=n,m=3-m;
i--;
t+=d)
for(j=n;
j--;
t++[0]+=q[j^(i&m)^(i/2)]*4);
}
void e(n,a,q,d)int n,d,*a,*q;
{
int h,i,k,l,z,g=n/2;
void (*m)();
for((m=g!=4?g%2?o:e:s)(g,a,q,g+d),m(g,a+g,q,g+d),l=(n+d)*g,u=a,i=g;
i--;
u+=d)
for(j=n;
j--;
ji)
for(j=g;!q[--j]||(k--,q[z]=1,q[j]=0););
if(q[i])
if(h)h--;
else
for(j=g;q[--j]||j==z||(q[j]=1,q[i]=0););
else
if(h>i||(g==3&&h&&(k==2||i==z)))
for(j=g;!q[--j]||j==z||(h--,i==z&&k--,q[i]=1,q[j]=0););
for(t=q,j=g;j--;u++)
if(0[t++])z=*u,*u=u[l],u[l]=z;
}
for(h=k=g/2-g%2,i=g;i--;q[i]=i Ian Collier
Ian Collier
Oxford University
57 Wyndham Avenue (home address)
BOLTON
BL3 4LG
England
Judges' comments:
To use:
make imc
./imc INTEGER
Try:
./imc 3
./imc 3
./imc 4
./imc 4
./imc 6
This entry's algorithm is as magic as its output!
Selected notes from the author:
This program may be compiled with an ANSI or K&R compiler. A few
harmless warnings are displayed only if "gcc -Wall" is used.
This entry is really a set of library functions, but an example
main function has been added so that the library can be tested. If
the program appears too large, consider that functions o and s can
each be separated from the rest of the program (except for the short
functions p and r, which they require) and used alone. However, the
main part of the program is the function e, which does require all the
other functions (apart from main).
The program should be supplied with an integer parameter. If no
parameter or an invalid parameter is given, then 5 is assumed. The
maximum parameter is determined only by the amount of cpu time, virtual
memory and display (or file) space available.
SPOILER:
OK, so you have probably seen magic square printers before. But what
about one that deals with even sizes as well as odd ones, or one that
prints out a different one each time (or attempts to, at any rate)?
I have formatted the important functions o, s and e to show how useful
the word "for" is, and (in function e) how useful the words "if" and
"else" are. In fact, hardly anything happens that isn't in one of these
instructions. I did this in order to simplify the code - the algorithms
used to make the random squares are quite complex without being shrouded
in obfuscated code! :-) Incidentally, I was surprised to find out how
useful the exclusive-or operator was while writing function s.
Here are the descriptions of the functions in the library:
o(n,a,q,d): makes a magic square of order n when n is odd and at least 3.
s(n,a,q,d): makes a magic square of order n when n equals 4.
e(n,a,q,d): makes a magic square of order n when n is even and at least 6.
In the above, a (of type int*) points to an area of memory in which
the magic square will be stored and q (also of type int*) points to
an area of memory of length at least n*sizeof(int) bytes which can be
used as a work space. Both a and q must be allocated by the caller.
The integer parameter d, usually zero, indicates the length of a gap
which will be left between adjacent rows of the magic square.
The square which is returned will (or should) be a permutation of the
numbers 1 to n*n in which all rows and columns and the two diagonals
add up to n*(n*n+1)/2.
Copyright (c) 1994, Landon Curt Noll & Larry Bassel.
All Rights Reserved. Permission for personal, educational or non-profit use is
granted provided this this copyright and notice are included in its entirety
and remains unaltered. All other uses must receive prior permission in writing
from both Landon Curt Noll and Larry Bassel.