andresnogales

FactorizationList.java

Oct 27th, 2021 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. //Grupo 2.3
  2.  
  3. public class FactorizationList extends SimpleLinkedList<PrimeFactor>{
  4.    
  5.     private Integer number;
  6.     private Integer multiplicity;
  7.  
  8.     public FactorizationList(Integer number) {
  9.         this.number = number;
  10.         this.multiplicity = 0;
  11.     }
  12.    
  13.     public void calculate() {
  14.         Integer n = number;
  15.         for (int i = 2; i <= n / i; i++) {
  16.             while (n % i == 0) {               
  17.                 if (this.size() == 0) {
  18.                     this.addFirst(new PrimeFactor(i,1));
  19.                     multiplicity = 1;
  20.                 }              
  21.                 else {                                     
  22.                     if (this.getFirst().getBase() == i) {
  23.                         this.getFirst().incrementExponent();
  24.                         multiplicity++;
  25.                     }
  26.                     else {
  27.                         this.addLast(new PrimeFactor(i,1));
  28.                     }
  29.                 }
  30.                 n = n / i;
  31.             }
  32.         }
  33.        
  34.         if (n >= 1) {
  35.             this.addLast(new PrimeFactor(n,1));
  36.             if (multiplicity == 0) multiplicity = 1;
  37.         }
  38.     }
  39.    
  40.     @Override
  41.     public String toString() {
  42.         String stringFromSuper = super.toString();
  43.         return stringFromSuper.replaceAll(", ", " x ");
  44.     }
  45.  
  46.     public Integer getMultiplicity() {
  47.         return multiplicity;
  48.     }
  49.        
  50.     public void printResult() {
  51.         calculate();
  52.         System.out.print("\nLa factorización del número " + number + " es ");
  53.         System.out.println(this.toString());
  54.         System.out.println("La multiplicidad es " + multiplicity);
  55.     }
  56. }
  57.  
Add Comment
Please, Sign In to add comment