User Tools

Site Tools


haas:fall2020:discrete:projects:pnc1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
haas:fall2020:discrete:projects:pnc1 [2018/09/10 13:28] – external edit 127.0.0.1haas:fall2020:discrete:projects:pnc1 [2020/10/11 13:27] (current) – [Evaluation Criteria] wedge
Line 4: Line 4:
 </WRAP> </WRAP>
  
-======Project: ALGORITHM OPTIMIZATION - PRIME NUMBER COMPUTATION (pnc1)======+======Project: ALGORITHMS - PRIME NUMBER CALCULATION (pnc1)======
  
 =====Errata===== =====Errata=====
Line 105: Line 105:
   * your timing should start before the loop (just AFTER argument processing), and terminate immediately following the terminating output newline outside the loops.   * your timing should start before the loop (just AFTER argument processing), and terminate immediately following the terminating output newline outside the loops.
   * you may **NOT** split **qty** and **range** functionality into two separate code blocks (ie have two sets of two loops). Only the one set as indicated.    * you may **NOT** split **qty** and **range** functionality into two separate code blocks (ie have two sets of two loops). Only the one set as indicated. 
- 
 =====prime algorithm optimizations===== =====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. 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.
Line 130: Line 129:
 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. 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===== +Here are the variants you'll be implementing for this project: 
