-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqrt_of_num.cpp
92 lines (68 loc) · 1.73 KB
/
sqrt_of_num.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/****************************************************************************
File name: sqrt_of_num.cpp
Author: babajr
*****************************************************************************/
/*
Program to find the square root of a number.
Input: 36
Output: 6
Input: 40
Output: 6.32 // assuming only 2 decimal points
*/
#include <bits/stdc++.h>
using namespace std;
/*
API to get the square root of a number using the binary search.
TC : O(log n)
Algo:
--> mid = (start + end) / 2;
--> if(mid * mid > num)
end = mid - 1
--> else if(mid * mid < num)
start = mid + 1;
--> else
return mid.
Input: num = 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
s m e
64 > 16
s m e
16 = 16 ==> ANS = 4
*/
double sqrt_of_num(int num, int precision)
{
int start = 0;
int end = num;
double root = 0.0;
while(start <= end)
{
int mid = (start + end) / 2;
if(mid * mid == num)
return mid;
else if(mid * mid > num)
end = mid - 1;
else
{
start = mid + 1;
root = mid; // if num is not perfect sqaure
}
}
// if num is not perfect square, get the decimal values upto mentioned
// precision with following logic.
double incr = 0.1;
for(int i = 0; i < precision; i++)
{
while(root * root <= num)
root += incr;
root -= incr;
incr /= 10;
}
return root;
}
int main(void)
{
int num = 40;
int precision = 2;
printf("Sqrt(%d) = %f", num, sqrt_of_num(num, precision));
return 0;
}