![]() |
VOOZH | about |
For a given number find the square root using log function. Number may be int, float or double.
Examples:
Input : n = 9
Output : 3Input : n = 2.93
Output : 1.711724
We can find square root of a number using sqrt() method:
3.464102
Time complexity: O(log2n), for using sqrt() function.
Auxiliary space: O(1)
We can also find square root using log2() library function:
3.464102
Time complexity: O(log2log2N), complexity of using log(N) is log(logN), and pow(x,N) is log(N), so pow(2,0.5*log(n)) will be log(logN).
Auxiliary space: O(1)
How does the above program work?
let d be our answer for input number n then n(1/2) = d apply log2 on both sides log2(n(1/2)) = log2(d) log2(d) = 1/2 * log2(n) d = 2(1/2 * log2(n)) d = pow(2, 0.5*log2(n))