-Something I noticed (and have historically noticedin relation to comments that I'like to point out:+ 
 +====break on composite (primeregb)==== 
 +This optimization to primereg will make but one algorithmic change, and that takes place at the moment of identifying a number as composite. So, if we had our 119 example above, and discovered that 7 was a factor: 
 + 
 +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 (how does this impact overall performance???). 
 + 
 +Make no other optimizations- this first project is to set up some important base line values that we can use for algorithmic comparison later on
 + 
 +====mapping factors of 6 (primeregm)==== 
 +This optimization will check only the numbers that fall on either side of a factor of 6 for primality. 
 + 
 +NOTE: If applicable, just display the initial "2" and "3" as hardcoded values. 
 + 
 +====odds-only checking (primerego)==== 
 +This optimization will check only the odd numbers for primality, skipping the evens entirely. 
 + 
 +NOTE: If applicable, just display the initial "2" as a hardcoded value. 
 + 
 +====sqrt() trick (primeregs)==== 
 +This optimization employs the square root trick utilizing the C library's **sqrt()** function. 
 + 
 +====sqrt()-less square root approximation (primerega)==== 
 +This optimization employs the approximated square root trick (**NOT** utilizing an existing square root function, but using simpler logic you implement to approximate the square root point). 
 + 
 +===Further explanation=== 
 +An optimization to the previous process, which used **sqrt()**, this variation will do the exact same thing, but without using the **sqrt()** function. It will approximate the square root. 
 + 
 +We know that a square root (especially a whole numbered square root), is when we have whole number factors that are squared. But in addition, only considering the whole number aspect of the square root, we start seeing series of values with the same whole square root value: 
 + 
 +<cli> 
 +lab46:~$ count=0; for ((i=2; i<152; i++)); do printf "[%3d] %2d " "${i}" `echo "sqrt($i)" | bc -q`; let count=count+1; if [ "${count}" -eq 10 ]; then echo; count=0; fi; done; echo 
 +[  2]  1 [  3]  1 [  4]  2 [  5]  2 [  6]  2 [  7]  2 [  8]  2 [  9]  3 [ 10]  3 [ 11]  3 
 +[ 12]  3 [ 13]  3 [ 14]  3 [ 15]  3 [ 16]  4 [ 17]  4 [ 18]  4 [ 19]  4 [ 20]  4 [ 21]  4 
 +[ 22]  4 [ 23]  4 [ 24]  4 [ 25]  5 [ 26]  5 [ 27]  5 [ 28]  5 [ 29]  5 [ 30]  5 [ 31]  5 
 +[ 32]  5 [ 33]  5 [ 34]  5 [ 35]  5 [ 36]  6 [ 37]  6 [ 38]  6 [ 39]  6 [ 40]  6 [ 41]  6 
 +[ 42]  6 [ 43]  6 [ 44]  6 [ 45]  6 [ 46]  6 [ 47]  6 [ 48]  6 [ 49]  7 [ 50]  7 [ 51]  7 
 +[ 52]  7 [ 53]  7 [ 54]  7 [ 55]  7 [ 56]  7 [ 57]  7 [ 58]  7 [ 59]  7 [ 60]  7 [ 61]  7 
 +[ 62]  7 [ 63]  7 [ 64]  8 [ 65]  8 [ 66]  8 [ 67]  8 [ 68]  8 [ 69]  8 [ 70]  8 [ 71]  8 
 +[ 72]  8 [ 73]  8 [ 74]  8 [ 75]  8 [ 76]  8 [ 77]  8 [ 78]  8 [ 79]  8 [ 80]  8 [ 81]  9 
 +[ 82]  9 [ 83]  9 [ 84]  9 [ 85]  9 [ 86]  9 [ 87]  9 [ 88]  9 [ 89]  9 [ 90]  9 [ 91]  9 
 +[ 92]  9 [ 93]  9 [ 94]  9 [ 95]  9 [ 96]  9 [ 97]  9 [ 98]  9 [ 99]  9 [100] 10 [101] 10 
 +[102] 10 [103] 10 [104] 10 [105] 10 [106] 10 [107] 10 [108] 10 [109] 10 [110] 10 [111] 10 
 +[112] 10 [113] 10 [114] 10 [115] 10 [116] 10 [117] 10 [118] 10 [119] 10 [120] 10 [121] 11 
 +[122] 11 [123] 11 [124] 11 [125] 11 [126] 11 [127] 11 [128] 11 [129] 11 [130] 11 [131] 11 
 +[132] 11 [133] 11 [134] 11 [135] 11 [136] 11 [137] 11 [138] 11 [139] 11 [140] 11 [141] 11 
 +[142] 11 [143] 11 [144] 12 [145] 12 [146] 12 [147] 12 [148] 12 [149] 12 [150] 12 [151] 12 
 +</cli> 
 + 
 +Or, if perhaps we instead order by square root value: 
 + 
 +<cli> 
 +lab46:~$ oldsqrt=$(echo "sqrt(2)" | bc -q); for ((i=2; i<49; i++)); do newsqrt=$(echo "sqrt($i)" | bc -q); if [ "${newsqrt}" -ne "${oldsqrt}" ]; then echo; fi; printf "[%3d] %2d " "${i}" "${newsqrt}"; oldsqrt="${newsqrt}"; done; echo 
 +[  2]  1 [  3]  1 
 +[  4]  2 [  5]  2 [  6]  2 [  7]  2 [  8]  2 
 +[  9]  3 [ 10]  3 [ 11]  3 [ 12]  3 [ 13]  3 [ 14]  3 [ 15]  3 
 +[ 16]  4 [ 17]  4 [ 18]  4 [ 19]  4 [ 20]  4 [ 21]  4 [ 22]  4 [ 23]  4 [ 24]  4 
 +[ 25]  5 [ 26]  5 [ 27]  5 [ 28]  5 [ 29]  5 [ 30]  5 [ 31]  5 [ 32]  5 [ 33]  5 [ 34]  5 [ 35]  5 
 +[ 36]  6 [ 37]  6 [ 38]  6 [ 39]  6 [ 40]  6 [ 41]  6 [ 42]  6 [ 43]  6 [ 44]  6 [ 45]  6 [ 46]  6 [ 47]  6 [ 48]  6 
 +</cli> 
 + 
 +We see that the square root of 36 is 6, but so is the square root of 37, 38, 39... etc. up until we hit 49 (where the whole number square root increments to 7). 
 + 
 +Therefore, if we were checking 42 to be prime, we'only have to check up to 6. 
 + 
 +We don't need a **sqrt()** function to tell us this, we can determine the approximate square root point ourselves- by squaring the current factor being tested, and so long as it hasn't exceeded the value we're checking, we know to continue. 
 + 
 +There are some important lessons at play here:
  
-Comments should be describing what is going on in your code.+  * approximation can be powerful 
 +  * approximation can result in a simpler algorithm, improving runtime 
 +    * **sqrt()** is more complex than you may be aware, not to mention it is in a function. By avoiding that function call, we eliminate some overhead, and that can make a difference in runtime performance.
  
-With projects like this, often relying on common basecomments become even more importantas they allow me to see specifically what is changed or unique about one variant over the other.+Depending on how you implement this and the original sqrt() algorithmsthis version may have noticeable performance difference. Ifon the other handyou were really optimal in both implementations, the performance difference may be narrower (if negligible). 
 +  
 +====primeregbm==== 
 +To get a taste for combining optimizations, you'll also implement a variant that incorporates both the **break** AND the **map** optimizations.
  
-As suchwhen 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.+NOTE: If applicablejust display the initial "2" and "3" as hardcoded values.
  
-And notice I said the "how" and/or "why". NOT the "what". I see all the time vague comments like "<nowiki>// doing the sqrt() optimization</nowiki>"..but:+====primeregbo==== 
 +To get a taste for combining optimizations, you'll also implement a variant that incorporates both the **break** AND the **odds-only checking** optimizations.
  
-  * WHY is that important to the process? +NOTE: If applicable, just display the initial "2" as a hardcoded value.
-  * HOW does it impact the efficiency of the algorithm?+
  
-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 anyon the requirements for that variant.+====primeregbs==== 
 +To get a taste for combining optimizations, you'll also implement a variant that incorporates both the **break** AND the **sqrt()** optimizations.
  
-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.+====primeregba==== 
 +To get a taste for combining optimizations, you'll also implement a variant that incorporates both the **break** AND the **approximated square root** optimizations.
  
 =====Programs===== =====Programs=====
Line 198: Line 270:
     * each program is to have no fewer and no more than 2 loops in this prime processing section.     * each program is to have no fewer and no more than 2 loops in this prime processing section.
     * in each program, you are not allowed to use a given loop type (for(), while(), do-while()) more than once!     * in each program, you are not allowed to use a given loop type (for(), while(), do-while()) more than once!
-  * display identified primes (space-separated) to a file pointer called **primelist** +  * display identified primes (space-separated) to a file pointer called **stdout** 
-  * stop your stopwatch immediately following your prime processing loops (and terminating newline display to **primelist**). Calculate the time that has transpired (ending time minus starting time). +  * stop your stopwatch immediately following your prime processing loops (and terminating newline display to **stdout**). Calculate the time that has transpired (ending time minus starting time). 
-  * output the processing run-time to the file pointer called **timing**+  * output the processing run-time to the file pointer called **stderr**
   * your output **MUST** conform to the example output in the **execution** section below. This is also a test to see how well you can implement to specifications. Basically:   * your output **MUST** conform to the example output in the **execution** section below. This is also a test to see how well you can implement to specifications. Basically:
     * as primes are being displayed, they are space-separated (first prime hugs the left margin), and when all said and done, a newline is issued.     * as primes are being displayed, they are space-separated (first prime hugs the left margin), and when all said and done, a newline is issued.
-    * the timing information will be displayed in accordance to code I will provide below (see the **timing** section).+    * the timing information will be displayed in accordance to code I will provide below (see the **Timing** section)
 + 
 +=====Implementation Restrictions===== 
 + 
 +As our goal is not only to explore the more subtle concepts of computing but to promote different methods of thinking (and arriving at solutions seemingly in different ways), one of the themes I have been harping on is the stricter adherence to the structured programming philosophy. It isn't just good enough to be able to crank out a solution if you remain blind to the many nuances of the tools we are using, so we will at times be going out of our way to emphasize focus on certain areas that may see less exposure (or avoidance due to it being less familiar). 
 + 
 +As such, the following implementation restrictions are also in place: 
 + 
 +  * use any **break** or **continue**, or other flow redirection statements sparingly. I am not forbidding their use, but I also don't want this to turn into a lazy solution free-for-all. I am letting you use them, but with **justification**. 
 +    * **justification** implies some thoughtful why/how style comments explaining how a particular use of one of these statements is effective and efficient (not: "I couldn't think of any other way to do it"). 
 +  * absolutely **NO** infinite loops (**while(1)** or the like). 
 +  * no forced redirection of the flow of the process (no seeking to the end of the file to grab a max size only to zip back somewhere else: deal with the data in as you are naturally encountering it; no telling; no "ungetting" data back into the file). 
 +  * All "arrays" must be declared and referenced using ONLY pointer notation, NO square brackets. 
 +  * **NO** logic shunts (ie having an if statement nested inside a loop to bypass an undesirable iteration)- this should be handled by the loop condition! 
 +  * at most, only **one** return() statement per function. Error terminations should use **exit()** 
 + 
 +Write clean, well-indented, well-commented, effective code... show me that you have learned something from your programming experience.
  
 =====Grabit Integration===== =====Grabit Integration=====
Line 222: Line 310:
  
 make: Leaving directory '/var/public/SEMESTER/discrete/pnc1' make: Leaving directory '/var/public/SEMESTER/discrete/pnc1'
-lab46:~/src/discrete/ 
-lab46:~/src/discrete$ cd pnc1 
-lab46:~/src/discrete/pnc1$  
 </cli> </cli>
  
Line 243: Line 328:
 ** make                     - build everything                        ** ** make                     - build everything                        **
 ** make showerrors          - display compiler warnings/errors        ** ** make showerrors          - display compiler warnings/errors        **
 +**                                                                    **
 ** make debug               - build everything with debug symbols     ** ** make debug               - build everything with debug symbols     **
 ** make checkqty            - runtime evaluation for qty              ** ** make checkqty            - runtime evaluation for qty              **
 ** make checkrange          - runtime evaluation for range            ** ** 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 save                - create a backup archive                 **
Line 264: Line 343:
 ** make help                - this information                        ** ** make help                - this information                        **
 ************************************************************************ ************************************************************************
-lab46:~/src/discrete/pnc1$  
 </cli> </cli>
  
Line 270: Line 348:
  
   * **make**: compile everything   * **make**: compile everything
-    * any **warnings** or **errors** generated by the compiler will go into a file in the base directory of pnc0 in a file called **errors**; you can **cat** it to view the information.+    * any **warnings** or **errors** generated by the compiler will go into a file in the base directory of the project in a file called **errors**; you can **cat** it to view the information.
   * **make debug**: compile everything with debug support   * **make debug**: compile everything with debug support
     * any **warnings** or **errors** generated by the compiler will be displayed to the screen as the programs compile.     * any **warnings** or **errors** generated by the compiler will be displayed to the screen as the programs compile.
Line 278: Line 356:
  
 The various "check" options do a runtime performance grid, allowing you to compare timings between your implementations. 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. Just another "nice thing" we deserve.
Line 319: Line 395:
  
 ===example=== ===example===
-For example, if we were to execute the **primeregbms** program:+For example, if we were to execute the **primereg** program:
  
 <cli> <cli>
-lab46:~/src/discrete/pnc1$ ./primeregbms 128 1 2 2048+lab46:~/src/discrete/pnc1$ ./primereg 128 1 2 2048
 </cli> </cli>
  
 We'd have: We'd have:
  
-  * <nowiki>argv[0]</nowiki>: "./primeregbms+  * <nowiki>argv[0]</nowiki>: "./primereg
   * <nowiki>argv[1]</nowiki>: "128" (note, NOT the scalar integer 128, but a string)    * <nowiki>argv[1]</nowiki>: "128" (note, NOT the scalar integer 128, but a string) 
   * <nowiki>argv[2]</nowiki>: "1"   * <nowiki>argv[2]</nowiki>: "1"
Line 409: Line 485:
  
 ====Displaying the runtime==== ====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:+Once we have the starting and ending times, we can display this to the **stderr** file pointer. You'll want this line:
  
 <code c> <code c>
-fprintf(timing, "%8.4lf\n",+fprintf(stderr, "%8.4lf\n",
 time_end.tv_sec-time_start.tv_sec+((time_end.tv_usec-time_start.tv_usec)/1000000.0)); time_end.tv_sec-time_start.tv_sec+((time_end.tv_usec-time_start.tv_usec)/1000000.0));
 </code> </code>
Line 419: Line 495:
  
 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. 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.
 +
 +=====Loops=====
 +A loop is basically instructing the computer to repeat a section, or block, or code a given amount of times (it can be based on a fixed value-- repeat this 4 times, or be based on a conditional value-- keep repeating as long as (or while) this value is not 4).
 +
 +Loops enable us to simplify our code-- allowing us to write a one-size-fits all algorithm (provided the algorithm itself can appropriately scale!), where the computer merely repeats the instructions we gave. We only have to write them once, but the computer can do that task any number of times.
 +
 +Loops can be initially difficult to comprehend because unlike other programmatic actions, they are not single-state in nature-- loops are multi-state. What this means is that in order to correctly "see" or visualize a loop, you must analyze what is going on with EACH iteration or cycle, watching the values/algorithm/process slowly march from its initial state to its resultant state. Think of it as climbing a set of stairs... yes, we can describe that action succinctly as "climbing a set of stairs", but there are multiple "steps" (heh, heh) involved: we place our foot, adjust our balance-- left foot, right foot, from one step, to the next, to the next, allowing us to progress from the bottom step to the top step... that process of scaling a stairway is the same as iterating through a loop-- but what is important as we implement is what needs to happen each step along the way.
 +
 +With that said, it is important to be able to focus on the process of the individual steps being taken. What is involved in taking a step? What constitutes a basic unit of stairway traversal? If that unit can be easily repeated for the next and the next (and in fact, the rest of the) steps, we've described the core process of the loop, or what will be iterated a given number of times.
 +
 +In C and C-syntax influenced languages (C++, Java, PHP, among others), we typically have 3 types of loops:
 +
 +  * **for** loop (automatic counter loop, stepping loop; top-driven) - when we know exactly how many times we wish something to run; we know where we want to start, where we want to end, and exactly how to progress from start to end (step value)
 +  * **while** loop (top-driven conditional loop) - when we want to repeat a process, but the exact number of iterations is either not known, not important, not known, or variable in nature. While loops can run 0 or more times.
 +  * **do-while** loop (bottom-driven conditional loop) - similar to the while loop, only we do the check for loop termination at the bottom of the loop, meaning it runs 1 or more times (a do-while loop is guaranteed to run at least once).
 +
 +====for() loops====
 +A **for()** loop is the most syntactically unique of the loops, so care must be taken to use the proper syntax.
 +
 +With any loop, we need (at least one) looping variable, which the loop will use to analyze whether or not we've met our looping destination, or to perform another iteration.
 +
 +A for loop typically also has a defined starting point, a "keep-looping-while" condition, and a stepping equation.
 +
 +Here's a sample for() loop, in C, which will display the squares of each number, starting at 0, and stepping one at a time, for 8 total iterations:
 +
 +<code c>
 +int i = 0;
 +
 +for (i = 0; i < 8; i++)
 +{
 +    fprintf(stdout, "loop #%d ... %d\n", (i+1), (i*i));
 +}
 +</code>
 +
 +The output of this code, with the help of our loop should be:
 +
 +<cli>
 +loop #1 ... 0
 +loop #2 ... 1
 +loop #3 ... 4
 +loop #4 ... 9
 +loop #5 ... 16
 +loop #6 ... 25
 +loop #7 ... 36
 +loop #8 ... 49
 +</cli>
 +
 +Note how we can use our looping variable (**i**) within mathematical expressions to drive a process along... loops can be of enormous help in this way.
 +
 +And again, we shouldn't look at this as one step-- we need to see there are 8 discrete, distinct steps happening here (when i is 0, when i is 1, when i is 2, ... up until (and including) when i is 7).
 +
 +The loop exits once **i** reaches a value of 8, because our loop determinant condition states as long as **i** is **less than** **8**, continue to loop. Once **i** becomes **8**, our looping condition has been satisfied, and the loop will no longer iterate.
 +
 +The stepping (that third) field is a mathematical expression indicating how we wish for **i** to progress from its starting state (of being equal to 0) to satisfying the loop's iterating condition (no longer being less than 8).
 +
 +**i++** is a shortcut we can use in C; the longhand (and likely more familiar) equivalent is: **i = i + 1**
 +
 +====while() loops====
 +A **while()** loop isn't as specific about starting and stepping values, really only caring about what condition needs to be met in order to exit the loop (keep looping while this condition is true).
 +
 +In actuality, anything we use a for loop for can be expressed as a while loop-- we merely have to ensure we provide the necessary loop variables and progressions within the loop.
 +
 +That same loop above, expressed as a while loop, could look like:
 +
 +<code c>
 +int i = 0;
 +
 +while (i < 8)
 +{
 +    fprintf(stdout, "loop #%d ... %d\n", (i+1), (i*i));
 +    i = i + 1;   // I could have used "i++;" here
 +}
 +</code>
 +
 +The output of this code should be identical, even though we used a different loop to accomplish the task (try them both out and confirm!)
 +
 +**while()** loops, like **for()** loops, will run 0 or more times; if the conditions enabling the loop to occur are not initially met, they will not run... if met, they will continue to iterate until their looping conditions are met.
 +
 +It is possible to introduce a certain kind of **logical error** into your programs using loops-- what is known as an "infinite loop"; this is basically where you erroneously provide incorrect conditions to the particular loop used, allowing it to start running, but never arriving at its conclusion, thereby iterating forever.
 +
 +Another common **logical error** that loops will allow us to encounter will be the "off by one" error-- where the conditions we pose to the loop are incorrect, and the loop runs one magnitude more or less than we had intended. Again, proper debugging of our code will resolve this situation.
 +
 +====do-while loops====
 +The third commonly recognized looping structure in C, the do-while loop is identical to the while() (and therefore also the for()) loop, only it differs in where it checks the looping condition: where **for()** and **while()** are "top-driven" loops (ie the test for loop continuance occurs at the top of the loop, **before** running the code in the loop body), the **do-while** is a "bottom-driven" loop (ie the test for loop continuance occurs at the bottom of the loop).
 +
 +The placement of this test determines the minimal number of times a loop can run.
 +
 +In the case of the for()/while() loops, because the test is at the top- if the looping conditions are not met, the loop may not run at all. It is for this reason why these loops can run "0 or more times"
 +
 +For the do-while loop, because the test occurs at the bottom, the body of the loop (one full iteration) is run before the test is encountered. So even if the conditions for looping are not met, a do-while will run "1 or more times".
 +
 +That may seem like a minor, and possibly annoying, difference, but in nuanced algorithm design, such distinctions can drastically change the layout of your code, potentially being the difference between beautifully elegant-looking solutions and those which appear slightly more hackish. They can BOTH be used to solve the same problems, it is merely the nature of how we choose express the solution that should make one more preferable over the other in any given moment.
 +
 +I encourage you to intentionally try your hand at taking your completed programs and implementing other versions that utilize the other types of loops you haven't utilized. This way, you can get more familiar with how to structure your solutions and express them. You will find you tend to think in a certain way (from experience, we seem to get in the habit of thinking "top-driven", and as we're unsure, we tend to exert far more of a need to control the situation, so we tend to want to use **for** loops for everything-- but practicing the others will free your mind to craft more elegant and efficient solutions; but only if you take the time to play and explore these possibilities).
 +
 +So, expressing that same program in the form of a do-while loop (note the changes from the while):
 +
 +<code c>
 +int i = 0;
 +
 +do
 +{
 +    fprintf(stdout, "loop #%d ... %d\n", (i+1), (i*i));
 +    i = i + 1;  // again, we could just as easily use "i++;" here
 +} while(i < 8);
 +</code>
 +
 +In this case, the 0 or more vs. 1 or more minimal iterations wasn't important; the difference is purely syntactical.
 +
 +With the do-while loop, we start the loop with a **do** statement.
 +
 +Also, the do-while is the only one of our loops which NEEDS a terminating semi-colon (**;**).. please take note of this.
  
 =====Execution===== =====Execution=====
