开发者

how do i output prime numbers in descending order using a stack?

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++;

        i开发者_C百科f(count % NUMBER_OF_PRIMES_PER_LINE ==0){
            System.out.println(number);

        }
        else
            System.out.print(number + " ");

    }
    number++;
    }
}
}


  1. Read the javadoc for Stack and its parent class : Vector
  2. When computing the 50 first prime members, instead of displaying them while you find them, store them in the stack
  3. 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.


Because it's a homework problem I won't give code, but here's the process, picking up from somewhere within your code.

  1. If number is prime, push it onto the stack.
  2. 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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