KnowledgeBoat Logo

Computer Science

A wondrous square is an n by n grid which fulfils the following conditions:

  1. It contains integers from 1 to n2, where each integer appears only once.
  2. The sum of integers in any row or column must add up to 0.5 x n x (n2 + 1).

For example, the following grid is a wondrous square where the sum of each row or column is 65 when n=5.

17241815
23571416
46132022
101219213
11182529

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.

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. For example, 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:

Input:
n = 4

161512
641014
98125
371113

Output:
Yes, it represents a wondrous square

PrimeRow IndexColumn Index
203
330
523
731
1132
1333
1501

Input:
n = 3

124
375
896

Output:
Not a wondrous square

PrimeRow IndexColumn Index
201
310
512
711

Input:
n = 2

23
32

Output: Not a wondrous square

PrimeRow IndexColumn Index
200
211
301
310

Java

Java Arrays

5 Likes

Answer

import java.util.Scanner;

public class KboatWondrousSquare
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = in.nextInt();

        if (n < 2 || n > 10) {
            System.out.println("Invalid value of n!");
            return;
        }

        int a[][] = new int[n][n];

        System.out.println("Enter elements of the matrix: ");
        for (int i = 0; i < n; i++) {
            System.out.println("Enter Row "+ (i+1) + " :");
            for (int j = 0; j < n; j++) {
                a[i][j] = in.nextInt();
            }
        }

        System.out.println("The Matrix is:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }

        //Check Wondrous
        int nSq = n * n;
        double validSum = 0.5 * n * (nSq + 1);
        boolean wondrous = isWondrous(a);

        if (wondrous) {
            System.out.println("Yes, it represents a wondrous square");
        }
        else {
            System.out.println("Not a wondrous square");
        }
        
        //Print Prime Numbers
        printPrime(a);
    }

    public static boolean isWondrous(int arr[][]) {
        int n = arr.length;
        int nSq = n * n;
        double validSum = 0.5 * n * (nSq + 1);

        /*
         * seenArr is used to check that
         * numbers are not repeated
         */
        boolean seenArr[] = new boolean[nSq];

        for (int i = 0; i < n; i++) {

            int rSum = 0, cSum = 0;

            for (int j = 0; j < n; j++) {
                if (arr[i][j] < 1 || arr[i][j] > nSq) {
                    return false;
                }

                //Number is not distinct
                if (seenArr[arr[i][j] - 1]) {
                    return false;
                }

                seenArr[arr[i][j] - 1] = true;

                rSum += arr[i][j];
                cSum += arr[j][i];
            }

            if (rSum != validSum || cSum != validSum) {
                return false;
            }
        }
        
        return true;
    }
    
    public static void printPrime(int arr[][]) {
        
        int n = arr.length;
        
        System.out.println("Prime\tRow Index\tColumn Index");
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (isPrime(arr[i][j])) {
                    System.out.println(arr[i][j] + "\t" + i + "\t\t" + j);
                }
            }
        }
    }
    
    public static boolean isPrime(int num) {
        int c = 0;
        
        for (int i = 1; i <= num; i++) {
            if (num % i == 0) {
                c++;
            }
        }
        
        return c == 2;
    }
}

Output

BlueJ output of A wondrous square is an n by n grid which fulfils the following conditions: (a) It contains integers from 1 to n 2 , where each integer appears only once. (b) The sum of integers in any row or column must add up to 0.5 x n x (n 2 + 1). For example, the following grid is a wondrous square where the sum of each row or column is 65 when n=5. 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. 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. For example, 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: Input: n = 4 Output: Yes, it represents a wondrous square Input: n = 3 Output: Not a wondrous square Input: n = 2 Output: Not a wondrous squareBlueJ output of A wondrous square is an n by n grid which fulfils the following conditions: (a) It contains integers from 1 to n 2 , where each integer appears only once. (b) The sum of integers in any row or column must add up to 0.5 x n x (n 2 + 1). For example, the following grid is a wondrous square where the sum of each row or column is 65 when n=5. 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. 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. For example, 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: Input: n = 4 Output: Yes, it represents a wondrous square Input: n = 3 Output: Not a wondrous square Input: n = 2 Output: Not a wondrous squareBlueJ output of A wondrous square is an n by n grid which fulfils the following conditions: (a) It contains integers from 1 to n 2 , where each integer appears only once. (b) The sum of integers in any row or column must add up to 0.5 x n x (n 2 + 1). For example, the following grid is a wondrous square where the sum of each row or column is 65 when n=5. 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. 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. For example, 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: Input: n = 4 Output: Yes, it represents a wondrous square Input: n = 3 Output: Not a wondrous square Input: n = 2 Output: Not a wondrous squareBlueJ output of A wondrous square is an n by n grid which fulfils the following conditions: (a) It contains integers from 1 to n 2 , where each integer appears only once. (b) The sum of integers in any row or column must add up to 0.5 x n x (n 2 + 1). For example, the following grid is a wondrous square where the sum of each row or column is 65 when n=5. 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. 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. For example, 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: Input: n = 4 Output: Yes, it represents a wondrous square Input: n = 3 Output: Not a wondrous square Input: n = 2 Output: Not a wondrous square

Answered By

2 Likes


Related Questions