Square root of a number using log - GeeksforGeeks

26 Apr, 2021
For a given number find the square root using log function. Number may be int, float or double.
Examples: 
Input  : n = 9
Output : 3

Input  : n = 2.93
Output : 1.711724
We can find square root of a number using sqrt() method.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to demonstrate finding// square root of a number using sqrt()#include<bits/stdc++.h> int main(void){    double n = 12;    printf("%lf ", sqrt(n));    return 0;}
// Java program to demonstrate finding// square root of a number using sqrt() import java.io.*; class GFG {    public static void main (String[] args) {    double n = 12;    System.out.println(Math.sqrt(n));  // This code is contributed by akt_mit    }}
# Python3 program to demonstrate finding# square root of a number using sqrt()import math if __name__=='__main__':    n = 12    print(math.sqrt(n)) # This code is contributed by# Sanjit_Prasad
// C# program to demonstrate finding// square root of a number using sqrt()using System; class GFG{public static void Main(){    double n = 12;    Console.Write(Math.Sqrt(n));}} // This code is contributed// by Akanksha Rai
<?php// PHP program to demonstrate finding// square root of a number using sqrt()$n = 12;echo sqrt($n); // This code is contributed by jit_t?>
<script> // Javascript program to demonstrate finding// square root of a number using sqrt()var n = 12; document.write(Math.sqrt(n).toFixed(6)); // This code is contributed by aashish1995 </script>
Output : 
3.464102 
We can also find square root using log2() library function: 
C++
Java
Python
C#
PHP
Javascript
// C++ program to demonstrate finding// square root of a number using log2()#include<bits/stdc++.h> double squareRoot(double n){    return pow(2, 0.5*log2(n));} int main(void){    double n = 12;    printf("%lf ", squareRoot(n));    return 0;}
// Java program to demonstrate finding// square root of a number using log2()import java.io.*; class GFG{static double squareRoot(double n){    return Math.pow(2, 0.5 * (Math.log(n) /                              Math.log(2)));} // Driver Codepublic static void main (String[] args){    double n = 12;    System.out.println(squareRoot(n));}} // This code is contributed by akt_mit
# Python program to demonstrate finding# square root of a number using sqrt()import math # function to return squarerootdef squareRoot(n):     return pow(2, 0.5 * math.log2(n)) # Driver program n = 12print(squareRoot(n)) # This code is contributed by# Sanjit_Prasad
// C# program to demonstrate finding// square root of a number using log2()using System; public class GFG{     static double squareRoot(double n){     return Math.Pow(2, 0.5 * (Math.Log(n) /Math.Log(2)));}          static public void Main (){            double n = 12;            Console.WriteLine(squareRoot(n));    }//This code is contributed by akt_mit   }
<?php// PHP program to demonstrate finding// square root of a number using log2()function squareRoot($n){    return pow(2, 0.5 * log($n, 2));} // Driver Code$n = 12;echo squareRoot($n);     // This code is contributed by ajit?>
<script>    // Javascript program to demonstrate finding    // square root of a number using log2()         function squareRoot(n)    {         return Math.pow(2, 0.5 * (Math.log(n) /Math.log(2)));    }         let n = 12;      document.write(squareRoot(n).toFixed(15)); </script>
Output: 
3.464101615137755
How does 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))  
This article is contributed by Tumma Umamaheswararao from Jntuh College of Engineering . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sanjit_Prasad
jit_t
Akanksha_Rai
aashish1995
divyesh072019
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Count ways to reach the n'th stair
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python