Thursday 19 January 2017

Wondrous Square..

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



Solution:-
import java.util.*;
class wonderous
{
     int arr[][],arr1[];;
     int n,i,j,x=0,r,c;
     int flag;
     void perform()
     {
           Scanner sc=new Scanner(System.in);
           System.out.println("Enter the size of array(row and column same):");
           n=sc.nextInt();
           arr=new int[n][n];
         arr1=new int[2*n];
           for(i=0;i<n;i++)
           {
                for(j=0;j<n;j++)
                {
                System.out.print("Enter the value:");
                arr[i][j]=sc.nextInt();
           }
     }
           System.out.println("The matrix is"); 
     for(i=0;i<n;i++)
     {
           r=0;
           c=0;
                for(j=0;j<n;j++)
                {
                     System.out.print(arr[i][j]+" ");
                     r=r+arr[i][j];
                     c=c+arr[j][i];
                }
           System.out.println();
                arr1[x]=r;
                arr1[x+n-1]=c;
                x++;
           }
           for(i=0;i< x;i++)
           {
           if(arr1[i]!= 0.5 * n * (n*n + 1))
                break;
        }
        if(i==x)
        System.out.println("YES IT REPRESENTS A WONDROUS SQUARE.");
        else
        System.out.println("IT IS NOT A WONDROUS SQUARE.");
        System.out.println("PRIME  ROW  COLUMN");
        for(i=0;i< n;i++)
        {
           for(j=0;j< n;j++)
           {
                if(prime(arr[i][j]))
                System.out.println(arr[i][j]+ "   "+i+ "   "+j);
           }
        }
     }
    boolean prime(int no)
    {
           int index;
        for(index=2;index<no;index++)
        {
           if(no%index==0)
          break;
        }
        if(index==no)
        return true;
        else
        return false;
     }
    public static void main(String args[])
    {
           wonderous ob=new wonderous();
           ob.perform();
    }
}



1 comment:

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...