/* primes.c - calculate the first twenty prime numbers */ /* Analog Devices, 1993 */ main() { int index = 0; int n_primes = 1; int primes[20] = {2}; int testnum = 3; while (n_primes < 20) { /* find a number that is indivisible by known primes. */ while (index < n_primes) if (!(testnum % primes[index++])) { /* if a number is divisible by a known prime, it cannot be prime. Start over with the next odd number */ testnum += 2; index = 0; } /* this number is prime, add it to the list. */ primes[n_primes++] = testnum; /* start checking at the next odd number */ testnum += 2; index = 0; } exit(0); }