Press factorization using Stack

Asked

Viewed 97 times

0

I made a stack with vector, with basic functions (push, pop, Peek) and with it I want to make a prime factorization of a value. I made the program, but when I compile it, it is in an infinite loop that I am not identifying and does not return anything. Follow the codes:

Stack:

public class Pilha {

    public Object[] pilha;
    public int posicaoPilha;

    public Pilha() {
        this.posicaoPilha = -1;
        this.pilha = new Object[1000];
    }

    public boolean isEmpty() {
        if (this.posicaoPilha == -1) {
            return true;
        }
        return false;
    }

    public int size() {
        if (this.isEmpty()) {
            return 0;
        }
        return this.posicaoPilha + 1;
    }

    public Object peek() {
        if (this.isEmpty()) {
            return null;
        }
        return this.pilha[this.posicaoPilha];
    }

    public Object pop() {
        if (isEmpty()) {
            return null;
        }
        return this.pilha[this.posicaoPilha--];
    }

    public void push(Object valor) {
        if (this.posicaoPilha < this.pilha.length - 1) {
            this.pilha[++posicaoPilha] = valor;
        }
    }

    public void imprime() {
        if (isEmpty()) {
            System.out.println("Pilha vazia");
        }

        while (isEmpty() == false) {
            System.out.print(pop() + " ");                    
        }
    }
}

Function Prime Factor:

public class Exercicio5 {
    int fatorPrimo(int n) {
        int resultado = 1;
        Pilha pilha = new Pilha();

        while (n % 2 == 0) {
            n = n / 2;
            pilha.push(2);
        }

        for(int i = 3; i <= Math.sqrt(n); i=+ 2) {
            while(n % i == 0) {
                n  = n / i;
                pilha.push(i);
            }
        }

        while(!pilha.isEmpty()) {
            resultado = resultado * (int)pilha.pop();
        }

        while(!pilha.isEmpty()) {
            System.out.print(pilha.pop() + " * ");
        }

        return resultado;
    }
}

Testa Pilha:

public class TestaPilha {

    public static void main(String args[]) {
        Exercicio5 ex5 = new Exercicio5();

        int n = 3960;

        System.out.println(ex5.fatorPrimo(n));
    }
}

Note: Prime factorization is to find prime values that are divisors of a value n, which, multiplied by each other, result in the value of n, for example 3960 = 11 * 5 * 3 * 3 * 2 * 2 * 2 * 2.

  • But what is this method of yours supposed to fatorPrimo return ? It seems that it doesn’t make much sense this calculation of the resultado what it is doing, as it ends up calculating the original value it received.

  • Have you tried any basic debug techniques, like printing a message in each method to see where the execution flow is going? Stackoverflow is not college exercises oracle no...

  • @Isac at first that’s right

  • @epx will do that, thanks

  • The main problem is the for(int i = 3; i <= Math.sqrt(n); i=+ 2) { that both the limit is wrong and the increment is i =+2 that should be i += 2, but there are other problems. Now I cannot elaborate an answer without knowing what you intend to return in the method fatorPrimo.

  • @Isac already solved the problem, it was the same of "i += 2"

Show 1 more comment

1 answer

0


Simply set "i =+ 2" to "i += 2"

    public class Exercicio5 {
            int fatorPrimo(int n) {
                int resultado = 1;
                Pilha pilha = new Pilha();

                while (n % 2 == 0) {
                    n = n / 2;
                    pilha.push(2);
            }

            for(int i = 3; i <= Math.sqrt(n); i += 2) {
                while(n % i == 0) {
                    n  = n / i;
                    pilha.push(i);
                }
            }

            while(!pilha.isEmpty()) {
                resultado = resultado * (int)pilha.pop();
            }

            while(!pilha.isEmpty()) {
                System.out.print(pilha.pop() + " * ");
            }

            return resultado;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.