Line 426: Line 614:
  
 <cli> <cli>
-lab46:~/src/discrete/pnc1$ ./primeregoa 24 1+lab46:~/src/discrete/pnc1$ ./primereg 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  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   0.0001
Line 438: Line 626:
  
 <cli> <cli>
-lab46:~/src/discrete/pnc1$ ./primeregbmo 32 1 0 +lab46:~/src/discrete/pnc1$ ./primerego 32 1 0 
-./primeregbmo: invalid lower bound+./primerego: invalid lower bound
 lab46:~/src/discrete/pnc1$  lab46:~/src/discrete/pnc1$ 
 </cli> </cli>
Line 449: Line 637:
  
 <cli> <cli>
-lab46:~/src/discrete/pnc1$ ./primeregos 128 1 7 23+lab46:~/src/discrete/pnc1$ ./primeregs 128 1 7 23
 7 11 13 17 19 23 7 11 13 17 19 23
   0.0001   0.0001
Line 460: Line 648:
 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. 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).+In order to work, you **MUST** be in the directory where your pnc1 binaries reside, and must be named as such (which occurs if you ran **make** to compile them).
  
 ====check qty==== ====check qty====
Line 467: Line 655:
 <cli> <cli>
 lab46:~/src/discrete/pnc1$ make checkqty lab46:~/src/discrete/pnc1$ make checkqty
