Advertisement
LEANDRONIEVA

Ejercicio4Tp4

Oct 25th, 2022
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import tp0.Helper;
  2.  
  3. public class Tp4ejercicio4 {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.         int opcion,mult;
  8.         SimpleLinkedList<Integer> lista = new SimpleLinkedList<Integer>();
  9.  
  10.         System.out.println("Ejercicio 4 Tp 4");
  11.         System.out.println("En este ejercicio obtendremos la factorización y multiplicidad de un número dado");
  12.        
  13.         do {
  14.             opcion = menu();
  15.             switch (opcion) {
  16.             case 1:        
  17.                 lista = manual();
  18.                 System.out.println("Su factorización es: ");
  19.                 System.out.println(lista.toString());
  20.                 mult = multiplicidad(lista);
  21.                 System.out.println("Multiplicidad: "+mult);
  22.                 break;
  23.             case 2:
  24.                 lista = aleatorio();
  25.                 System.out.println("Su factorización es: ");
  26.                 System.out.println(lista.toString());
  27.                 mult = multiplicidad(lista);
  28.                 System.out.println("Multiplicidad: "+mult);
  29.                 break;
  30.             case 3:
  31.                 System.out.println("Programa Terminado");
  32.                 break;
  33.             default:
  34.                 System.out.println("No es una opción correcta ");
  35.                 break;
  36.             }
  37.         }while(opcion!=3);
  38.     }
  39.  
  40.     public static int menu() {
  41.         int op;
  42.         System.out.println();
  43.         System.out.println("1.Ingresar manualmente");
  44.         System.out.println("2.Generar aleatoriamente");
  45.         System.out.println("3.Salir");
  46.         op = Helper.getPositiveInt("Ingrese una opcion");
  47.  
  48.         return op;
  49.     }
  50.    
  51.     public static SimpleLinkedList<Integer> manual() {
  52.         SimpleLinkedList<Integer> list = new SimpleLinkedList<Integer>();
  53.         int num = Helper.getPositiveInt("Ingrese el número a factorizar"),i = 2;
  54.        
  55.         while (num<=1) {
  56.             System.out.println("El número debe ser mayor que 1");
  57.             num = Helper.getPositiveInt("Ingrese el número a factorizar");
  58.         }
  59.         while(i<num+1) {
  60.             if(esPrimo(i)&((num%i)==0)) {
  61.                 list.addLast(i);
  62.                 num = num/i;
  63.             }else {
  64.                 i++;               
  65.             }
  66.         }
  67.        
  68.         return list;
  69.     }
  70.    
  71.     public static SimpleLinkedList<Integer> aleatorio() {
  72.         SimpleLinkedList<Integer> list = new SimpleLinkedList<Integer>();
  73.         int num = Helper.random.nextInt(100),i = 2;
  74.        
  75.         while (num<=1) {
  76.             num = Helper.random.nextInt(100);
  77.         }
  78.        
  79.         System.out.println("El número generado es: "+num);
  80.        
  81.         while(i<=num) {
  82.             if(esPrimo(i)&((num%i)==0)) {
  83.                 list.addLast(i);
  84.                 num = num/i;
  85.             }else {
  86.                 i++;               
  87.             }
  88.         }
  89.        
  90.         return list;
  91.     }
  92.    
  93.     public static int multiplicidad(SimpleLinkedList<Integer> list) {
  94.         int x = list.element(),cont = 0;
  95.        
  96.         for(int num = 0; list.size()>0;) {
  97.             num = list.removeFirst();
  98.             if (num==x) cont++;
  99.         }
  100.         return cont;
  101.     }
  102.    
  103.     public static boolean esPrimo(int a) {
  104.         int contador = 0;
  105.         int i = 2;
  106.         while(i<=(Math.sqrt(a))) {
  107.             if (a%i==0)contador++;
  108.             i++;
  109.         }
  110.         if (contador>0) return false;
  111.         return true;
  112.     }
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement