sandeep03edu

Nth root of M

Jun 24th, 2022
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. double multiply(double m, int n){
  2.     double ans = 1;
  3.     for(int i=0; i<n; ++i){
  4.         ans *= m;
  5.     }
  6.     return ans;
  7. }
  8.  
  9. double findNthRootOfM(int n, long long m) {
  10.     double low = 1, high = m, diff = 1e-7;
  11.    
  12.     while((high-low)>diff){
  13.         double mid = (low + high)/2.0;
  14.        
  15.         if(multiply(mid, n)<m){
  16.             low = mid;
  17.         }
  18.         else{
  19.             high = mid;
  20.         }
  21.     }
  22.     return high;
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment