This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:fall2017:cprog:projects:pnc1 [2017/10/15 21:10] – [prime algorithm implementation] wedge | haas:fall2017:cprog:projects:pnc1 [2017/10/15 21:27] (current) – [Full Verification Compliance] wedge | ||
---|---|---|---|
Line 128: | Line 128: | ||
Here are the variants you'll be implementing for this project: | Here are the variants you'll be implementing for this project: | ||
+ | ====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 " | ||
+ | |||
+ | ====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 " | ||
+ | |||
+ | ====sqrt() trick (primeregs)==== | ||
+ | This optimization employs the square root trick utilizing the C library' | ||
+ | |||
+ | ====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 " " | ||
+ | [ 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 | ||
+ | </ | ||
+ | |||
+ | Or, if perhaps we instead order by square root value: | ||
+ | |||
+ | <cli> | ||
+ | lab46:~$ oldsqrt=$(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 | ||
+ | </ | ||
+ | |||
+ | 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'd 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: | ||
+ | |||
+ | * 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. | ||
+ | |||
+ | Depending on how you implement this and the original sqrt() algorithms, this version may have a noticeable performance difference. If, on the other hand, you were really optimal in both implementations, | ||
=====Programs===== | =====Programs===== | ||
It is your task to write the following prime number variants: | It is your task to write the following prime number variants: | ||
- | | + | * **primeregb.c**: tests specifically |
+ | * **primeregm.c**: | ||
+ | * **primerego.c**: | ||
+ | * **primeregs.c**: | ||
+ | * **primerega.c**: | ||
====Program Specifications==== | ====Program Specifications==== | ||
Line 422: | Line 499: | ||
====check qty==== | ====check qty==== | ||
- | For instance (running on my implementation of the pnc1 programs, some output omitted to keep the surprise alive): | + | For instance (running on my implementation of the pnc1 programs): |
<cli> | <cli> | ||
lab46: | lab46: | ||
- | ================= | + | ========================================================= |
- | qty reg | + | qty |
- | ================= | + | ========================================================= |
- | | + | |
- | | + | |
- | 128 0.0028 | + | 128 0.0012 0.0004 |
- | 256 0.0123 | + | 256 0.0057 0.0020 |
- | 512 0.0574 | + | 512 0.0278 0.0098 |
- | | + | |
- | ... | + | 2048 0.6416 0.2317 0.1510 0.0727 |
- | 262144 | + | |
- | ================= | + | |
- | | + | 16384 ------ |
- | ================= | + | 32768 ------ |
+ | 65536 ------ | ||
+ | 131072 | ||
+ | ========================================================= | ||
+ | | ||
+ | ========================================================= | ||
lab46: | lab46: | ||
</ | </ | ||
Line 448: | Line 530: | ||
<cli> | <cli> | ||
lab46: | lab46: | ||
- | ================= | + | ========================================================= |
- | range reg | + | range |
- | ================= | + | ========================================================= |
- | | + | |
- | | + | |
- | 128 0.0002 | + | 128 0.0001 0.0001 |
- | 256 0.0004 | + | 256 0.0002 0.0001 |
- | 512 0.0015 | + | 512 0.0006 0.0003 |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | ... | + | |
- | 2097152 | + | 32768 2.1530 |
- | ================= | + | 65536 ------ |
- | | + | |
- | ================= | + | |
+ | | ||
+ | 1048576 | ||
+ | 2097152 | ||
+ | ========================================================= | ||
+ | | ||
+ | ========================================================= | ||
lab46: | lab46: | ||
</ | </ | ||
Line 480: | Line 568: | ||
<cli> | <cli> | ||
lab46: | lab46: | ||
- | ================= | + | ========================================================= |
- | reg | + | reg |
- | ================= | + | ========================================================= |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | coop: OK | + | coop: OK |
- | | + | |
- | | + | |
- | noargs: | + | noargs: |
- | | + | |
- | invqty: | + | invqty: |
- | | + | |
- | invlow: | + | invlow: |
- | | + | |
- | ================= | + | ========================================================= |
lab46: | lab46: | ||
</ | </ |