Exercise 4-12 - CBD page 166 - Jan Dimon.

Solution index                   Textual C program


/**
 * PRIMESMAIN.C 
 *
 * Exercise solution 2.5.  (C By Dissection Chap. 4, ex. 12)
 *
 * Computes a list of prime numbers.
 * 26-Feb-03 by Dimon
 **/


/**
 * INCLUDE statements 
 **/
#include "primes.h"


/**
 * The main function
 **/
int main(void) {  
  
  /* Declaration of variables */

  int i = 1, numPrimes = 0, value = 2; 
  int status = 0;


  /* Display program header and enter number of primes to calculate */
  
  printf("V1: Primes will be printed\n");
  printf("----------------------\n");
  printf("\n");
  printf("How many do you want to see? ");
  scanf("%d",&numPrimes);


/* 2 is the only even prime number, so it is treated as a special
   case */

  if (numPrimes > 0) { 
    printf("%6d:  %10d", i, value); 
  }
  printf("\n");
  value = 3; /* value is set to the second smallest prime */


  /* A FOR loop calculates and prints out the results */ 

  for(i = 2; i <= numPrimes; i++) {
    while (!is_prime(value)) {
      value += 2; /* Only the odd integers need to be tested */ 
    }
    printf("%6d:  %10d", i, value); 
    printf("\n");
    value += 2; /* Increment to the next potential prime number */ 
  } /* End of FOR loop */

  /* Terminate program */
  return status;    

} /* END OF MAIN */




#include "primes.h"
Before int main(void), we include the header file containing the function signature. The compiler thus knows the name of the function isPrime, the input argument type and the return type.
 

  if (numPrimes > 0) { 
    printf("%6d:  %10d", i, value); 
  }
  printf("\n");
  value = 3; /* value is set to the second smallest prime */
The variables i and value were initialized to 1 and 2, respectively. If the user asked for at least one prime number to be printed out, we know that the first one is going to be 2. 2 is the only even prime number, so it is treated as a special case. When the first prime number is printed out, we set value to 3, the smallest odd number greater than 2.
 

  for(i = 2; i <= numPrimes; i++) {
A FOR loop calculates and prints out the results. The counting variable i is set to start at 2, since we have already printed out the first prime number. The loop is executed (numPrimes - 1) times.
 

    while (!is_prime(value)) {
      value += 2; /* Only the odd integers need to be tested */ 
    }
Each time the FOR-loop is executed, we run an inner loop that determines the next prime number. The WHILE loop runs as long as its argument is different from 0. isPrime() is called with value as argument and returns 1/0 if value is/is not a prime number, respectively. The ! operator negates the result, i.e., the WHILE loop runs for as long as value is not a prime. Each time the loop is executed, value is incremented by 2, as we do not need to test even numbers (a little trick, which does matter in terms of execution speed when the prime numbers become large).
 

    printf("%6d:  %10d", i, value); 
    printf("\n");
    value += 2; /* Increment to the next potential prime number */ 
  } /* End of FOR loop */
When a prime number has been found, the WHILE loop terminates, value is ncremented to the next potential prime number, and the FOR loop can be executed again if there are more primes to be found.
 


Generated: Wednesday, March 29, 2006, 12:33:22
This program dissection page is generated from an XML-in-LAML source file