-========================================================================================================================= +========================================================================================= 
-      qty   regmo  regbmo   regms   regma   regos   regoa  regmos  regmoa  regbms  regbma  regbos regbmos  regboa regbmoa +      qty     reg    regm    rego    regb   regbm   regbo    regs    rega   regbs   regba 
-========================================================================================================================= +========================================================================================= 
-       32  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 +       32  0.0002  0.0001  0.0001  0.0002  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 
-       64  0.0002  0.0002  0.0001  0.0002  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 +       64  0.0006  0.0003  0.0002  0.0002  0.0002  0.0001  0.0001  0.0002  0.0002  0.0001 
-      128  0.0005  0.0003  0.0002  0.0001  0.0002  0.0002  0.0002  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 +      128  0.0028  0.0010  0.0008  0.0006  0.0006  0.0003  0.0004  0.0003  0.0002  0.0002 
-      256  0.0022  0.0011  0.0003  0.0003  0.0003  0.0004  0.0003  0.0003  0.0003  0.0003  0.0003  0.0002  0.0002  0.0002 +      256  0.0123  0.0041  0.0031  0.0020  0.0019  0.0010  0.0009  0.0008  0.0004  0.0003 
-      512  0.0096  0.0039  0.0009  0.0008  0.0009  0.0008  0.0006  0.0006  0.0006  0.0006  0.0005  0.0004  0.0004  0.0004 +      512  0.0574  0.0188  0.0144  0.0077  0.0077  0.0040  0.0025  0.0026  0.0008  0.0007 
-     1024  0.0444  0.0159  0.0025  0.0025  0.0023  0.0021  0.0016  0.0015  0.0015  0.0014  0.0011  0.0010  0.0010  0.0009 +     1024  0.2690  0.0880  0.0665  0.0320  0.0312  0.0161  0.0077  0.0080  0.0019  0.0016
-     2048  0.2061  0.0676  0.0076  0.0075  0.0065  0.0062  0.0044  0.0042  0.0040  0.0038  0.0028  0.0025  0.0023  0.0022 +
-     4096  0.9626  0.2905  0.0236  0.0231  0.0194  0.0188  0.0129  0.0125  0.0108  0.0104  0.0069  0.0063  0.0061  0.0059 +
-     8192  5.4731  1.5194  0.0812  0.0805  0.0646  0.0633  0.0431  0.0424  0.0334  0.0321  0.0197  0.0185  0.0182  0.0175+
 ... ...
