title: Compute the square root of 3 in C++
body: <p>How can I compute the square root of 3 in C++ using the following relation?</p>
  
  <p><a href="https://i.stack.imgur.com/Ga9Ab.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ga9Ab.png" alt="enter image description here"></a></p>
  
  <p>Here is what I tried:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  
  
  int main(void)
  {
      double prevRes(1);
      double res(1 + 1./2);
      short i(2);
  
  
      while (abs(prevRes - res) &gt; 1.e-14)
      {
          prevRes = res;
  
          res = i + 1 / res;
  
          i = 3 - i;
      }
  
      std::cout &lt;&lt; res &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>The program runs forever.</p>