1.Define an int matrix (n * n)
2. Create a static method that print the matrix
2.1 Create a static method that print an array.
3. Create a cicle that insert factors number from a given input (n from 1 to 100) and print all arrays and then insert the array’s elements to a matrix of all factorized number.
package factortomatrix; public class FactorToMatrix { public static void main(String[] args) { //1. Define an int matrix (n * n) //2. Create a static method that print the matrix //2.1 Create a static method that print an array //3. Create a cicle that insert factors number from //a given input (n from 1 to 100) and print // all array and then insert the array // elements to a matrix of all factorized number. int a[][]= new int[4][4]; int i=1,n=100; /* i create a cicle that put i as input to a method that finds factors i start from 1 and go to 100 */ while(i<n) { System.out.println("n given :"+ i); //randomMatrixCreator(a,16); //PrintMatrix(a); int factors[]= new int[i]; factors=FindFactors(i); PrintArray(factors); System.out.println("--------------"); i++; } } // i code a findFactors(int n) // method that return an array....----here the trick---- public static int[] FindFactors(int n) { int factors=1;int len=n/2+1; int factorsArray[] = new int[len]; int i=0; while(factors<len) { if (n%factors==0) // here i find factors { // here we have a factor factorsArray[i]=factors; i++; } factors++; } return factorsArray; } // to test printMatrix i create a randomMatrixCreator public static void randomMatrixCreator(int m[][],int nMax) {// nMax is the maximum value i insert to the matrix // from 0 to nMax for(int i=0;i<m.length;i++) { for(int j=0;j<m.length;j++) { m[i][j]=(int)(Math.random()*nMax); } } } public static void PrintMatrix(int m[][]) { for(int i=0;i<m.length;i++) { for(int j=0;j<m.length;j++) { System.out.print(m[i][j]+ " "); } System.out.println(); } } public static void PrintArray(int m[]) { for(int i=0;i<m.length;i++) System.out.print(m[i]+ " "); } } The following video is the solution:
from an factors arrays we create a matrix filled by numbers divisors.