### java

#### Prime Factorization using Stacks

```I am trying to write a program that uses a stack to print the prime factors of a positive integer in descending order.
However, I'm not sure what I'm doing is exactly on point.
This is the code that I have so far. At the moment I have the prime factorization done, but the Stack part is tripping me up.
import java.util.Stack; // Initialize Stacks
public class PrimeFactorization {
public static void printPrimeNumbers(int prime) {
Stack<Integer> stack = new Stack<Integer>(); //create stack
int n = 0;
for (int i = 0; i < n; i++) { //intialize for loop to check each letter
stack.push(n);
}
while (!stack.isEmpty()) {
n += stack.pop();
}
for (int i=0; i <= prime; i++) {
n = 0;
while (prime % i == 0) {
prime /= i;
n++;
}
if (n != 0) {
for (int j = n; j > 0; j--) {
System.out.print(i);
if (prime != 1) {
System.out.print("*");
}
}
}
}
}
public static void main(String[] args) {
}
}
```
```First, find all the 2's which divide the input and add them to stack.
Second, find all prime numbers greater than 2 and if they divide input, add them to stack.
Third, as a last step, check whether the input itself is a prime number greater than 2. If so, print it since step 2 will not add that number.
public static void printPrimeNumbers(int n) {
Stack<Integer> stack = new Stack<Integer>(); //create stack
//First Step
while (n % 2 == 0) {
n = n / 2;
}
//Second Step
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
n = n / i;
}
}
//Third Step
if (n > 2) {
System.out.printf("%d ", n);
}
while (!stack.isEmpty()) {
System.out.printf("%d ", stack.pop());
}
}
```
```You can do basically find the prime numbers and add them to stack then just print until there is no element on stack:
public static void main(String[] args) {
printPrimeFactors(189); // prints 7 3 3 3
}
public static void findPrimeNumbers(int n, Stack<Integer> stack) {
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
n /= i;
i--;
}
}
}
public static void printPrimeFactors(int n){
if(n > 1){
Stack<Integer> stack = new Stack<Integer>();