title: How to find the nearest square root to nearest using binary search using python
body: <p>How to find the nearest square root of number?</p>
  <ul>
  <li>If square root present then I need the exact square toot, otherwise the previous of square root.</li>
  </ul>
  <p>Example is below:</p>
  <ul>
  <li><code>squre_root(101) = 10</code></li>
  <li><code>squre_root(100) = 10</code></li>
  </ul>
  <p>Approach</p>
  <ul>
  <li>Find the mid number of square root to find</li>
  <li>for <code>101</code>: find the mid, that is <code>51</code></li>
  <li>since <code>51 * 51 &lt;= 101</code> then <code>51</code> will be divided by half</li>
  <li>Like at last by binary search I will get <code>10</code></li>
  </ul>
  <p>Sample Code :</p>
  <pre><code>def find_sqrt(k):
      begin, last = 1, k
      mid = int((begin + last)/ 2)
      
      while begin &lt; mid:
          if mid * mid =&gt; k:
              mid = int(mid/ 2)
          if mid * mid &lt;= k:
              mid += 1
      return mid
  </code></pre>
  <p>I had found <a href="https://www.tutorialspoint.com/guess-nearest-square-root-in-python" rel="nofollow noreferrer">guess nearest square root in python</a> while researching.</p>