Table of Contents

Corning Community College

CSCS2330 Discrete Structures

Project: ALGORITHM OPTIMIZATION - PRIME NUMBER COMPUTATION (pnc3)

Errata

With any increasingly complex piece of code or environment, we must find effective means of organizing various processes or communications. This “Errata” section and related updates are one such instance of that; intended as a means of disseminating information on changes to the project, you can be informed what modifications have taken place, along with any unique actions you need to take to compensate.

Any typos, bugs, or other updates/changes to the project will be documented here.

Revision List

Some changes may involve updates being made available to the project, in which case you'll be prompted with such notification and can run the available updating commands to synchronize your copy of the project with the changes.

Objective

To explore the pnc0/pnc1 programs from a different perspective: using only ONE loop to drive central processing.

Background

In mathematics, a prime number is a value that is only evenly divisible by 1 and itself; it has just that one pair of factors, no others. Numbers that have divisibility/factors are classified as composite numbers.

The number 6 is a composite number, as in addition to 1 and 6, it also has the factors of 2 and 3.

The number 17, however, is a prime number, as no numbers other than 1 and 17 can be evenly divided into it.

Calculating the primality of a number

As of yet, there is no quick and direct way of determining the primality of a given number. Instead, we must perform a series of tests to determine if it fails primality (typically by proving it is composite).

This process incurs a considerable amount of processing overhead on the task, so much so that increasingly large values take ever-expanding amounts of time. Often, approaches to prime number calculation involve various algorithms, which offer various benefits (less time) and drawback (more complex code).

Your task for this project is to implement a prime number program using the straightforward, unoptimized brute-force algorithm, which determines the primality of a number in a “trial by division” approach.

Main algorithm: brute force (primerol)

The brute force approach is the simplest to implement (although at some cost).

As we will be looking to do some time/performance analysis and comparisons, it is often good to have a baseline. This program will be it.

To perform the process of computing the primality of a number, we simply attempt to evenly divide all the values between 2 and one less than the number in question. If any one of them divides evenly, the number is NOT prime, but instead composite.

Checking the remainder of a division indicates whether or not a division was clean (having 0 remainder indicates such a state).

For example, the number 11:

11 % 2 = 1 (2 is not a factor of 11)
11 % 3 = 2 (3 is not a factor of 11)
11 % 4 = 3 (4 is not a factor of 11)
11 % 5 = 1 (5 is not a factor of 11)
11 % 6 = 5 (6 is not a factor of 11)
11 % 7 = 4 (7 is not a factor of 11)
11 % 8 = 3 (8 is not a factor of 11)
11 % 9 = 2 (9 is not a factor of 11)
11 % 10 = 1 (10 is not a factor of 11)

Because none of the values 2-10 evenly divided into 11, we can say it passed the test: 11 is a prime number

On the other hand, take 119:

119 % 2 = 1 (2 is not a factor of 119)
119 % 3 = 2 (3 is not a factor of 119)
119 % 4 = 3 (4 is not a factor of 119)
119 % 5 = 4 (5 is not a factor of 119)
119 % 6 = 5 (6 is not a factor of 119)
119 % 7 = 0 (7 is a factor of 119)
119 % 8 = 7
119 % 9 = 2
119 % 10 = 9
119 % 11 = 9
119 % 12 = 11
119 % 13 = 2
...

Because, during our range of testing every value from 2-118, we find that 7 evenly divides into 119, it failed the test: 119 is not prime, but is instead a composite number.

Please NOTE: Even once a number is identified as composite, your primerol MUST CONTINUE evaluating the remainder of the values (up to 119-1, or 118). It might seem pointless (and it is for a production program), but I want you to see the performance implications this creates.

algorithm

Some things to keep in mind on your implementation:

prime algorithm optimizations

To give us a decent appreciation of the subtleties of algorithm development in a theme of programs, I have identified the following optimizations that we will be implementing.

For simplicity, I have encoded this information in the file name (and therefore resulting executable/binary) that will correspond to the indicated algorithm+optimizations.

To break it down, all prime programs will be of the form:

The optimizations we will be implementing in this project (and their naming values) include:

Unless specified in the encoded name, your algorithm should only implement the algorithm and optimization(s) specified.

