Striver-SDE-Sheet

Repository containing solution for #SdeSheetChallenge by striver

View the Project on GitHub

N’th root of M

Code

long double power(long double mid, int n)
{
    long double ans = 1.0;
    while (n--)
        ans *= mid;
    return ans;
}

long double findNthRootOfM(int n, long long m)
{
    long double low = 1.0, high = (long double)m, eps = 1e-9;
    while ((high - low) > eps) {
        long double mid = (high - low) / 2.0 + low;
        if (power(mid, n) > (long double)m)
            high = mid;
        else
            low = mid;
    }
    return low;
}