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
denition of prime numbers already
suggests a naive algorithm. For
example, you could use a while loop to
check that c %i 6= 0 for all positive
integers i such that 1 < i and i < c.
However, it is not dicult to see that
a much better method is making sure
that c % p 6= 0 for all prime numbers
p such that p < c. Using the primes in
your ArrayList this is easy as pie.is
method is the method which you are
supposed to use. Note again that this
suggest that you use a while loop.
Finally, your implementation should be
ecient and should not waste checks of
the form c % p 6= 0 or c % p = 0. So,
you should stop cheking as soon as c %
p = 0 for some of the primes in
ArrayList.
My solution is as follows :
import java.util.*;
public class Prime {
public static void main( String[] args ){
}
public static void prime( int initialCapacity){
int index=2;
int logOfInitialCapacity = initialCapacity / (int)(Math.log(initialCapacity));
ArrayList<Integer> listOfPrimeNumbers = new ArrayList<Integer>(logOfInitialCapacity);
boolean[] isPrimeNumber = new boolean[initialCapacity + 1]; // boolean defaults to
// false
for (int i=0;i==initialCapacity;i++) {
isPrimeNumber[index] = true;
}
while ( index <= listOfPrimeNumbers.size() )
{
if (isPrimeNumber[index]) {
listOfPrimeNumbers.add(index);
}
for (int j = index; j * index <= initialCapacity; j++) {
isPrimeNumber[index * j] = false;
}
// Now mark the multiple of i as non-prime number
index++;
}
}
}
which should compute the first initialCapacity of prime numbers and save them in an arrayList as the question asks…I’m just not sure if I have done exactly as the question says. It’s probably more a product of studying all day and being a little worn out than anything else !
Thanks a lot for the help and for reading all of this; sorry for the long post.
Your solution doesn’t really match the problem description.
The problem description prescribes primality testing (i.e. given a candidate prime, test whether it is prime). It also wants
initialCapacityprimes.The problems with your solution:
initialCapacity(not the firstinitialCapacityprimes – to see the difference, tryinitialCapacity = 1).