That is, if your program to implement is primerolo, that means you are ONLY to implement the brute force algorithm and odds-only checking. NO break on composite, NO sqrt() trick, etc. We are establishing separate data points for analytical comparison.

Some of these optimizations can co-exist easily (break + map, odd + sqrt()), others are partially compatible (map + odd can coexist in a certain form), while others are mutually exclusive (sqrt() and approximated square root conflict). So there are definitely a few combinations that are possible using this scheme.

A note on comments

Something I noticed (and have historically noticed) in relation to comments that I'd like to point out:

Comments should be describing what is going on in your code.

With projects like this, often relying on a common base, comments become even more important, as they allow me to see specifically what is changed or unique about one variant over the other.

As such, when evaluating the project, I will be looking for pertinent comments specifically covering the how or why of the particular change unique to the variant in question.

And notice I said the “how” and/or “why”. NOT the “what”. I see all the time vague comments like “// doing the sqrt() optimization”… but:

These are things I'd like to see addressed in your comments, as there were some cases where the WHAT was claimed, yet what actually followed had little resemblance (if any) on the requirements for that variant.

Just like if you can't do it by hand you have no business trying to code it- if you cannot adequately explain the WHY and HOW, you similarly will have trouble.

Programs

It is your task to write the following prime number variants:

Program Specifications

Your program should:

Grabit Integration

For those familiar with the grabit tool on lab46, I have made some skeleton files and a custom Makefile available for this project.

To “grab” it:

lab46:~/src/discrete$ grabit discrete pnc3
make: Entering directory '/var/public/SEMESTER/discrete/pnc3'
Commencing copy process for SEMESTER discrete project pnc3:
 -> Creating project pnc3 directory tree           ... OK
 -> Copying pnc3 project files                     ... OK
 -> Synchronizing pnc3 project revision level      ... OK
 -> Establishing sane file permissions for pnc3    ... OK

*** Copy COMPLETE! You may now go to the '/home/USER/src/discrete/pnc3' directory ***

make: Leaving directory '/var/public/SEMESTER/discrete/pnc3'
lab46:~/src/discrete/$ 
lab46:~/src/discrete$ cd pnc3
lab46:~/src/discrete/pnc3$ 

NOTE: You do NOT want to do this on a populated pnc3 project directory– it will overwrite files.

And, of course, your basic compile and clean-up operations via the Makefile.

Makefile operations

Makefiles provide a build automation system for our programs, instructing the computer on how to compile files, so we don't have to constantly type compiler command-lines ourselves. I've also integration some other useful, value-added features that will help you with overall administration of the project.

Basic operation of the Makefile is invoked by running the command “make” by itself. The default action is to compile everything in the project directory.

Additional options are available, and they are provided as an argument to the make command. You can see the available options by running “make help”:

lab46:~/src/discrete/pnc3$ make help
******************[ Discrete Structures pnc3 Project ]******************
** make                     - build everything                        **
** make showerrors          - display compiler warnings/errors        **
** make debug               - build everything with debug symbols     **
** make checkqty            - runtime evaluation for qty              **
** make checkrange          - runtime evaluation for range            **
**                                                                    **
** make verifyqty           - check implementation for qty validity   **
** make verifyrange         - check implementation for range validity **
** make verifyall           - verify project specifications           **
**                                                                    **
** make link                - link in previous prime programs         **
** make delink              - remove links to previous prime programs **
**                                                                    **
** make save                - create a backup archive                 **
** make submit              - submit assignment (based on dirname)    **
**                                                                    **
** make update              - check for and apply updates             **
** make reupdate            - re-apply last revision                  **
** make reupdate-all        - re-apply all revisions                  **
**                                                                    **
** make clean               - clean; remove all objects/compiled code **
** make help                - this information                        **
************************************************************************
lab46:~/src/discrete/pnc3$ 

A description of some available commands include:

The various “check” options do a runtime performance grid, allowing you to compare timings between your implementations.

The various “verify” options do more aggressive checks, helping to ensure your project falls within stated project specifications.

Just another “nice thing” we deserve.

Command-Line Arguments

To automate our comparisons, we will be making use of command-line arguments in our programs.

header files

We don't need any extra header files to use command-line arguments, but we will need an additional header file to use the atoi(3) function, which we'll use to quickly turn the command-line parameter into an integer, and that header file is stdlib.h, so be sure to include it with the others:

