Homework 2: Sieve of Eratosthenes

Due: 01/23/05

Educational Objectives: Experience using bit vector objects in applications; experience using makefiles to organize and compile multifile applications

Operational Objectives: Provide a client program sieve.cpp for the class fsu::BitVector that implements the Sieve of Eratosthenes to compute all prime numbers less than or equal to an input integer, and provide a makefile to create an executable sieve.x

Deliverables: Two files sieve.cpp and makefile

The Sieve of Eratosthenes Algorithm:

Given a bitvector b, indexed from 0 to n, the sieve algorithm begins with all bits set and ends with b[k] set iff k is prime. The Sieve makes use of two useful observations about prime numbers.

The first useful observation is that, if n is not prime, then n is divisible by a prime. In other words, the non-prime numbers are precisely the numbers that are multiples of smaller prime numbers.

The second useful observation is that if a number is not prime, then it is divisible by a prime that is no greater than the square root of the number.

Using these observations to ensure correctness, the sieve algorithm goes as follows:

  1. Begin with a bitvector b indexed from 0 to n.
  2. Initialize b by setting all bits.
  3. Clear b[0] and b[1] (because 0 and 1 are not prime).
  4. For k between 2 and the square root of n, stepsize 1:
  5. Stop.

In short, clear the bits of all multiples of primes less than the square root of n.

After invoking the sieve, an integer k in the range 0 < k < n is prime iff b[k] is set. For, if k is not prime then k is a multiple of a prime less than the square root of n and b[k] would have been cleared by the algorithm. Conversely, any cleared bit is indexed by such a multiple.

Procedural Requirements

  1. All of the work for this assignment should be done in your subdirectory cop4530/hw2.

  2. Begin by copying the file LIB/submitscripts/hw2submit.sh into your assignment directory and changing its permissions to 700.

  3. Create the files sieve.cpp and makefile, according to the specifications below.

  4. Turn in the two files sieve.cpp and makefile using the script hw2submit.sh.

  5. Warning: Submit scripts do not work on the program and linprog servers. Use shell.cs.fsu.edu to submit this assignment. If you do not receive the second confirmation with the contents of your project, there has been a malfunction.

Technical Requirements and Specificatons

  1. The file sieve.cpp should contain two functions:

    void sieve (fsu::BitVector& bv)
    int main()
    
  2. Function sieve() takes an fsu::BitVector object bv by reference and performs the sieve algorithm on it, so that when the function returns, bits of bv are set if and only if the index of the bit is a prime number. Note that sieve() is a function only. It does not do any interaction with users, streams, or files.

  3. Function main() should handle the I/O: query the user for a positive integer and output a list of all prime numbers that are less than or equal to the input. (See example executable.)

  4. Write a makefile for your project that creates object files bitvect.o, sieve.o and one executable called sieve.x.

  5. Your makefile should create bitvect.o directly from the course library directory, that is, without copying the source code into your project directory.

Hints: