CS 141 Programming Assignment 2

Prime Numbers


Overview

For programming assignment 2, you are going to search for prime numbers. As a reminder, a prime number is a whole number greater than 1 that is divisible only by itself and 1.

Your program must accept a maximum number as a parameter on the command line. Then it must search for all the prime numbers less than or equal to this number. For example, if the user ran your program like this:

./a.out 5

You would need to find 2,3 and 5.

If the user fails to supply an argument or if the argument is not a number between 2 and the maximum unsigned integer then you should print an error message and exit the program.

Important modification: Your program should accept the following arguments:

./a.out -slow 5

./a.out -fast 5

If the slow option is passed in, it should search for primes in a completely non-optimized fashion (e.g. full loop over all numbers 2 through n-1). If the fast option is passed in, it should search for primes in an optimized fashion. The default behaviour of this option is not supplied should be the optimized or fast option.

Part of your assignment is to do this task as efficiently as possible. You can do this by minimizing the total number of divisions (or other operations like multiplication or mod) while still be sure to correctly classify all numbers in the range as either prime or composite. We will be testing your program both for correctness and for speed!

In addition to the list of primes (one on each line and in increasing order), you should print out the total number of primes found, the maximum prime found and the total number of operations like this:

2
3
5
total_primes 3
max_prime 5

You should be careful to print exactly these messages - spelling, order, lowercase letters included - as we expect to test your program using automated scripts. For the same reason, your program should not ask the user for any input except for the maximum number which is provided on the command line.

The heart of this code will be a set of loops. A loop to try all possible numbers to see if they are a divisor. Consider that the most straightforward way would be to try to divide the number given by the user (n) on all divisors from 2 up through n-1. However, this is inefficient in many ways. Consider how you can avoid trying as many of these potential divisors as possible without compromising the accuracy of your answer.

In your code, you should clearly document all optimizations you make to save divisions by writing clear comments next to the optimization in the code.

Remember to start early!! Little logistical problems solved with a quick question or an email, turn into major crisis the night before the due date when no one is available to help. Also if you don't start early, you won't have time for testing with lots of users. Determine which office hours fit your schedule and plan to start the assignment before those!!! Then come in for help if you need it.


What to Turn In

You should turn in the assignment to the directory /afs/cu/class/cs141/students/YOUR_USERNAME/prog2 (e.g. I would put mine in /afs/cu/class/cs141/students/jmatthew/prog2). There should be three files:

  • A prog2.cpp file with the source code of your working program. The program must compile and run on polaris.clarkson.edu using g++. Remember to choose good variable names, use consistent, clear indentation and add good comments to explain your code.
  • A transcript of the program being run (transcript.txt).
  • A file that contains timings of your program being run in both fast and slow mode on a variety of numbers including the largest number you have successfully run on. You can use the time command to do this (timing.txt).