Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Grupo 2.3
- public class FactorizationList extends SimpleLinkedList<PrimeFactor>{
- private Integer number;
- private Integer multiplicity;
- public FactorizationList(Integer number) {
- this.number = number;
- this.multiplicity = 0;
- }
- public void calculate() {
- Integer n = number;
- for (int i = 2; i <= n / i; i++) {
- while (n % i == 0) {
- if (this.size() == 0) {
- this.addFirst(new PrimeFactor(i,1));
- multiplicity = 1;
- }
- else {
- if (this.getFirst().getBase() == i) {
- this.getFirst().incrementExponent();
- multiplicity++;
- }
- else {
- this.addLast(new PrimeFactor(i,1));
- }
- }
- n = n / i;
- }
- }
- if (n >= 1) {
- this.addLast(new PrimeFactor(n,1));
- if (multiplicity == 0) multiplicity = 1;
- }
- }
- @Override
- public String toString() {
- String stringFromSuper = super.toString();
- return stringFromSuper.replaceAll(", ", " x ");
- }
- public Integer getMultiplicity() {
- return multiplicity;
- }
- public void printResult() {
- calculate();
- System.out.print("\nLa factorización del número " + number + " es ");
- System.out.println(this.toString());
- System.out.println("La multiplicidad es " + multiplicity);
- }
- }
Add Comment
Please, Sign In to add comment