-   262144  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------ +   262144  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------ 
-========================================================================================================================= +=========================================================================================
- verify:     OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-========================================================================================================================= +
-lab46:~/src/discrete/pnc1$ +
 </cli> </cli>
  
Line 492: Line 674:
 <cli> <cli>
 lab46:~/src/discrete/pnc1$ make checkrange lab46:~/src/discrete/pnc1$ make checkrange
-========================================================================================================================= +========================================================================================= 
-    range   regmo  regbmo   regms   regma   regos   regoa  regmos  regmoa  regbms  regbma  regbos regbmos  regboa regbmoa +    range     reg    regm    rego    regb   regbm   regbo    regs    rega   regbs   regba 
-========================================================================================================================= +========================================================================================= 
-       32  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 +       32  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 
-       64  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 +       64  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 
-      128  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 +      128  0.0002  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 
-      256  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001  0.0001 +      256  0.0004  0.0002  0.0002  0.0001  0.0002  0.0002  0.0002  0.0002  0.0001  0.0001 
-      512  0.0003  0.0002  0.0002  0.0001  0.0002  0.0001  0.0001  0.0001  0.0001  0.0001  0.0002  0.0001  0.0001  0.0001 +      512  0.0015  0.0006  0.0005  0.0003  0.0003  0.0002  0.0002  0.0002  0.0002  0.0002 
-     1024  0.0010  0.0005  0.0002  0.0002  0.0002  0.0002  0.0002  0.0002  0.0002  0.0002  0.0002  0.0002  0.0001  0.0002 +     1024  0.0053  0.0018  0.0014  0.0009  0.0010  0.0005  0.0005  0.0005  0.0002  0.0002 
-     2048  0.0033  0.0015  0.0004  0.0004  0.0004  0.0004  0.0003  0.0003  0.0003  0.0003  0.0003  0.0003  0.0003  0.0003 +     2048  0.0191  0.0063  0.0049  0.0028  0.0027  0.0015  0.0011  0.0011  0.0004  0.0003 
-     4096  0.0118  0.0047  0.0010  0.0010  0.0009  0.0009  0.0007  0.0007  0.0006  0.0006  0.0006  0.0005  0.0005  0.0005 +     4096  0.0709  0.0232  0.0177  0.0094  0.0091  0.0048  0.0029  0.0030  0.0008  0.0008 
-     8192  0.0448  0.0161  0.0026  0.0025  0.0023  0.0022  0.0016  0.0015  0.0015  0.0015  0.0011  0.0011  0.0010  0.0009 +     8192  0.2712  0.0887  0.0672  0.0322  0.0315  0.0163  0.0078  0.0077  0.0019  0.0016
-    16384  0.1742  0.0576  0.0067  0.0067  0.0058  0.0055  0.0038  0.0046  0.0036  0.0034  0.0024  0.0022  0.0021  0.0020 +
-    32768  0.6861  0.2096  0.0186  0.0181  0.0152  0.0147  0.0101  0.0099  0.0086  0.0083  0.0055  0.0051  0.0049  0.0048 +
-    65536  2.7211  0.7758  0.0503  0.0500  0.0410  0.0397  0.0270  0.0268  0.0215  0.0208  0.0132  0.0123  0.0119  0.0117+
 ... ...
