Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While @alestanis has provided what looks to me like a much <em>more</em> accurate analysis of this problem than those in the comments, I still don't think it's quite right.</p> <p>Let's create a small test program that prints out the values of <code>i</code> produced by the inner loop:</p> <pre><code>#include &lt;iostream&gt; void inner(double k) { double i; i = 0.0; while(i &lt; k) { i ++; i = i * i; std::cout &lt;&lt; i &lt;&lt; "\n"; } } int main() { inner(1e200); return 0; } </code></pre> <p>When I run this, the result I get is:</p> <pre><code>1 4 25 676 458329 2.10066e+011 4.41279e+022 1.94727e+045 3.79186e+090 1.43782e+181 1.#INF </code></pre> <p>If the number of iterations were logarithmic, then the number of iterations to reach a particular number should be proportional to the number of digits in the limit. For example, if it were logarithmic, it should take around 180 iterations to reach 1e181, give or take some (fairly small) constant factor. That's clearly not the case here at all -- as is easily visible by looking at the exponents of the results in scientific notation, this is approximately doubling the number of digits every iteration, where logarithmic would mean it was roughly adding one digit every iteration.</p> <p>I'm not absolutely certain, but I believe that puts the inner loop at something like O(log log N) instead of just O(log N). I think it's pretty easy to agree that the outer loop is probably intended to be O(N) (though it's currently written to execute only once), putting the overall complexity at <code>O(N log log N)</code>.</p> <p>I feel obliged to add that from a pragmatic viewpoint, <code>O(log log N)</code> can often be treated as essentially constant -- as shown above, the highest limit you can specify with a typical double precision floating point number is reached in only 11 iterations. As such, for most practical purposes, the overall complexity can be treated as <code>O(N)</code>.</p> <p>[Oops -- didn't notice he'd answered as I was writing this, but it looks like @jwpat7 has reached about the same conclusion I did. Kudos to him/her.]</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. 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