I've been given a description of how I am to implement a prime number generator using Java; but I'm not sure if I am doing it properly. I was hoping someone could give me some general advice for how to improve my implementation! Please don't give me an answer as this is homework and I'd like to crack this myself/become a better programmer/not cheat !
I will post the description and my solution below, basically I would just like to know some general things - have I followed the instructions properly (most important as I find the language used a little confusing) and if I could make my program any better (if so some general tips would be nice so I can figure out how...I think my use of the logarithm is way off, but I can't figure out a better way to use it)!
Apologies if this isn't allowed - if it isn't then please ignore or delete ! Thank you very much for your help! :)
Description :
Prime( int initialCapacity ): The class constructor. The purpose of the constructor is to compute and store the first initialCapacity prime first initialCapacity primefirst initialCapacity primefirst i numbers. The idea is that you compute the numbers once and store them so that you can re-use them several times. You should use an ArrayList to store the prime numbers. An algorithm for computing prime numbers is provided in the next section.
Start with an empty list of prime numbers. You may represent this list using an ArrayList.
While the size of the ArrayList is not equal to n, consider the next candidate prime number. Initially, the next candidate prime number should be 2, but otherwise it should be the successor of the previous candidate prime number.
If the candidate prime number is a prime then add it to the list.
Continue with Step 2.
Note that this algorithm suggests that you should use a while loop. Step 3 of the previous algorithm requires that you know how to decide whether a given candidate prime, c > 1, is a prime number. As a matter of fact, the de
精彩评论