Prime numbers

This program calculates and displays the first 1,000 prime numbers (including 1, 2 and 3).

Programming Issues

To test if a number n is prime, we could loop through 2 to n - 1 and test whether each number divides exactly into n ((n % test) == 0). If any of them do, the number is not prime.

However, since by definition any number which is not prime can be divided by at least one other prime number, a more efficient way to do it is to test only the prime numbers less than n. Therefore, our program maintains a list of prime numbers already found, and uses that to test n.


Just type ./prime to run the program

Source and Downloads