Programming Examples
Java program for wondrous square
A wondrous square is an n by n grid which fulfils the following conditions:
It contains integers from 1 to n2, where each integer appears only once.
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.
17 | 24 | 1 | 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.
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
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 |
15 | 0 | 1 |
Input:
n=3
1 | 2 | 4 |
3 | 7 | 5 |
8 | 9 | 6 |
Output