📜  编写程序以计算pow(x,n)

📅  最后修改于: 2021-04-27 16:44:55             🧑  作者: Mango

给定两个整数x和n,编写一个函数来计算x n 。我们可以假设x和n很小,并且不会发生溢出。

例子 :

Input : x = 2, n = 3
Output : 8

Input : x = 7, n = 2
Output : 49

下面的解决方案将问题分为大小为y / 2的子问题,然后递归调用子问题。

C++
// C++ program to calculate pow(x,n)
#include
using namespace std;
class gfg
{
     
/* Function to calculate x raised to the power y */
public:
int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}
};
 
/* Driver code */
int main()
{
    gfg g;
    int x = 2;
    unsigned int y = 3;
 
    cout << g.power(x, y);
    return 0;
}
 
// This code is contributed by SoM15242


C
#include
 
/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
 
/* Program to test function power */
int main()
{
    int x = 2;
    unsigned int y = 3;
 
    printf("%d", power(x, y));
    return 0;
}


Java
class GFG {
    /* Function to calculate x raised to the power y */
    static int power(int x, int y)
    {
        if (y == 0)
            return 1;
        else if (y % 2 == 0)
            return power(x, y / 2) * power(x, y / 2);
        else
            return x * power(x, y / 2) * power(x, y / 2);
    }
 
    /* Program to test function power */
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
 
        System.out.printf("%d", power(x, y));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal


Python3
# Python3 program to calculate pow(x,n)
 
# Function to calculate x
# raised to the power y
def power(x, y):
 
    if (y == 0): return 1
    elif (int(y % 2) == 0):
        return (power(x, int(y / 2)) *
               power(x, int(y / 2)))
    else:
        return (x * power(x, int(y / 2)) *
                   power(x, int(y / 2)))
 
# Driver Code
x = 2; y = 3
print(power(x, y))
 
# This code is contributed by Smitha Dinesh Semwal.


C#
using System;
 
public class GFG {
     
    // Function to calculate x raised to the power y
    static int power(int x, int y)
    {
        if (y == 0)
            return 1;
        else if (y % 2 == 0)
            return power(x, y / 2) * power(x, y / 2);
        else
            return x * power(x, y / 2) * power(x, y / 2);
    }
 
    // Program to test function power
    public static void Main()
    {
        int x = 2;
        int y = 3;
 
        Console.Write(power(x, y));
    }
}
 
// This code is contributed by shiv_bhakt.


PHP


Javascript


C++
/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
}
 
// This code is contributed by Shubhamsingh10


C
/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}


