A wondrous square is an n by n grid which fulfils the following conditions:
(i) It contains integers from 1 to n2, where each integer appears only once.
(ii) The sum of integers in any row or column must add up to 0.5 * n * (n2 + 1).
For example the following grid is a wondrous square where the sum of each row or column is 65 when n = 5 :
17 24 2 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Write a program to read n (2 < = n < = 10) and the values stored in these n by n cells and output if the grid represents a wondrous square or not.
Also output all the prime numbers in the grid along with their row index and column index as shown in the output. A natural number is said to be prime if it has exactly two divisors. E.g. 2, 3, 5, 7, 11,.......
The first element of the given grid i.e. 17 is stored at row index 0 and column index 0 and the next element in the row i.e. 24 is stored at row index 0 and column index 1.
Test your program for the following data and some random data.
SAMPLE DATA:
INPUT : N = 4
16 15 1 2
6 4 10 14
9 8 12 5
3 7 11 13
OUTPUT: YES IT REPRESENTS A WONDROUS SQUARE.
PRIME ROW INDEX COLUMN INDEX
2 0 3
3 3 0
5 2 3
7 3 1
11 3 2
13 3 3
Input N=4
1 2 4
3 7 5
8 9 6
Output not a wondrous square
PRIME ROW INDEX COLUMN INDEX
2 0 0
2 1 1
i. Input the order and elements while checking its validity
Remember elements must be from 1 to n^2
ii. Find the row sum and coloumn sum and equate it with the value determined
by the formula 0.5 * n * (n^2 + 1).
iii. Check the frequeny of each element. None of the elements must appears more than once
iv. Lastly find all the primes along with thier postion and print them
Step iv doesn't require any explanation, just pass the matrix element along with its
row and coloumn index to a method which checks whether it is prime or not. If it is print else
contiune till the last element.
For step i and ii the following method has been used. The code is simple and revealing jsut go
through it.
row_sum +=mat[i][j];
col_sum +=mat[j][i];
respectively determines the row and coloumn sum inside the for loops.
public boolean isWondrousSquare() {
flag=true;
double temp = 0.5*(float)order*((float)order*(float)order + 1);
double row_sum,col_sum;
int frequency=0;
// checking that each element appears only once
for(int k=1;k<=order*order;k++) {
frequency=0;
for(int p=0;p<order;p++) {
for(int q=0;q<order;q++) {
if(k==mat[p][q])
frequency++;
}
}
if(frequency>1)
break;
}
// checking the sum of each row and each coloumn
for(int i=0;i<order;i++) {
row_sum=0;
col_sum=0;
for(int j=0;j<order;j++) {
row_sum +=mat[i][j];
col_sum +=mat[j][i];
}
if(row_sum != temp || col_sum !=temp) {
flag=false;
break;
}
}
if(flag==true && frequency==1)
return true;
else
return false;
}
Complete JAVA CODE
import java.util.*;
public class ISC2005Q2 {
public static void main(String[] args) {
int N;
Scanner in = new Scanner(System.in);
do {
System.out.println("Enter the order of the square matrix:");
N = in.nextInt();
if(N<2 ||
N>10)
System.out.println("INVALID INPUT,TRY AGAIN");
}while(N<2 ||
N>10);
WondrousSquare obj = new WondrousSquare(N);
obj.inputElements();
if(obj.isWondrousSquare())
System.out.println("Yes it represents a wondrous
square.");
else
System.out.println("Not a wondrous square.");
System.out.println("Entered Matrix:");
obj.display();
in.close();
}
}
class WondrousSquare {
public boolean isWondrousSquare() {
flag=true;
double temp = 0.5*(float)order*((float)order*(float)order + 1);
double row_sum,col_sum;
int frequency=0;
// checking that each element appears only once
for(int k=1;k<=order*order;k++) {
frequency=0;
for(int p=0;p<order;p++) {
for(int q=0;q<order;q++) {
if(k==mat[p][q])
frequency++;
}
}
if(frequency>1)
break;
}
// checking the sum of each row and each coloumn
for(int i=0;i<order;i++) {
row_sum=0;
col_sum=0;
for(int j=0;j<order;j++) {
row_sum +=mat[i][j];
col_sum +=mat[j][i];
}
if(row_sum != temp || col_sum !=temp) {
flag=false;
break;
}
}
if(flag==true && frequency==1)
return true;
else
return false;
}
private void isPrime(int element,int row,int col) {
flag = true;
if (element==1)
flag=false;
else {
for(int i = 2; i<=element/2;i++)
if(element%i==0) {
flag=false;
break;
}
}
if(flag==true)
System.out.println(element+" "+row+"
"+col);
}
public void display() {
for(int p=0;p<order;p++)
for(int q=0;q<order;q++)
if(MAX_MATRIX < mat[p][q])
MAX_MATRIX = mat[p][q];
String s,element;
s=Integer.toString(MAX_MATRIX);
for(int i=0;i<order;i++) {
for(int j=0;j<order;j++) {
element=Integer.toString(mat[i][j]); // for formatted output
int tmp=element.length();
while(tmp !=s.length())
{
element +="
";
tmp++;
}
System.out.print(element+" ");
}
System.out.println();
}
// printing
all the primes
System.out.println("Prime "+"Row Index
"+"Coloumn
Index");
for(int i=0;i<order;i++)
for(int j=0;j<order;j++)
isPrime(mat[i][j],i,j);
}
public void inputElements() {
Scanner in = new Scanner(System.in);
System.out.println("Enter the values of the matrix:");
for(int i=0;i<order;i++) {
for(int j=0;j<order;j++) {
mat[i][j]=in.nextInt();
if(mat[i][j]<=0 || mat[i][j] > order*order) {
System.out.println("Invalid element,try again:");
j--;
}
}
}
in.close();
}
WondrousSquare(int N){
order = N;
mat = new int[order][order];
MAX_MATRIX =0;
}
private int MAX_MATRIX;
private boolean flag;
private int[][] mat;
private int order;
}