# Finding all Permutations that add up to a number.

```public static void createPerm3(){//works only if(n==3) for(int i = 1; i <= n * n; i++) for(int j = 1; j <= n * n; j++) if(i != j && i != mg-i-j && j != mg-i-j &&mg-i-j <= n * n && mg-i-j >0){ Integer[] temp = {i,j,mg-i-j}; perm.add(deepCopy(temp));}//deepCopy returns a manual copy of the array } public static void createPerm4(){//works only if(n==4) for(int i = 1; i <= n * n; i++) for(int j = 1; j <= n * n; j++) for(int k = 1; k <= n * n; k++) if(i != j && i !=k && i != mg-i-j-k && j != k && j != mg-i-j-k && k != mg-i-j-k && mg-i-j-k <= n * n && mg-i-j-k >0){ Integer[] temp = {i,j,k,mg-i-j-k}; perm.add(deepCopy(temp));}} public static void createPerm5(){//works only if(n==5) for(int i = 1; i <= n * n; i++) for(int j = 1; j <= n * n; j++) for(int k = 1; k <= n * n; k++) for(int l = 1; l <= n * n;l++) if(i != j && i !=k && i != l && i != mg-i-j-k-l && j != k && j != l && j != mg-i-j-k-l && k != l && k != mg-i-j-k-l && l != mg-i-j-k-l && mg-i-j-k-l <= n * n && mg-i-j-k-l >0){ Integer[] temp = {i,j,k,mg-i-j-k}; perm.add(deepCopy(temp));}}   public static final int n = 3; //this is should be the only thing that changes in the program public static final int mg = n*(n*n + 1) / 2; // magic number public static ArrayList<Integer[]> perm = new ArrayList<Integer[]>();```