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) { printPrimeNumbers(1) } }
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) { stack.add(2); n = n / 2; } //Second Step for (int i = 3; i <= Math.sqrt(n); i += 2) { while (n % i == 0) { stack.add(i); 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) { stack.add(i); n /= i; i--; } } } public static void printPrimeFactors(int n){ if(n > 1){ Stack<Integer> stack = new Stack<Integer>(); findPrimeNumbers(n, stack); while (!stack.isEmpty()) { System.out.print(stack.pop()+ " "); } } }
Related Links
Android apk could load native library
Can you create objects faster than JVM GC can handle ? Thus OOM occurs?
hadoop - java.lang.RuntimeException: java.lang.InstantiationException
Use of Alternate Entry Points making an Blackberry application can not be uninstalled
Struts 2 get result string in JSP
Java DDR Game, Arrows and Music out of sync
Index certain maven modules in Intellij Idea 12
So why isn't this actually starting? ServerSocket and Sockets
Displaying Pictures in Array
Programmed Android app Does not Show in an AVD
Generic method is not applicable for the arguments
Insert list of objects into combobox
How best to store and modify categorized data in Android [closed]
Spring RowMapper NullPointerException
Reigniting conditional statement after breaking out of it?
Can urls in a RESTful web service do different things for users with different authorization levels?