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?

Categories

HOME
gerrit
omnet++
plot
google-docs
google-oauth
cmd
umd
podio
swagger-ui
qore
facebook-messenger-bot
slurm
elasticsearch-hadoop
correlation
collectd
visual-studio-2005
ibm-odm
foselasticabundle
tostring
finite-automata
aurigma
database-replication
interop
alpine
one-hot-encoding
orleans
dcevm
kvc
extjs5
telerik-reporting
sparse-matrix
cas
wpfdatagrid
pingfederate
filezilla
web-api-testing
fluentvalidation
large-file-upload
gitignore
c++-amp
gsmcomm
http-status-code-503
uisplitview
pdb
noraui
code-contracts
azure-sql-database
rotational-matrices
picasso
jmonkeyengine
qwerty
x11-forwarding
occlusion
ensembles
galen
core-plot
segment
wso2carbon
youcompleteme
blogengine.net
dotnetzip
veracode
yii2-extension
drupal-6
knockout-components
windows-mobile-6.5
r-forge
namecoin
zendesk-app
iso8601
hittest
dynamics-sl
account-kit
google-cdn
messenger
cubes
xcb
firmata
heidisql
bonobo
intrusion-detection
django-debug-toolbar
autorest
firebaseui
sigabrt
itextpdf
pyke
hsv
vhd
execute
t4mvc
marching-cubes
iis-arr
dukescript
sframe
appfabric-cache
composite
notify
apache-commons-net
website-monitoring
codeigniter-url
network-interface
cdt
android-radiobutton
uitouch
rebol3
datagridviewcolumn
oracle-warehouse-builder
cos
enterprisedb
distutils
hamiltonian-cycle
cufon
doh
psi
tomcat-valve
yui-datatable
actionview
iphone-web-app
jmock
site.master
cxxtest
.nettiers
mozilla-prism
perfect-hash
backcolor
avatar
sustainable-pace
mediarss
data-acquisition
lzh

Resources

Encrypt Message