-  4194304  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------ +  2097152  ------  ------  ------  ------  ------  ------  ------  ------  ------  ------ 
-========================================================================================================================= +=========================================================================================
- verify:     OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-========================================================================================================================= +
-lab46:~/src/discrete/pnc1$ +
 </cli> </cli>
  
Line 520: Line 696:
  
 ====Verification==== ====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"+You will want to verify your program output'validity to ensure maximum correctness.
- +
-====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: +
- +
-<cli> +
-lab46:~/src/discrete/pnc1$ make verifyall +
-========================================================================================================================= +
-            regmo  regbmo   regms   regma   regos   regoa  regmos  regmoa  regbms  regbma  regbos regbmos  regboa regbmoa +
-========================================================================================================================= +
- qtynorm:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
- qtypart:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
- rngnorm:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
- rngpart:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-    coop:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-   coop2:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-   coop3:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-  noargs:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
- invargs:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-  invqty:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
- invnary:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-  invlow:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
- invhigh:    OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK      OK +
-========================================================================================================================= +
-lab46:~/src/discrete/pnc1$  +
-</cli> +
- +
-===verifyall tests=== +
-The "**verifyall**" is an industrial grade verification; there are 13 specific tests performed, they are: +
-  * **qtynorm**: a normal quantity run (2-max) +
-    * **./primealg 2048 1 2 0** +
-  * **qtypart**: an offset quantity run (24-max) +
-    * **./primealg 2048 1 24 0** +
-  * **rngnorm**: a normal range run (2-max) +
-    * **./primealg 0 1 2 2048** +
-  * **rngpart**: an offset range run (24-max) +
-    * **./primealg 0 1 24 2048** +
-  * **coop**: both qty and upper bounds set (q: 2048, ub: 8192) +
-    * **./primealg 2048 1 2 8192** +
-  * **coop2**: both qty and upper bounds set (q: 512, ub: 8192) +
-    * **./primealg 512 1 2 8192** +
-  * **coop3**: both qty and upper bounds set, offset start (24-max, q: 2048, ub: 8192) +
-    * **./primealg 2048 1 24 8192** +
-  * **noargs**:  no arguments provided on command line (invokes error message) +
-    * **./primealg** +
-  * **invargs**: insufficient number of arguments provided (invokes error) +
-    * **./primealg 128** +
-  * **invqty**: invalid value for quantity argument given (invokes error) +
-    * **./primealg -2 1** +
-  * **invnary**: invalid value given for n-ary (invokes error) +
-    * **./primealg 128 2** +
-  * **invlow**: invalid value given for lower bound (invokes error) +
-    * **./primealg 128 1 1** +
-  * **invhigh**: invalid value given for upper bound (invokes error) +
-    * **./primealg 128 1 32 24** +
- +
-If you'd actually to see the output your program'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: +
- +
-  * **/usr/local/etc/primeinvnary**+
  
-You could easily run your program with the stated arguments for the test, then use **cat** to display the test results and do visual comparison.+In the **data/** directory you will find **primelist.gz** file which contains the first 295947 primes for your verification needs.
  
 ====In general==== ====In general====
Line 678: Line 794:
 *:pnc1:primeALGO.c relevant comments throughout code [1/1] *:pnc1:primeALGO.c relevant comments throughout code [1/1]
 *:pnc1:primeALGO.c code conforms to project specifications [2/2] *:pnc1:primeALGO.c code conforms to project specifications [2/2]
-*:pnc1:primeALGO.c runtime output conforms to specifications [1/1]+*:pnc1:primeALGO.c runtime output conforms to specifications [4/4]
 *:pnc1:primeALGO.c make checkqty test times within reason [1/1] *:pnc1:primeALGO.c make checkqty test times within reason [1/1]
 *:pnc1:primeALGO.c make checkrange test times within reason [1/1] *:pnc1:primeALGO.c make checkrange test times within reason [1/1]
-*:pnc1:primeALGO.c make verifyall tests succeed [3/3] 
 </code> </code>
  
haas/fall2020/discrete/projects/pnc1.1536586081.txt.gz · Last modified: 2018/09/10 13:28 by 127.0.0.1