Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tp0.Helper;
- public class Tp4ejercicio4 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int opcion,mult;
- SimpleLinkedList<Integer> lista = new SimpleLinkedList<Integer>();
- System.out.println("Ejercicio 4 Tp 4");
- System.out.println("En este ejercicio obtendremos la factorización y multiplicidad de un número dado");
- do {
- opcion = menu();
- switch (opcion) {
- case 1:
- lista = manual();
- System.out.println("Su factorización es: ");
- System.out.println(lista.toString());
- mult = multiplicidad(lista);
- System.out.println("Multiplicidad: "+mult);
- break;
- case 2:
- lista = aleatorio();
- System.out.println("Su factorización es: ");
- System.out.println(lista.toString());
- mult = multiplicidad(lista);
- System.out.println("Multiplicidad: "+mult);
- break;
- case 3:
- System.out.println("Programa Terminado");
- break;
- default:
- System.out.println("No es una opción correcta ");
- break;
- }
- }while(opcion!=3);
- }
- public static int menu() {
- int op;
- System.out.println();
- System.out.println("1.Ingresar manualmente");
- System.out.println("2.Generar aleatoriamente");
- System.out.println("3.Salir");
- op = Helper.getPositiveInt("Ingrese una opcion");
- return op;
- }
- public static SimpleLinkedList<Integer> manual() {
- SimpleLinkedList<Integer> list = new SimpleLinkedList<Integer>();
- int num = Helper.getPositiveInt("Ingrese el número a factorizar"),i = 2;
- while (num<=1) {
- System.out.println("El número debe ser mayor que 1");
- num = Helper.getPositiveInt("Ingrese el número a factorizar");
- }
- while(i<num+1) {
- if(esPrimo(i)&((num%i)==0)) {
- list.addLast(i);
- num = num/i;
- }else {
- i++;
- }
- }
- return list;
- }
- public static SimpleLinkedList<Integer> aleatorio() {
- SimpleLinkedList<Integer> list = new SimpleLinkedList<Integer>();
- int num = Helper.random.nextInt(100),i = 2;
- while (num<=1) {
- num = Helper.random.nextInt(100);
- }
- System.out.println("El número generado es: "+num);
- while(i<=num) {
- if(esPrimo(i)&((num%i)==0)) {
- list.addLast(i);
- num = num/i;
- }else {
- i++;
- }
- }
- return list;
- }
- public static int multiplicidad(SimpleLinkedList<Integer> list) {
- int x = list.element(),cont = 0;
- for(int num = 0; list.size()>0;) {
- num = list.removeFirst();
- if (num==x) cont++;
- }
- return cont;
- }
- public static boolean esPrimo(int a) {
- int contador = 0;
- int i = 2;
- while(i<=(Math.sqrt(a))) {
- if (a%i==0)contador++;
- i++;
- }
- if (contador>0) return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement