Complete the given code so that makeGrid creates an n-by-n boolean array, result, such that result[i][j] is true if and only if i and j are relatively prime (also called coprime i.e. they have no common factors). Thus, each entry in the grid is for a pair of numbers. Location (i, j) is true if and only if i and j are coprime. For example,You may want to create some other helper methods.
public class Main
{
// Do not change this method's signature or access.
public static boolean[][] makeGrid(int n)
{
boolean[][] result = new boolean[n][n];
for(int n1 =0; n1 < n; n1++) {
for (int n2 = 0; n2 < n; n2++) {
int a = n1;
int b = n2;
if (n1 == 0 || n2 == 0) {
if(n1 ==0 && n2 ==1){
result[n1][n2] = true;
}
else if(n1 == 1 && n2 == 0){
result[n1][n2] = true;
}
else
result[n1][n2] = false;
}
else{
if (a < b) {
int tmp = a;
a = b;
b = tmp;
}
int c;
while ((c = a % b) != 0) {
a = b;
b = c;
}
if (b == 1) {
result[n1][n2] = true;
}
}
}
}
return result;
}
public static void main(String[] args)
{
// Make sure we have the right number of arguments
if(args.length != 1)
{
System.err.println("Should have exactly one argument n (the size of the grid).");
System.exit(1);
}
// Make sure the argument is an integer
int n;
try
{
n = Integer.parseInt(args[0]);
}
catch (NumberFormatException ex)
{
System.err.printf("Argument must be an integer, given %s.\n", args[0]);
System.exit(2);
return;
}
// Make sure that n is positive
if(n < 1)
{
System.err.printf("Argument must be positive, given %s.\n", args[0]);
System.exit(3);
}
boolean[][] grid = makeGrid(n);
// Print the grid
for(int rowNumber = 0; rowNumber < n; rowNumber++)
{
for(int columnNumber = 0; columnNumber < n; columnNumber++)
{
System.out.print(grid[rowNumber][columnNumber] ? "*" : ".");
}
System.out.println();
}
}
}