User Tools

Site Tools


Sidebar

projects

  • cci0 (due 20160127)
  • mms0 (due 20160203)
  • dow0 (due 20160210)
  • mbe0 (due 20160224)
  • pnc0 (due 20160302)
  • mbe1 (due 20160309)
  • cos0 (due 20160316)
  • sam0 (due 20160323)
  • cbf0 (due 20160406)
  • afn0 (due 20160413)
  • gfo0 (due 20160420)
haas:spring2016:cprog:projects:pnc0

This is an old revision of the document!


Corning Community College

CSCS1320 C/C++ Programming

~~TOC~~

Project: ALGORITHMS - PRIME NUMBER CALCULATION (pnc0)

Objective

To apply your skills in the implementation of prime number calculating algorithms.

Prerequisites/Corequisites

In addition to the new skills required on previous projects, to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • ability to make decisions (if statement)
  • ability to iterate sections of code (for/while statement)

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 increasing 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 3 prime number programs:

  1. brute force prime calculation
  2. square root-optimized brute force calculation
  3. a further optimized solution

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 1 and 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.

There is no further need to check the remaining values, as once we have proven the non-primality of a number, the state is set: it is composite. So be sure to use a break statement to terminate the computation loop (will also be a nice boost to runtime).

square root

An optimization to the computation of prime numbers is the square root trick. Basically, if we've processed numbers up to the square root of the number we're testing, and none have proven to be evenly divisible, we can also assume primality and bail out.

The C library has a sqrt() function available through including the math.h header file, and linking against the math library at compile time (add -lm to your gcc line).

To use sqrt(), we pass in the value we wish to obtain the square root of, and assign the result to an int:

int x = 25;
int y = 0;
 
y = sqrt(x);
 
// y should be 5 as a result

For instance, the number 37 (using the square root optimization), we find the square root (whole number) of 37 is 6, so we only need to check 2-6:

37 % 2 = 1 (2 is not a factor of 37)
37 % 3 = 1 (3 is not a factor of 37)
37 % 4 = 1 (4 is not a factor of 37)
37 % 5 = 2 (5 is not a factor of 37)
37 % 6 = 1 (6 is not a factor of 37)

Because none of these values evenly divides, we can give 37 a pass: it is a prime

This will dramatically improve the runtime, and offers a nice comparison against our brute force baseline.

further optimization

There are many other methods, approaches, and tweaks that can be employed to further improve runtime (while maintaining accuracy– all your solutions must match: the same prime numbers should be identified no matter which program is run).

So I'd like you to explore other optimizations that can be made, be it using other prime number algorithms, further refining existing ones, or playing off patterns in numbers.

One assumption I will allow you to make for your optimized solution is that the single-digit primes (2, 3, 5, 7) can be assumed prime, and just printed out if having them be calculated would otherwise break your algorithm (might be helpful to some people; I certainly found it useful in some of my solutions).

Program

It is your task to write the program that will use the above method to compute the requested one-, two-, or three-digit value against a multiplicand of 11 (without using any multiplication to obtain your result).

Your program should:

  • obtain its input from STDIN.
    • input should be in the form of a single integer value
  • determine from the input if it is a one-, two-, or three-digit number
  • perform the correct algorithm against the input
  • propagate any carries
  • output the final value
    • you can display each digit individually, or combine them into one variable and display that (whichever you prefer)

Execution

Several operating behaviors are shown as examples.

A two digit value:

lab46:~/src/cprog/mbe0$ ./multby11
Enter value: 32
32 x 11 = 352
lab46:~/src/cprog/mbe0$ 

Next, a one digit value:

lab46:~/src/cprog/mbe0$ ./multby11
Enter value: 7
7 x 11 = 77
lab46:~/src/cprog/mbe0$ 

Finally, three digit value:

lab46:~/src/cprog/mbe0$ ./multby11
Enter value: 567
567 x 11 = 6237
lab46:~/src/cprog/mbe0$ 

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

Bonus Points

There will be an additional bonus point opportunity with this project, based on processing run-time of your optimized solution.

Submission

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

  • Code must compile cleanly (no warnings or errors)
  • Output must be correct, and match the form given in the sample output above.
  • Code must be nicely and consistently indented (you may use the indent tool)
  • Code must utilize the algorithm(s) presented above:
    • primebrute.c must do the unoptimized brute force method
    • primesqrt.c must utilize the brute force method along with the square root trick (no other tricks)
    • primeopt.c is your attempt at optimizing the code, using whatever additional tricks (or enhancements to tricks) to further improve runtime.
  • Code must be commented
    • have a properly filled-out comment banner at the top
      • be sure to include any compiling instructions
    • have at least 20% of your program consist of //-style descriptive comments
  • Output Formatting (including spacing) of program must conform to the provided output (see above).
  • Track/version the source code in a repository
  • Submit a copy of your source code to me using the submit tool.

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

$ submit cprog pnc0 primebrute.c primesqrt.c primeopt.c
Submitting cprog project "pnc0":
    -> primebrute.c(OK)
    -> primesqrt.c(OK)
    -> primeopt.c(OK)

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

haas/spring2016/cprog/projects/pnc0.1456163719.txt.gz · Last modified: 2016/02/22 17:55 by wedge