#include <stdio.h>
#include <stdlib.h>

setting up main()

To accept (or rather, to gain access) to arguments given to your program at runtime, we need to specify two parameters to the main() function. While the names don't matter, the types do.. I like the traditional argc and argv names, although it is also common to see them abbreviated as ac and av.

Please declare your main() function as follows:

int main(int argc, char **argv)

There are two very important variables involved here (the types are actually what are important, the names given to the variables are actually quite, variable; you may see other references refer to them as things like “ac” and “av”):

The arguments are accessible via the argv array, in the order they were specified:

Additionally, let's not forget the argc variable, an integer, which contains a count of arguments (argc == argument count). If we provided argv[0] through argv[4], argc would contain a 5.

example

For example, if we were to execute the primerolbms program:

lab46:~/src/discrete/pnc3$ ./primerolbms 128 1 2 2048

We'd have:

and let's not forget:

With the conditionally optional arguments as part of the program spec, for a valid execution of the program, argc could be a value anywhere from 3 to 5.

Simple argument checks

While there are a number of checks we should perform, one of the first should be a check to see if the minimal number of arguments has been provided:

    if (argc < 3)  // if less than 3 arguments (program_name + quantity + argv[2] == 3) have been provided
    {
        fprintf(stderr, "%s: insufficient number of arguments!\n", argv[0]);
        exit(1);
    }

Since argv[3] (lower bound) and argv[4] (upper bound) are conditionally optional, it wouldn't make sense to check for them in the overall count. But we can and do still want to stategically utilize argc to determine if an argv[3] or argv[4] is present.

Grab and convert max

Finally, we need to put the argument representing the maximum quantity into a variable.

I'd recommend declaring a variable of type int.

We will use the atoi(3) function to quickly convert the command-line arguments into int values:

    max  = atoi (argv[1]);

And now we can proceed with the rest of our prime implementation.

Timing

Often times, when checking the efficiency of a solution, a good measurement (especially for comparison), is to time how long the processing takes.

In order to do that in our prime number programs, we are going to use C library functions that obtain the current time, and use it as a stopwatch: we'll grab the time just before starting processing, and then once more when done. The total time will then be the difference between the two (end_time - start_time).

We are going to use the gettimeofday(2) function to aid us in this, and to use it, we'll need to do the following:

header file

In order to use the gettimeofday(2) function in our program, we'll need to include the sys/time.h header file, so be sure to add it in with the existing ones:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

timeval variables

gettimeofday(2) uses a struct timeval data type, of which we'll need to declare two variables in our programs (one for storing the starting time, and the other for the ending time).

Please declare these with your other variables, up at the top of main() (but still WITHIN main()– you do not need to declare global variables).

    struct timeval time_start; // starting time
    struct timeval time_end;   // ending time

Obtaining the time

To use gettimeofday(2), we merely place it at the point in our code we wish to take the time.

For our prime number programs, you'll want to grab the start time AFTER you've declared variables and processed arguments, but JUST BEFORE starting the driving loop doing the processing.

That call will look something like this:

    gettimeofday(&time_start, 0);

The ending time should be taken immediately after all processing (and prime number output) is completed, and right before we display the timing information to STDERR:

    gettimeofday(&time_end, 0);

Displaying the runtime

Once we have the starting and ending times, we can display this to the timing file pointer. You'll want this line:

fprintf(timing, "%8.4lf\n",
time_end.tv_sec-time_start.tv_sec+((time_end.tv_usec-time_start.tv_usec)/1000000.0));

For clarity sake, that format specifier is “%8.4lf”, where the “lf” is “long float”, that is NOT a number 'one' but a lowercase letter 'ell'.

And with that, we can compute an approximate run-time of our programs. The timing won't necessarily be accurate down to that level of precision, but it will be informative enough for our purposes.

Execution

specified quantity

Your program output should be as follows (given the specified quantity):

lab46:~/src/discrete/pnc3$ ./primeroloa 24 1
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 
  0.0001
lab46:~/src/discrete/pnc3$ 

The execution of the programs is short and simple- grab the parameters, do the processing, produce the output, and then terminate.

invalid lower bound

Here's an example that should generate an error upon running (based on project specifications):

lab46:~/src/discrete/pnc3$ ./primerolbmo 32 1 0
./primerolbmo: invalid lower bound
lab46:~/src/discrete/pnc3$ 

In this case, the program logic should have detected an invalid condition and bailed out before prime computations even began. No timing data is displayed, because exiting should occur even prior to that.

upper bound overriding quantity

As indicated above, there is potential interplay with an active quantity and upper bound values. Here is an example where upper bound overrides quantity, resulting in an early termination (ie upper bound is hit before quantity):

lab46:~/src/discrete/pnc3$ ./primerolos 128 1 7 23
7 11 13 17 19 23
  0.0001
lab46:~/src/discrete/pnc3$ 

Also for fun, I set the lower bound to 7, so you'll see computation starts at 7 (vs. the usual 2).

Check Results

If you'd like to compare your implementations, I rigged up a Makefile checking rule called “make checkqty” and “make checkrange” which you can run to get a nice side-by-side runtime comparisons of your implementations.

In order to work, you MUST be in the directory where your pnc0 binaries reside, and must be named as such (which occurs if you ran make to compile them).

check qty

For instance (running on my implementation of the pnc3 programs, some output omitted to keep the surprise alive):

lab46:~/src/discrete/pnc3$ make checkqty

check range

Or check range runtimes:

lab46:~/src/discrete/pnc3$ make checkrange

If the runtime of a particular prime variant exceeds an upper runtime threshold (likely to be set at 1 second), it will be omitted from further tests, and a series of dashes will instead appear in the output.

If you don't feel like waiting, simply hit CTRL-c (maybe a couple of times) and the script will terminate.

Verification

I also include a validation check- to ensure your prime programs are actually producing the correct list of prime numbers. If the check is successful, you will see “OK” displayed beneath in the appropriate column; if unsuccessful, you will see “MISMATCH”.

Full Verification Compliance

There's also a more rigorous verification step you can take, which runs your programs through a series to tests to see if they conform to project specifications:

lab46:~/src/discrete/pnc3$ make verifyall

verifyall tests

The “verifyall” is an industrial grade verification; there are 13 specific tests performed, they are:

If you'd actually to see the output your program's output is being tested against, that can be found in the /usr/local/etc directory in the file primeTEST, where “TEST” is the name of the verify test mentioned above.

For example, if you wanted to see the intended output of the invnary test, that would be found in:

You could easily run your program with the stated arguments for the test, then use cat to display the test results and do a visual comparison.

In general

Analyze the times you see… do they make sense, especially when comparing the algorithm used and the quantity being processed? These are related to some very important core Computer Science considerations we need to be increasingly mindful of as we design our programs and implement our solutions. Algorithmic complexity and algorithmic efficiency will be common themes in all we do.

Submission

To successfully complete this project, the following criteria must be met:

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

lab46:~/src/discrete/pnc3$ make submit
removed ‘errors’

Project backup process commencing

Taking snapshot of current project (pnc3)      ... OK
Compressing snapshot of pnc3 project archive   ... OK
Setting secure permissions on pnc3 archive     ... OK

Project backup process complete

Submitting discrete project "pnc3":
    -> ../pnc3-20171101-16.tar.gz(OK)

SUCCESSFULLY SUBMITTED

You should get that final “SUCCESSFULLY SUBMITTED” with no error messages occurring. If not, check for typos and or locational mismatches.

Evaluation Criteria

Grand total points:

182:pnc3:final tally of results (182/182)

What I will be looking for (for each file):

*:pnc3:primeALGO.c compiles cleanly, no compiler messages [1/1]
*:pnc3:primeALGO.c implements only specified algorithm [2/2]
*:pnc3:primeALGO.c consistent indentation throughout code [1/1]
*:pnc3:primeALGO.c relevant comments throughout code [1/1]
*:pnc3:primeALGO.c code conforms to project specifications [2/2]
*:pnc3:primeALGO.c runtime output conforms to specifications [1/1]
*:pnc3:primeALGO.c make checkqty test times within reason [1/1]
*:pnc3:primeALGO.c make checkrange test times within reason [1/1]
*:pnc3:primeALGO.c make verifyall tests succeed [3/3]

As the optimizations improve upon others, some evaluations will be based upon differences between a baseline (in some cases, primereg) and the optimization.

Additionally:

As the optimizations improve upon others, some evaluations will be based upon differences between a baseline (in some cases, primerol) and the optimization.