What is the most elegant way to implement this function:
ArrayList generatePrimes(int n)
This function generates the first n primes (edit: where n>1), so generatePrimes(5) will return an ArrayList with {2, 3, 5, 7, 11}. (I’m doing this in C#, but I’m happy with a Java implementation – or any other similar language for that matter (so not Haskell)).
I do know how to write this function, but when I did it last night it didn’t end up as nice as I was hoping. Here is what I came up with:
ArrayList generatePrimes(int toGenerate)
{
ArrayList primes = new ArrayList();
primes.Add(2);
primes.Add(3);
while (primes.Count < toGenerate)
{
int nextPrime = (int)(primes[primes.Count - 1]) + 2;
while (true)
{
bool isPrime = true;
foreach (int n in primes)
{
if (nextPrime % n == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
break;
}
else
{
nextPrime += 2;
}
}
primes.Add(nextPrime);
}
return primes;
}
I’m not too concerned about speed, although I don’t want it to be obviously inefficient. I don’t mind which method is used (naive or sieve or anything else), but I do want it to be fairly short and obvious how it works.
Edit: Thanks to all who have responded, although many didn’t answer my actual question. To reiterate, I wanted a nice clean piece of code that generated a list of prime numbers. I already know how to do it a bunch of different ways, but I’m prone to writing code that isn’t as clear as it could be. In this thread a few good options have been proposed:
- A nicer version of what I originally had (Peter Smit, jmservera and Rekreativc)
- A very clean implementation of the sieve of Eratosthenes (starblue)
- Use Java’s
BigIntegers andnextProbablePrimefor very simple code, although I can’t imagine it being particularly efficient (dfa) - Use LINQ to lazily generate the list of primes (Maghis)
- Put lots of primes in a text file and read them in when necessary (darin)
Edit 2: I’ve implemented in C# a couple of the methods given here, and another method not mentioned here. They all find the first n primes effectively (and I have a decent method of finding the limit to provide to the sieves).
Many thanks to all who gave helpful answers. Here are my implementations of a few different methods of finding the first n primes in C#. The first two methods are pretty much what was posted here. (The posters names are next to the title.) I plan on doing the sieve of Atkin sometime, although I suspect it won’t be quite as simple as the methods here currently. If anybody can see any way of improving any of these methods I’d love to know 🙂
Standard Method (Peter Smit, jmservera, Rekreativc)
The first prime number is 2. Add this to a list of primes. The next prime is the next number that is not evenly divisible by any number on this list.
This has been optimised by only testing for divisibility up to the square root of the number being tested; and by only testing odd numbers. This can be further optimised by testing only numbers of the form
6k+[1, 5], or30k+[1, 7, 11, 13, 17, 19, 23, 29]or so on.Sieve of Eratosthenes (starblue)
This finds all the primes to k. To make a list of the first n primes, we first need to approximate value of the nth prime. The following method, as described here, does this.
Sieve of Sundaram
I only discovered this sieve recently, but it can be implemented quite simply. My implementation isn’t as fast as the sieve of Eratosthenes, but it is significantly faster than the naive method.