-
Notifications
You must be signed in to change notification settings - Fork 9
/
Nth root of a number M upto D decimal places.cpp
54 lines (35 loc) · 1.39 KB
/
Nth root of a number M upto D decimal places.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
// PROBLEM :- Find approximate Nth root of a number M upto D decimal places
// Solution T.C. O(N * log(M * 10^D)) S.C. O(1) [Binary Search]
// What can be the lowest possible Nth root -> 1
// can be the highest possible Nth root -> M
// Do a binary seatch over this range.
// Lets say X be the mid. Find out X^N and compare with M.
// If X^N > M, the Nth root should be smaller, thus search in left search space.
// Else , the Nth root should be greater, thus search in right search space.
// When to stop??
// When the (high - low) value becomes less than 10^(-D).
// Why so? Because it is that point when the approximate value is found upto D decimal place.
// What is the answer?
// Both LOW and HIGH can contribute to answer, as we are not looking for a specific answer, but as an approximate answer.
void findApproximateNthRootOfANumberUptoDdecimalPlaces(int NRoot, int number, int decimalPlaces) {
double low = 1;
double high = number;
double esp = 1.0;
for(int i = 0 ; i < decimalPlaces ; i++){
esp = esp / 10;
}
while((high - low) > esp){
double mid = (low + high) / 2.0;
double curValue = 1.0;
for(int i = 0 ; i < NRoot ; i++){
curValue *= mid;
}
if(curValue > number){
high = mid;
}else{
low = mid;
}
}
cout<<low<<" "<<high<<endl;
return;
}