VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-square-root-number-upto-given-precision-using-binary-search/

⇱ Square Root upto Given Precision using Binary Search - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Square Root upto Given Precision using Binary Search

Last Updated : 23 Apr, 2026

Given a positive number n and an integer p representing the desired precision, compute the square root of n accurate to p decimal places. The solution should avoid using built-in square root functions.
Note: Precision control is required to ensure the output is correctly rounded or truncated at p digits after the decimal.

Examples:

Input: n = 50, p = 3
Output: 7.071
Explanation: The square root of 50 up to 3 decimal places is 7.071

Input: n = 10, p = 4
Output: 3.1622
Explanation: The square root of 10 up to 4 decimal places is 3.1622

[Expected Approach] Binary Search with Incremental Refinement

This approach begins by finding the integer part of the square root between 0 and the given number. At each step, the midpoint is checked: if its square is equal to the number, that’s the exact root; if less, the search continues on the right, and if more, it shifts to the left. Once the closest integer is found, the algorithm switches to fractional refinement. It gradually adds smaller values (like 0.1, 0.01, etc.) to approximate the square root up to the required precision.

Step by Step Approach

Finding the Integer Part using Binary Search

  • The square root of n lies between 0 and n (inclusive)
  • Using binary search in this range to find the largest integer x such that x*x <= n.
    => Initialize start = 0, end = n, and ans = 0.
    => While start ≤ end:
    => Compute mid = (start + end) / 2
    => If mid * mid == n, you've found the exact square root.
    => If mid * mid < n, store ans = mid, and search the right half (start = mid + 1).
    => If mid * mid > n, search the left half (end = mid - 1).
  • This gives the integer part of the square root (i.e., floor(√n))

Decimal Precision

  • Now refine the answer by adding decimal values incrementally.
  • Start with increment = 0.1 and repeat for p digits of precision.
  • For each digit:
    => While (ans + increment)^2 ≤ n, keep increasing ans by increment.
    => Once you exceed n, backtrack (don't update ans) and reduce increment by a factor of 10:
    => 0.1 → 0.01 → 0.001 → ... (for p times)
  • This process helps add one decimal digit of precision per iteration.

Final ans contains the square root of n up to p decimal places.


Output
7.071

Time Complexity : O(log(n) + p), because binary search takes O(log n) time to find the integer part of the square root, and the refinement loop takes O(p) time to compute each of the p decimal places using linear steps.
Auxiliary Space: O(1) since it is using constant space for variables.

[Alternate Approach] Binary Search with Epsilon Precision

This approach uses binary search to approximate the square root of a number within a given decimal precision. It starts with a search range between 0 and n, repeatedly narrowing it down by comparing the square of the midpoint with n. The process stops when the range difference becomes smaller than a predefined epsilon value, which is determined by the required precision. This ensures both accuracy and efficiency in finding the result.

Step-by-Step Approach:

  • Initialize:
    => Set low = 0, high = n (or 1 if n < 1)
    => Set small value eps = 1e-6 for precision
  • Binary Search Loop: While (high - low) > eps:
  • Compute mid = (low + high) / 2
    => Compute mid = (low + high) / 2
    =>If mid * mid < n, root lies on the right, so update low = mid
    => Else, root lies on the left, so update high = mid
  • Return low or high (both will be very close after loop ends)

Output
7.072

Time Complexity: O(log₂((n / ε))), the binary search keeps halving the search range until the difference between low and high is less than the epsilon ε (which is 10^-p for p decimal places). This gives a complexity proportional to the logarithm of (n / ε).
Auxiliary Space: O(1), only a constant number of variables (low, high, mid, eps) are used, so the extra space remains constant regardless of input size.

Note: The second approach is an alternate method as it is more concise and mathematically clean. However, due to floating-point rounding, it may give slight inaccuracies, while the first approach ensures precise digit control and matches strict output requirements.

Comment
Article Tags: