Deshaw Inc Interview Question
Development Support EngineersHere is the code:
/**
* Creates a Magic Square of dimension num*num where num is any odd number.
* The procedure for creating a magic square of odd dimension is given here,
* http://en.wikipedia.org/wiki/Magic_square
* I just tried to wrote it in Java.
* @param num Integer specifying the dimension of the Magic Square
* @return Two dimensional int array, which is a perfect Magic Square.
*/
public int[][] createMagicSquare(int num){
int[][] magicSquare = new int[num][num];
// Initialize MagicSquare to 0
for(int i = 0; i < num; i++)
for(int j = 0; j < num; j++)
magicSquare[i][j] = 0;
int row = 0;
int col = num / 2;
int count = 1;
for(int i = 0; i < num; i++){
for(int j = 1; j <= num; j++){
if(magicSquare[row][col] == 0 )
magicSquare[row][col] = count;
else{
row = row + 2;
col = col -1;
if(row >= num)
row -= num;
if(col < 0)
col += num;
magicSquare[row][col] = count;
}
row = row - 1;
if(row == -1)
row = num + row;
col = col + 1;
if( col == num)
col = 0;
count++;
}
}
return magicSquare;
}
square[][]-->initialized to 0
square[0][n/2]=1;
row=0;
col=n/2;
for(i=2;i<=n*n;i++)
{
if(row>=1)
r=row-1;
else
r=n-1;
if(col>=1)
c=col-1;
else
c=n-1;
if(square[r][c]>=1)
row=(row+1)%n;
else
{
row=r;
col=c;
}
square[row][col]=i;
}
The technique used for generating a magic square is called the Siamese method.
Below is its implementation in C for a matrix of size 3X3.
/*
Magic square
*/
#include<stdio.h>
#include<math.h>
#define N 9
int magic(int a[][3],int i,int j,int count)
{
int k , l;
a[i][j] = count;
while(count<N)
{
++count;
/*Go up and then right*/
k = 3- i - ((i+1)%3);
l=(j+1)%3;
if(!a[k][l])
{
a[k][l] = count;
i = k;j = l;
}
else
{ /*If up and right is taken, then just go to the cell below*/
i = (i+1)%3;
a[i][j] = count;
}
}
}
int main()
{
int a[3][3]={0};
int i,j,n = 3;
magic(a,0,1,1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
}
import java.util.*;
class magic
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter dimension of matrix :-");
int n=sc.nextInt();
int A[][]=new int[n][n];
int r=n-1,c=n/2;
A[r][c]=1;
for(int i=2;i<=n*n;i++)
{
if(A[(r+1)%n][(c+1)%n]==0)
{
r=(r+1)%n;
c=(c+1)%n;
}
else
r=(r-1+n)%n;
A[r][c]=i;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[j][i]+"\t");
}
System.out.println();
}
System.out.println();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-i][n-1-j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-j][n-1-i]+"\t");
}
System.out.println();
}
System.out.println();
for (int i=0;i<n;i++ ) {
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-j][i]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-i][j]+"\t");
}
System.out.println();
}
System.out.println();
for (int i=0;i<n;i++ ) {
for(int j=0;j<n;j++)
{
System.out.print(A[i][n-1-j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[j][n-1-i]+"\t");
}
System.out.println();
}
}
}
import java.util.*;
class magic
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter dimension of matrix :-");
int n=sc.nextInt();
int A[][]=new int[n][n];
int r=n-1,c=n/2;
A[r][c]=1;
for(int i=2;i<=n*n;i++)
{
if(A[(r+1)%n][(c+1)%n]==0)
{
r=(r+1)%n;
c=(c+1)%n;
}
else
r=(r-1+n)%n;
A[r][c]=i;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[j][i]+"\t");
}
System.out.println();
}
System.out.println();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-i][n-1-j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-j][n-1-i]+"\t");
}
System.out.println();
}
System.out.println();
for (int i=0;i<n;i++ ) {
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-j][i]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-i][j]+"\t");
}
System.out.println();
}
System.out.println();
for (int i=0;i<n;i++ ) {
for(int j=0;j<n;j++)
{
System.out.print(A[i][n-1-j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[j][n-1-i]+"\t");
}
System.out.println();
}
}
}
import java.util.*;
class magic
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter dimension of matrix :-");
int n=sc.nextInt();
int A[][]=new int[n][n];
int r=n-1,c=n/2;
A[r][c]=1;
for(int i=2;i<=n*n;i++)
{
if(A[(r+1)%n][(c+1)%n]==0)
{
r=(r+1)%n;
c=(c+1)%n;
}
else
r=(r-1+n)%n;
A[r][c]=i;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[j][i]+"\t");
}
System.out.println();
}
System.out.println();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-i][n-1-j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-j][n-1-i]+"\t");
}
System.out.println();
}
System.out.println();
for (int i=0;i<n;i++ ) {
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-j][i]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[n-1-i][j]+"\t");
}
System.out.println();
}
System.out.println();
for (int i=0;i<n;i++ ) {
for(int j=0;j<n;j++)
{
System.out.print(A[i][n-1-j]+"\t");
}
System.out.print("| \t");
for(int j=0;j<n;j++)
{
System.out.print(A[j][n-1-i]+"\t");
}
System.out.println();
}
}
}
I am not sure if I understand this question. Can you provide a couple of example squares?
- Anonymous September 11, 2008