how can I optimize this algorithm? [closed]

1
  

Read 9 whole numbers store them in a matrix.   Find out how many numbers are cousins and which are perfect numbers

import java.io.*;
import javax.swing.JOptionPane;
public class matriz1 {
    private static int matriz[][] = new int [3][3];
    private static int perfectos []; 


public static void llenar(){
    int cont = 0, cont2 = 0, k = 0;
    for(int f = 0; f < 3; f++ ){
        for (int c = 0; c < 3; c++){
            matriz[f][c] = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero"));
        }
    } 
    for (int i = 0; i < matriz.length; i++) {
        for (int j = 0; j < matriz[i].length; j++) {
                if(esPrimo(matriz[i][j]) == true){
                    cont = cont + 1;
                }  //Si es true, el valor es primo
        }

    }
    for (int i = 0; i < matriz.length; i++) {
        for (int j = 0; j < matriz[i].length; j++) {
                if(esPerfecto(matriz[i][j]) == 1){
                    cont2 = cont2 + 1;
                }  //Si es 1, el valor es perfecto
        }
    }
        perfectos = new int [cont2];
    for (int i = 0; i < matriz.length; i++) {
        for (int j = 0; j < matriz[i].length; j++) {
                if(esPerfecto(matriz[i][j]) == 1){
                    perfectos[k] = matriz[i][j];
                    k++;
                }  //Si es 1, el valor es perfecto
        }

    }

    imprimir(cont, cont2);
}

public static  boolean  esPrimo(int num){
    for(int i=2; i<num/2; i++)
    {
        if(num%i==0)
            return false;
    }
    return true;
}
public static  int  esPerfecto(int num){
    int suma = 0;
    int verdadero = 0;
    for(int i=1; i<num; i++){

       if (num % i == 0){
                suma = suma + i;     // si es divisor se suma
        }
    }
        if (suma == num) {  // si el numero es igual a la suma de sus divisores es perfecto
          verdadero = 1; 
        }
    return verdadero;
}
public static void imprimir(int primos,int cont2){
    JOptionPane.showMessageDialog(null,"Hay " + primos +" Numeros primos");
    for(int i = 0; i < cont2; i++){
        System.out.println("el "+ perfectos[i] +" es perfectos");
    }
}
public static void main (String[] args) {
    llenar();
} 
}
    
asked by Cristian Gallego 12.11.2018 в 05:04
source

1 answer

1

To begin with, you have three almost equal loops:

for (int i = 0; i < matriz.length; i++) {
    for (int j = 0; j < matriz[i].length; j++) {
            if(esPrimo(matriz[i][j]) == true){
                cont = cont + 1;
            }  //Si es true, el valor es primo
            if(esPerfecto(matriz[i][j]) == 1){
                cont2 = cont2 + 1;
            }  //Si es 1, el valor es perfecto
    }

}
for (int i = 0; i < matriz.length; i++) {
    for (int j = 0; j < matriz[i].length; j++) {
            if(esPerfecto(matriz[i][j]) == 1){
                cont2 = cont2 + 1;
            }  //Si es 1, el valor es perfecto
    }
}
perfectos = new int [cont2];
for (int i = 0; i < matriz.length; i++) {
    for (int j = 0; j < matriz[i].length; j++) {
            if(esPerfecto(matriz[i][j]) == 1){
                perfectos[k] = matriz[i][j];
                k++;
            }  //Si es 1, el valor es perfecto
    }

}

Why not do the 3 checks in one pass? To begin with, we join the first two:

for (int i = 0; i < matriz.length; i++) {
    for (int j = 0; j < matriz[i].length; j++) {
            if(esPrimo(matriz[i][j])){
                cont = cont + 1;
            }  //Si es true, el valor es primo
            if(esPerfecto(matriz[i][j]) == 1){
                cont2 = cont2 + 1;
            }  //Si es 1, el valor es perfecto
    }

}

perfectos = new int [cont2];
for (int i = 0; i < matriz.length; i++) {
    for (int j = 0; j < matriz[i].length; j++) {
            if(esPerfecto(matriz[i][j]) == 1){
                perfectos[k] = matriz[i][j];
                k++;
            }  //Si es 1, el valor es perfecto
    }

}

The third one has a slight dependence on the previous ones, since you initialize an array with a size that depends on the result, but you can do two things:

  • Define the array with the maximum size (9), leaving the remaining fields null.
  • Use a dynamic size list (interface List, class ArrayList, for example).

Yes ...

perfectos= new int [9];

Then you can do:

for (int i = 0; i < matriz.length; i++) {
    for (int j = 0; j < matriz[i].length; j++) {
        if(esPrimo(matriz[i][j])){
            cont ++;
        }  //Si es true, el valor es primo
        if(esPerfecto(matriz[i][j]) == 1){
            perfectos[cont2 ++] = matriz[i][j]);
        }  
    }
}
    
answered by 16.11.2018 / 12:36
source