Primes

Computing Primes

Your next task is to implement an ancient algorithm known as Sieve of Eratosthenes. The objective is to find all prime numbers up to a specified limit, 100 in your case.

Implementing the algorithm is simple:

  • Create a new file with name sieve.c.
  • Declare a global array with static linkage consisting of 100 elements of type boolean. The boolean value of element i will, at the end of the algorithm, indicate whether i is prime or not.
  • Define a function sieve that takes not argument and does not return any value.
    • Initialize all elements in the array to true, i.e., initially you assume that all numbers might be primes.
    • You then go through the numbers starting with 2, marking all multiples of your current number with false in the array, i.e., the multiples are evidently not primes.
  • Provide a main function that calls sieve, then prints all the primes computed by the algorithm on a separate line (see the lecture slides for the corresponding escape code), and returns EXIT_SUCCESS.
  • Compile and run your code, fixing compiler errors/warning, and verifying that the produced output is correct.

Primes in Columns

Extend your code such that the prime numbers are printed in columns:

  • The output should be printed in three columns.
  • The numbers should be separated by a horizontal tab (see the lecture slides for the corresponding escape code).
  • The last line should end with a line feed in any case.