📜  使用日志的数字的平方根

📅  最后修改于: 2021-04-26 06:16:30             🧑  作者: Mango

对于给定的数字,使用对数函数找到平方根。数字可以是int,float或double。

例子:

Input  : n = 9
Output : 3

Input  : n = 2.93
Output : 1.711724

我们可以使用sqrt()方法找到数字的平方根。

C++
// C++ program to demonstrate finding
// square root of a number using sqrt()
#include
  
int main(void)
{
    double n = 12;
    printf("%lf ", sqrt(n));
    return 0;
}


Java
// 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
# 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#
// 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


C++
// C++ program to demonstrate finding
// square root of a number using log2()
#include
  
double squareRoot(double n)
{
    return pow(2, 0.5*log2(n));
}
  
int main(void)
{
    double n = 12;
    printf("%lf ", squareRoot(n));
    return 0;
}


Java
// 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 Code
public static void main (String[] args) 
{
    double n = 12; 
    System.out.println(squareRoot(n)); 
}
}
  
// This code is contributed by akt_mit


Python
# Python program to demonstrate finding
# square root of a number using sqrt()
import math
  
# function to return squareroot
def squareRoot(n):
  
    return pow(2, 0.5 * math.log2(n))
  
# Driver program
  
n = 12
print(squareRoot(n))
  
# This code is contributed by
# Sanjit_Prasad


C#
// 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


输出 :

3.464102 

我们还可以使用log2()库函数找到平方根:

C++

// C++ program to demonstrate finding
// square root of a number using log2()
#include
  
double squareRoot(double n)
{
    return pow(2, 0.5*log2(n));
}
  
int main(void)
{
    double n = 12;
    printf("%lf ", squareRoot(n));
    return 0;
}

Java

// 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 Code
public static void main (String[] args) 
{
    double n = 12; 
    System.out.println(squareRoot(n)); 
}
}
  
// This code is contributed by akt_mit 

Python

# Python program to demonstrate finding
# square root of a number using sqrt()
import math
  
# function to return squareroot
def squareRoot(n):
  
    return pow(2, 0.5 * math.log2(n))
  
# Driver program
  
n = 12
print(squareRoot(n))
  
# This code is contributed by
# Sanjit_Prasad

C#

// 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


输出:

3.464101615137755

以上程序如何运作?

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))