I'm trying to make a program which prints different sized grids (n by n grids). These grids cannot have the same number in any column or row (like a Sudoku). My bruteSolve() method runs through every combination and then isValid() checks if the rows and columns add up & multiply up to a certain number too - if they do, then the program prints the grid.However, this program takes too long and I want one where I could do say, 10 * 10 grids fairly quickly. How would I be able to change the code so it does it much faster?import java.util.ArrayList;import java.util.Arrays;public class MagicSquareSolver2 {static ArrayList<Integer> numbers;static int n;static int [] [] grid;static int count;public static void main (String [] args){    n = 4;      grid = new int [n] [n] ;    bruteSolve(0,0,n);}public static void bruteSolve(int a, int b, int n){    for  (int i=1; i<n+1; i++){        grid [a][b] = i;        if (b==n-1 && a==n-1){             if (isValid(grid)){count++;            System.out.println(Arrays.deepToString(grid));System.out.println(count);        }        }        else if (b==n-1){            bruteSolve(a+1,0,n);        }        else{            bruteSolve(a, b+1,n);        }    }}public static boolean isValid (int [] [] grid) {    int factorial = 1;    int fibonacci = 0;    int totalX1 = 1;    int totalY1 = 1;    int totalX2 = 0;    int totalY2 = 0;    for (int i=n; i>0; i--){        factorial = factorial * i;    }    for (int i=n; i>0; i--){        fibonacci = fibonacci +i;    }    for (int i=0; i<n; i++){        for (int j=0; j<n; j++){            totalY1= totalY1 * grid[i][j]; //checks all columns            totalX1= totalX1 * grid[j][i]; // checks all rows            totalX2 = totalX2 + grid[j][i];            totalY2 = totalY2 + grid[i][j];        }        if (totalX1 != factorial || totalY1 != factorial || totalX2 != fibonacci || totalY2 != fibonacci) { return false; }        totalX1 = 1;        totalY1 = 1;        totalX2 = 0;        totalY2 = 0;    }    return true;}}
Sudoku type grid generator
java;performance;sudoku