Java
/* Function to calculate x raised to the power y in O(logn)*/
static int power(int x, int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
 
// This code is contributed by divyeshrabadiya07.


Python3
# Function to calculate x raised to the power y in O(logn)
def power(x,y):
    temp = 0
    if( y == 0):
        return 1
    temp = power(x, int(y / 2))
    if (y % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
 
# This code is contributed by avanitrachhadiya2155


C#
/* Function to calculate x raised to the power y in O(logn)*/
static int power(int x, int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
 
// This code is contributed by divyesh072019.


Javascript


C++
/* Extended version of power function
that can work for float x and negative y*/
#include 
using namespace std;
 
float power(float x, int y)
{
    float temp;
    if(y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
    {
        if(y > 0)
            return x * temp * temp;
        else
            return (temp * temp) / x;
    }
}
 
// Driver Code
int main()
{
    float x = 2;
    int y = -3;
    cout << power(x, y);
    return 0;
}
 
// This is code is contributed
// by rathbhupendra


C
/* Extended version of power function that can work
 for float x and negative y*/
#include
 
float power(float x, int y)
{
    float temp;
    if( y == 0)
       return 1;
    temp = power(x, y/2);      
    if (y%2 == 0)
        return temp*temp;
    else
    {
        if(y > 0)
            return x*temp*temp;
        else
            return (temp*temp)/x;
    }
} 
 
/* Program to test function power */
int main()
{
    float x = 2;
    int y = -3;
    printf("%f", power(x, y));
    return 0;
}


Java
/* Java code for extended version of power function
that can work for float x and negative y */
class GFG {
     
    static float power(float x, int y)
    {
        float temp;
        if( y == 0)
            return 1;
        temp = power(x, y/2);
         
        if (y%2 == 0)
            return temp*temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
     
    /* Program to test function power */
    public static void main(String[] args)
    {
        float x = 2;
        int y = -3;
        System.out.printf("%f", power(x, y));
    }
}
 
// This code is contributed by  Smitha Dinesh Semwal.


Python3
# Python3 code for extended version
# of power function that can work
# for float x and negative y
 
def power(x, y):
 
    if(y == 0): return 1
    temp = power(x, int(y / 2))
     
    if (y % 2 == 0):
        return temp * temp
    else:
        if(y > 0): return x * temp * temp
        else: return (temp * temp) / x
     
# Driver Code
x, y = 2, -3
print('%.6f' %(power(x, y)))
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# code for extended version of power function
// that can work for float x and negative y
 
using System;
 
public class GFG{
     
    static float power(float x, int y)
    {
        float temp;
         
        if( y == 0)
            return 1;
        temp = power(x, y/2);
         
        if (y % 2 == 0)
            return temp * temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
     
    // Program to test function power
    public static void Main()
    {
        float x = 2;
        int y = -3;
         
        Console.Write(power(x, y));
    }
}
 
// This code is contributed by shiv_bhakt.


PHP
 0)
            return $x *
                   $temp * $temp;
        else
            return ($temp *
                    $temp) / $x;
    }
}
 
// Driver Code
$x = 2;
$y = -3;
echo power($x, $y);
 
// This code is contributed by ajit
?>


Javascript


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
    public static int power(int x, int y)
    {
         
        // If x^0 return 1
        if (y == 0)
            return 1;
         
        // If we need to find of 0^y
        if (x == 0)
            return 0;
         
        // For all other cases
        return x * power(x, y - 1);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
 
        System.out.println(power(x, y));
    }
}


C#
// C# program for the above approach
using System;
 
class GFG{
     
public static int power(int x, int y)
{
     
    // If x^0 return 1
    if (y == 0)
        return 1;
     
    // If we need to find of 0^y
    if (x == 0)
        return 0;
     
    // For all other cases
    return x * power(x, y - 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int x = 2;
    int y = 3;
 
    Console.WriteLine(power(x, y));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
    public static int power(int x, int y)
    {
         
        // Math.pow() is a function that
        // return floating number
        return (int)Math.pow(x, y);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
 
        System.out.println(power(x, y));
    }
}


Javascript


输出 :

8

时间复杂度: O(n)
空间复杂度: O(1)

算法范式:分而治之。
通过仅计算一次幂(x,y / 2)并将其存储,可以将上述函数优化为O(logn)。

C++

/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
}
 
// This code is contributed by Shubhamsingh10

C

/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

Java

/* Function to calculate x raised to the power y in O(logn)*/
static int power(int x, int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
 
// This code is contributed by divyeshrabadiya07.

Python3

# Function to calculate x raised to the power y in O(logn)
def power(x,y):
    temp = 0
    if( y == 0):
        return 1
    temp = power(x, int(y / 2))
    if (y % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
 
# This code is contributed by avanitrachhadiya2155

C#

/* Function to calculate x raised to the power y in O(logn)*/
static int power(int x, int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
 
// This code is contributed by divyesh072019.

Java脚本


优化解决方案的时间复杂度: O(logn)

让我们扩展pow函数,使其适用于负y和浮点x。

C++

/* Extended version of power function
that can work for float x and negative y*/
#include 
using namespace std;
 
float power(float x, int y)
{
    float temp;
    if(y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
    {
        if(y > 0)
            return x * temp * temp;
        else
            return (temp * temp) / x;
    }
}
 
// Driver Code
int main()
{
    float x = 2;
    int y = -3;
    cout << power(x, y);
    return 0;
}
 
// This is code is contributed
// by rathbhupendra

C

/* Extended version of power function that can work
 for float x and negative y*/
#include
 
float power(float x, int y)
{
    float temp;
    if( y == 0)
       return 1;
    temp = power(x, y/2);      
    if (y%2 == 0)
        return temp*temp;
    else
    {
        if(y > 0)
            return x*temp*temp;
        else
            return (temp*temp)/x;
    }
} 
 
/* Program to test function power */
int main()
{
    float x = 2;
    int y = -3;
    printf("%f", power(x, y));
    return 0;
}

Java

/* Java code for extended version of power function
that can work for float x and negative y */
class GFG {
     
    static float power(float x, int y)
    {
        float temp;
        if( y == 0)
            return 1;
        temp = power(x, y/2);
         
        if (y%2 == 0)
            return temp*temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
     
    /* Program to test function power */
    public static void main(String[] args)
    {
        float x = 2;
        int y = -3;
        System.out.printf("%f", power(x, y));
    }
}
 
// This code is contributed by  Smitha Dinesh Semwal.

Python3

# Python3 code for extended version
# of power function that can work
# for float x and negative y
 
def power(x, y):
 
    if(y == 0): return 1
    temp = power(x, int(y / 2))
     
    if (y % 2 == 0):
        return temp * temp
    else:
        if(y > 0): return x * temp * temp
        else: return (temp * temp) / x
     
# Driver Code
x, y = 2, -3
print('%.6f' %(power(x, y)))
 
# This code is contributed by Smitha Dinesh Semwal.

C#

// C# code for extended version of power function
// that can work for float x and negative y
 
using System;
 
public class GFG{
     
    static float power(float x, int y)
    {
        float temp;
         
        if( y == 0)
            return 1;
        temp = power(x, y/2);
         
        if (y % 2 == 0)
            return temp * temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
     
    // Program to test function power
    public static void Main()
    {
        float x = 2;
        int y = -3;
         
        Console.Write(power(x, y));
    }
}
 
// This code is contributed by shiv_bhakt.

的PHP

 0)
            return $x *
                   $temp * $temp;
        else
            return ($temp *
                    $temp) / $x;
    }
}
 
// Driver Code
$x = 2;
$y = -3;
echo power($x, $y);
 
// This code is contributed by ajit
?>

Java脚本


输出 :

0.125000

时间复杂度: O(log | n |)

辅助空间: O(1)

使用递归:

这种方法几乎类似于迭代解决方案

Java

// Java program for the above approach
import java.io.*;
 
class GFG {
    public static int power(int x, int y)
    {
         
        // If x^0 return 1
        if (y == 0)
            return 1;
         
        // If we need to find of 0^y
        if (x == 0)
            return 0;
         
        // For all other cases
        return x * power(x, y - 1);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
 
        System.out.println(power(x, y));
    }
}

C#

// C# program for the above approach
using System;
 
class GFG{
     
public static int power(int x, int y)
{
     
    // If x^0 return 1
    if (y == 0)
        return 1;
     
    // If we need to find of 0^y
    if (x == 0)
        return 0;
     
    // For all other cases
    return x * power(x, y - 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int x = 2;
    int y = 3;
 
    Console.WriteLine(power(x, y));
}
}
 
// This code is contributed by Rajput-Ji

Java脚本


输出:

8

使用Java的Math.pow()函数:

Java

// Java program for the above approach
import java.io.*;
 
class GFG {
    public static int power(int x, int y)
    {
         
        // Math.pow() is a function that
        // return floating number
        return (int)Math.pow(x, y);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
 
        System.out.println(power(x, y));
    }
}

Java脚本


输出:

8