Magic Square pseudo-code
static public void magicSquare(int b[][]) {
int r= 0; int c= 0; int i; int size= b.length;
zeroOut(b);
// Make up the magic square
r= 0; c= size/2; i= 1;
while (b[r][c] == 0) {
b[r][c]= i; i= i+1;
if (b[mod(r-1, size)][mod(c+1, size)] == 0) {
r= mod(r-1, size);
c= mod(c+1, size); }
else r= mod(r+1,size);
}
}
Previous slide
Back to first slide
View graphic version