This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:spring2017:cprog:projects:pnc1 [2017/03/01 10:37] – [sqrt()-less square root (primesqrtopt)] wedge | haas:spring2017:cprog:projects:pnc1 [2017/03/01 18:43] (current) – [Check Results] wedge | ||
---|---|---|---|
Line 80: | Line 80: | ||
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. | 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. | + | We know that a square root (especially a whole numbered square root), is when we have whole number factors that are squared. |
<cli> | <cli> | ||
- | lab46:~$ for ((i=8; i<38; i++)); do echo -n "[${i}] "; echo " | + | lab46: |
- | [8] 2 | + | [ |
- | [9] 3 | + | [ 12] 3 [ 13] 3 [ 14] 3 [ 15] 3 [ 16] 4 [ 17] 4 [ 18] 4 [ 19] 4 [ 20] 4 [ 21] 4 |
- | [10] 3 | + | [ 22] 4 [ 23] 4 [ 24] 4 [ 25] 5 [ 26] 5 [ 27] 5 [ 28] 5 [ 29] 5 [ 30] 5 [ 31] 5 |
- | [11] 3 | + | [ 32] 5 [ 33] 5 [ 34] 5 [ 35] 5 [ 36] 6 [ 37] 6 [ 38] 6 [ 39] 6 [ 40] 6 [ 41] 6 |
- | [12] 3 | + | [ 42] 6 [ 43] 6 [ 44] 6 [ 45] 6 [ 46] 6 [ 47] 6 [ 48] 6 [ 49] 7 [ 50] 7 [ 51] 7 |
- | [13] 3 | + | [ 52] 7 [ 53] 7 [ 54] 7 [ 55] 7 [ 56] 7 [ 57] 7 [ 58] 7 [ 59] 7 [ 60] 7 [ 61] 7 |
- | [14] 3 | + | [ 62] 7 [ 63] 7 [ 64] 8 [ 65] 8 [ 66] 8 [ 67] 8 [ 68] 8 [ 69] 8 [ 70] 8 [ 71] 8 |
- | [15] 3 | + | [ 72] 8 [ 73] 8 [ 74] 8 [ 75] 8 [ 76] 8 [ 77] 8 [ 78] 8 [ 79] 8 [ 80] 8 [ 81] 9 |
- | [16] 4 | + | [ 82] 9 [ 83] 9 [ 84] 9 [ 85] 9 [ 86] 9 [ 87] 9 [ 88] 9 [ 89] 9 [ 90] 9 [ 91] 9 |
- | [17] 4 | + | [ 92] 9 [ 93] 9 [ 94] 9 [ 95] 9 [ 96] 9 [ 97] 9 [ 98] 9 [ 99] 9 [100] 10 [101] 10 |
- | [18] 4 | + | [102] 10 [103] 10 [104] 10 [105] 10 [106] 10 [107] 10 [108] 10 [109] 10 [110] 10 [111] 10 |
- | [19] 4 | + | [112] 10 [113] 10 [114] 10 [115] 10 [116] 10 [117] 10 [118] 10 [119] 10 [120] 10 [121] 11 |
- | [20] 4 | + | [122] 11 [123] 11 [124] 11 [125] 11 [126] 11 [127] 11 [128] 11 [129] 11 [130] 11 [131] 11 |
- | [21] 4 | + | [132] 11 [133] 11 [134] 11 [135] 11 [136] 11 [137] 11 [138] 11 [139] 11 [140] 11 [141] 11 |
- | [22] 4 | + | [142] 11 [143] 11 [144] 12 [145] 12 [146] 12 [147] 12 [148] 12 [149] 12 [150] 12 [151] 12 |
- | [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 | + | |
</ | </ | ||
- | 9*9 == 81, the square root of 81 is 9. | + | Square root of 81 is 9, but so is the square root of 82, 83, 84... etc. up until we hit 100. |
- | If we were checking | + | If we were checking |
- | We don't need a **sqrt()** function to tell us this, we can determine the approximate square root point ourselves- | + | We don't need a **sqrt()** function to tell us this, we can determine the approximate square root point ourselves- |
There are some important lessons at play here: | There are some important lessons at play here: | ||
Line 285: | Line 270: | ||
lab46: | lab46: | ||
==================================================================================================== | ==================================================================================================== | ||
- | range brute | + | range brute |
==================================================================================================== | ==================================================================================================== | ||
- | 128 0.000097 | + | 128 0.000179 |
- | 256 0.000238 | + | 256 0.000401 |
- | 512 0.001000 | + | 512 0.001535 |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 16384 0.705101 | + | 16384 1.059970 |
- | 32768 2.786815 | + | 32768 4.209891 |
- | 65536 | + | 65536 |
- | | + | |
- | | + | |
- | | + | |
- | 1048576 | + | 1048576 |
- | 2097152 | + | 2097152 |
- | 4194304 | + | 4194304 |
- | 8388608 | + | 8388608 |
==================================================================================================== | ==================================================================================================== | ||
- | | + | |
==================================================================================================== | ==================================================================================================== | ||
lab46: | lab46: |