import java.util.Stack;
public class Primes{
public static void main(String[]args){
Stack<Integer> stack = new Stack<Integer>();
stack.push(null);
//number of primes to display
final int NUMBER_OF_PRIMES = 50;
//number of primes to display per line
final int NUMBER_OF_PRIMES_PER_LINE = 10;
//count number of primes
int count = 0;
int number = 2;
System.out.println("The first 50 primes are \n");
while(count < NUMBER_OF_PRIMES){
boolean isPrime = true;
for(int divisor = 2; divisor <= number/2; divisor++){
if(number % divisor == 0){
isPrime = false;
break;
}
}
if(isPrime){
count++;
if(count % NUMBER_OF_PRIMES_PER_LINE ==0){
System.out.println(number);
}
else
System.out.print(number + " ");
}
number++;
}
}
}

- 118,147
- 33
- 203
- 236

- 401
- 1
- 6
- 4
-
Side note: consider to use Deque instead of Stack: Deque
stack = new ArrayDeque – Puce Feb 15 '11 at 19:46(); (See note in Javadoc of Stack: http://download.oracle.com/javase/6/docs/api/java/util/Stack.html ) -
Use LinkedList instead of ArrayDeque) – Stan Kurilin Feb 15 '11 at 19:50
-
ok i will try it out and see how far i get. Thanks. – not looking for answer Feb 15 '11 at 19:52
-
So what is your problem? – Thomas Jungblut Feb 15 '11 at 19:52
-
Am going to try work on this for a bit thank you for all the suggestions.. Its better i work it out myself.. :) – not looking for answer Feb 15 '11 at 20:08
4 Answers
- Read the javadoc for Stack and its parent class : Vector
- When computing the 50 first prime members, instead of displaying them while you find them, store them in the stack
- Once you have finished finding the primes, the stack contains all the primes you found. The smallest one is the first element of the stack, and the biggest one is the last element of the stack. Start another loop from the end of the stack to the beginning to display the primes in descending order.
Note : Stack is an old class that should not be used anymore. You should prefer ArrayList.

- 678,734
- 91
- 1,224
- 1,255
-
"Note : Stack is an old class that should not be used anymore. You should prefer ArrayList." --- Uuhhh no, ArrayList and Stack solve totally different problems. – Martin Doms Feb 15 '11 at 19:58
-
@Martin while you're correct in a sense, you should read the Javadoc for the Stack class, which state that you should probably use something different. Specifically, they mention the Deque interface. @JB In regard to my comment to Martin, that would make the preferred class LinkedList, not ArrayList. This is despite the fact that ArrayList is the typical replacement for Vector. It should also be noted that Vector is not a bad class, it merely has overhead that you typically don't need. – corsiKa Feb 15 '11 at 20:17
-
But why would you use a List class instead of a Deque? Deque provides the correct constraints and a better interface for solving queuing problems. – Martin Doms Feb 15 '11 at 20:26
-
@Martin and @glowcoder : since my proposition suggests iterating in reverse order through the stack (which is a Vector, and thus has a relationship to ArrayList) and accessing it by index, ArrayList fits perfectly. There is no need for stack operations (push, pop) in this problem : no need to empty the stack to display the prime numbers. – JB Nizet Feb 15 '11 at 20:59
Because it's a homework problem I won't give code, but here's the process, picking up from somewhere within your code.
- If
number
is prime, push it onto the stack. - When the prime finder loop terminates, start popping from the stack in a new loop. The numbers will be popped in descending order (because they were pushed in ascending order).

- 8,598
- 11
- 43
- 60
First, have a look at Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?) for a discussion of better ways of finding prime numbers. Then, use the stack to reverse the order, based on the "last in, first out" property.

- 1
- 1

- 10,353
- 5
- 22
- 30
One interesting property of a stack is that it can be used to reverse order. This is because the first item to be pushed onto it is the last off.
Imagine if I push the letters of the word "pan" onto a stack, one by one. I first push "p", then "a", then "n". Now, since "n" was the last letter pushed, it is the first one to be popped off the stack. So, when I remove the letters I get "n" followed by "a" followed by "p" - "nap". In such a manner, a stack can be used to reverse a word by considering it as a list of characters.
The same is true for a list of prime numbers. If you have a list of the first NUMBER_OF_PRIMES
prime numbers in ascending order (eg: 2, 3, 5, 7 ...), then you can perform the same trick using a stack to change that list into descending order by pushing each onto the stack, then reading them off.
So, what I'd do is each time you detect a prime number, push it onto your stack until there are NUMBER_OF_PRIMES
primes on the stack. Then, pop each item off the stack to print them in reverse order.
It might also be worth spinning off your isPrime
logic into its own function, once you get things running.

- 14,615
- 9
- 62
- 80