-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareRootBinarySearch.cpp
71 lines (59 loc) · 2.1 KB
/
SquareRootBinarySearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* SQUARE ROOT OF A NUMBER USING BINARY SEARCH */
#include <iostream>
using namespace std;
// function to find square root of a number using binary search upto a give precision
float SquareRootBinarySearch(int num, int precision) {
int start = 0, end = num, mid;
// we will store our answer in this
float answer;
// binarySearch
while (start <= end) {
/* mid index of array here we are not writing (s+e)/2 too handle a condition
where start and end both are 2^31-1 which will execeed the int range while adding */
mid = start + (end - start) / 2;
/* if square of mid is less than number it means it is a solution */
if (mid * mid == num) {
answer = mid;
break;
} else if (mid * mid < num) {
/* if square is less than number that means right side of mid */
start = mid + 1;
/* it can be possible solution so we will store this */
answer = mid;
} else {
/* moving to left side of mid */
end = mid - 1;
}
}
/* for fraction upto a given precision */
float increment = 0.1;
// for loop will run upto a precision
for (int i = 0; i < precision; i++) {
// calulating value for each place of fraction
while (answer * answer <= num) {
answer += increment;
}
/* if loop doesnot satisfy condition we will subtract
previously added increment value from answer */
answer = answer - increment;
// moving to next place of precision
increment = increment / 10;
}
// returning answer that is root of a number
return answer;
}
// main function
int main() {
int num, precision;
//enter the number
cout << "Enter the number: ";
cin >> num;
//enter precision
cout << "Enter the precision: ";
cin >> precision;
//storing square root in result variable by calling function
float result = SquareRootBinarySearch(num, precision);
//displaying result
cout << "Square root of " << num << " is: " << result << endl;
return 0;
}