User Tools

Site Tools


notes:comporg:projects:pnc0

This is an old revision of the document!


Corning Community College

CSCS2650 Computer Organization

Project: IMPLEMENTATIONS AND OPTIMIZATIONS - PRIME NUMBER COMPUTATION (pnc0)

Objective

To apply your skills in the implementation of several prime number computing algorithms across varying languages and utilizing various forms of optimization.

Background

In mathematics, a prime number is a value that is only evenly divisible by 1 and itself; it has no other factors. Numbers that have divisibility/factors are known as composite numbers.

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

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

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.

brute force

The brute force approach is the simplest to implement (and likely also the worst-performing). We will use it as our baseline (it is nice to have something to compare against).

To perform it, 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 a composite value.

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)

Because 7 evenly divided into 119, it failed the test: 119 is not a prime, but instead a composite number.

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

Timing

Measuring time can help us determine the efficiency of the program because we can determine if the runtime was faster or not.

Header file

In order to use the gettimeofday(2) function, we need to include sys/time.h in our program.

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

The prototype for this function is as follows:

int gettimeofday(struct timeval *tv, struct timezone *tz);

Since we need a place to store in the values and the parameter that stores the value is a struct timeval datatype, we need to declare the variable as follows:

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

First, we need to set up our initial time. We can do this by doing a call something like this:

gettimeofday(&time_start, 0);

We would want to put this at the start of our program after all the declaration and error checking BUT before the main algorithm. Now that we have our starting time, we would also need a ending time. This call should be after all the processing is complete, and before we display the timing information to STDERR.

gettimeofday(&time_end, 0);

Displaying the runtime

Now that we got both starting and ending time, all we need to do is print out the difference in time. You can display this by using the following:

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

Programs - Pick at least 5

primereg - baseline program (Brute force, no break)

primeregs - baseline, plus square root trick (Use <cmath> header for sqrt())

primeregb - baseline, plus break on composite

primeregbs - baseline, plus square root trick, plus break on composite

Choice of A, R, or L
primeregAbs - baseline, array, square root trick, break on composite

Note: (Store prime divisors into array and check against the array)
Array size(amount of elements) == argument 1(number of primes)

primeregRbs - Recursive brain melter ?
primeregLbs - Linked List

Program Format

./primereg numberOfPrimes
The argument notates how many primes (starting from 2) that you want to print.
Example: ./primereg 8 results in printing out: 2 3 5 7 11 13 17 19

-Put all c files in gcc directory from pnc0 grabit
-Numbers go to stderr, time goes to stdout
-Use pncrun executable to test programs

notes/comporg/projects/pnc0.1516287783.txt.gz · Last modified: 2018/01/18 15:03 by ktodd3