Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know if it is any faster, but it is considerably shorter (and easier to analyze).</p> <pre><code>int k[] = { 17, 15, 13, 11, 9, 7, 5, 3, 1 }; int r = 0; f *= 20; s -= 81; s -= f * 9; while (s &lt; 0) { s += f; s += k[r]; if (++r == 9) break; } if (s &gt;= 0) return 9-r; </code></pre> <p><strong>Edit:</strong> In fact, the original poster came up with a clever way to optimize this loop by pre-computing the sum of constants in the <code>k</code> array, and compared <code>s</code> against the sums, rather than incrementally adding them to <code>s</code>.</p> <p><strong>Edit:</strong> I followed moonshadow's analysis technique, but arrived at a different equation. Original TeX formatting replaced with ASCII art (I tried to get MathJax to render the TeX for me, but it wasn't working):</p> <pre><code>S[0] = s &gt;= 0 =&gt; 9 - 0 S[1] = S[0] + f + 19 - 2*1 &gt;= 0 =&gt; 9 - 1 S[2] = S[1] + f + 19 - 2*2 &gt;= 0 =&gt; 9 - 2 ... S[i] = S[i-1] + f + 19 - 2*i &gt;= 0 =&gt; 9 - i </code></pre> <p>So to calculate <code>S[n]</code>:</p> <pre><code> S[n] = S[n-1] + f + 19 - 2n .-- n =&gt; S[n] = s + &gt; (f + 19 - 2*i) `-- i=1 .-- n =&gt; S[n] = s + n(f + 19) - 2 &gt; i `-- i=1 =&gt; S[n] = s + n(f + 19) - n(n+1) 2 =&gt; S[n] = s + n(f + 18) - n </code></pre> <p>So, the inequality <code>S[n] &gt;= 0</code> is a quadratic equation in <code>n</code>. Assuming <code>s &lt; 0</code>, we want <code>n</code> to be the ceiling of the solution to the quadratic.</p> <pre><code> +-- --+ | _____________ | | / 2 | | f + 18 - . / (f + 18) + 4s | | ` | n = | --------------------------- | | 2 | </code></pre> <p>So the routine would look something like:</p> <pre><code>f *= 180; s -= 81; s -= f; if (s &gt;= 0) return 9; f /= 9; f += 18; s *= 4; int1024_t ff = f; ff *= f; ff += s; ff = ff.Sqrt(); f -= ff; f += f.Mod2(); return 9 - f/2; </code></pre> <p>However, I am not sure the expense of performing these operations on your big integer objects is worth implementing to replace the simple loop shown above. (Unless you expect to extend the function and would require a much longer loop.)</p> <p>To be faster than the loop, the big integer square root implementation would have to always converge within 4 iterations to beat the average expected 4.5 iterations of the existing while loop. However the <code>ttmath</code> implementation does not seem to be calculating an integer square root. It seems to calculate a floating point square root and then rounding the result, which I would guess would be much slower than the loop.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload