An integer is prime if it is greater than 1 and its only divisors are 1 and itself. Non-prime numbers are composite, meaning they are products of prime factors.
Given any number N, you can check if its prime by dividing it by smaller natural numbers. Further, you don't even have to consider all of them; its easy to show that you need to go up to and no higher than sqrt(N) since that will be the largest possible divisor.
Primes are important in several routines you may use later in your time at UTK, for example, in public-key cryptography. Primes are also cool for a bunch of mathematical reasons, for example, the unproven twin prime conjecture states that the number of primes that differ by two (i.e., 5 and 7; 11 and 13; 41 and 43) is infinite. The largest known prime has over 24.8 million decimal digits, discovered by a FedEx employee who lives in Germantown, TN. Finding large prime numbers has even led to financial awards given how important they are.
Write a C++ program called primes1.cpp that takes a single command-line argument and prints all primes between 0 and that number, displaying 20 per line, separated by commas.
Example usage:
./primes 100
should output the following:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 73, 79, 83, 89, 97
It would be good practice to develop a separate procedure "isPrime(x)" in whatever approach/procedure you'd like.
Use % to check for divisibility. Remember to skip numbers less than or equal to 1.
Develop another simple C++ program called "primes2.cpp" that caches known primes using an STL container of your choice (e.g., vector). This program, like others you wrote this semester, should take a series of numbers from standard in and report if they are prime or not prime as follows:
100 5 4 3 2 1should report
not prime prime not prime prime prime not prime
Requirement: You must use a container that supports searching, e.g., vector or set. If using vector, use std::find() for lookup for a number that is smaller than the previous max (and therefore it will be in said vector if its prime).
Caching logic: If a number is larger than any number you've previously checked, extend your known prime cache up to that number.
In a historical Dr. Plank style 140/302 students would be asked to complete TopCoder challenges to further hone their coding skills, after which a TA would provide their solution on a high level (no actual code usually). There are quite a bit on primes, but I want you to "geek out" further. We will award 2 extra credit points to students who win one of the challenges below. Note they are different, i.e., a well done attempt at #2 probably will not win #1. Let us know if you have questions or concerns.
+ 2 primes1.cpp and primes2.cpp are well formatted, commented (inc. name, assignment, and overview), with reasonable variable names +4 Matches the target output for 100 primes for part 1 (see above) +11 Passes make test for part 2 (primes2.cpp; 1 point per test) +14 Report (report.txt) has the requested values. Namely: - make test times (n=3) using find (3 points, 1 per value) - test times (n=3) using a vector of size max value, with v[i] == 1 if i is prime - Provides the correct # of primes / load factor (2 points) - test times (n = 3) using binary_search from the STL instead of find (1 point per value) - test times (n = 3) using either a map or a set instead of a vector (1 point per value)
To faciliate testing, you were previously asked to clone the course Github repository as follows:
git clone https://github.com/semrich/CS202-fall24.git cs202
For this assignment, update this clone by using the following:
git pull
Name your solution to part 1 "primes1.cpp," the solution to part 2, "primes2.cpp," and your final solution to part 3 that uses a map or a set as "primes3.cpp." Name your report "report.txt." Submitting part 3 is optional as mentioned in class.
Then, please run the following command prior to submission:
tar -cvf lab6.tar primes1.cpp primes2.cpp report.txt
Note: Although submission will be faciliated by Canvas, we will compile and test on EECS lab machines!
If you develop your solution elsewhere please make sure it works on
the lab computers prior to the deadline.