📜  为pow(x,y)编写一个迭代O(Log y)函数

📅  最后修改于: 2021-04-29 17:23:40             🧑  作者: Mango

给定一个整数x和一个正数y,编写一个在以下条件下计算x y的函数。
a)函数的时间复杂度应为O(Log y)
b)额外空间为O(1)
例子:

Input: x = 3, y = 5
Output: 243

Input: x = 2, y = 5
Output: 32

我们强烈建议您单击此处并进行实践,然后再继续解决方案。

我们讨论了功率的递归O(Log y)解决方案。递归解决方案通常不是首选,因为它们在调用堆栈上需要空间,并且它们涉及函数调用开销。
以下是计算x y的实现

C
// Iterative C program to implement pow(x, n)
#include 
 
/* Iterative Function to calculate (x^y) in O(logy) */
int power(int x, unsigned int y)
{
    int res = 1; // Initialize result
 
    while (y > 0) {
        // If y is odd, multiply x with result
        if (y & 1)
            res = res * x;
 
        // n must be even now
        y = y >> 1; // y = y/2
        x = x * x; // Change x to x^2
    }
    return res;
}
 
// Driver program to test above functions
int main()
{
    int x = 3;
    unsigned int y = 5;
 
    printf("Power is %d", power(x, y));
 
    return 0;
}


Java
// Iterative Java program
// to implement pow(x, n)
import java.io.*;
 
class GFG
{
     
/* Iterative Function to
calculate (x^y) in O(logy) */
static int power(int x, int y)
{
    // Initialize result
    int res = 1;
 
    while (y > 0)
    {
        // If y is odd,
        // multiply
        // x with result
        if ((y & 1) == 1)
            res = res * x;
 
        // n must be even now
        y = y >> 1; // y = y/2
        x = x * x; // Change x to x^2
    }
    return res;
}
 
// Driver Code
public static void main (String[] args)
{
    int x = 3;
    int y = 5;
 
    System.out.println("Power is " +
                        power(x, y));
}
}
 
// This code is contributed
// by aj_36


Python3
# Iterative Python3 program
# to implement pow(x, n)
 
# Iterative Function to
# calculate (x^y) in O(logy)
def power(x, y):
 
    # Initialize result
    res = 1
     
    while (y > 0):
         
        # If y is odd, multiply
        # x with result
        if ((y & 1) == 1) :
            res = res * x
 
        # n must be even
        # now y = y/2
        y = y >> 1
         
        # Change x to x^2
        x = x * x
     
    return res
 
 
# Driver Code
x = 3
y = 5
 
print("Power is ",
       power(x, y))
 
# This code is contributed
# by ihritik


C#
// Iterative C# program
// to implement pow(x, n)
using System;
 
class GFG
{
     
/* Iterative Function to
calculate (x^y) in O(logy) */
static int power(int x, int y)
{
    int res = 1; // Initialize result
 
    while (y > 0)
    {
        // If y is odd, multiply
        // x with result
        if ((y & 1) == 1)
            res = res * x;
 
        // n must be even now
        y = y >> 1; // y = y/2
        x = x * x; // Change x to x^2
    }
    return res;
}
 
// Driver Code
static public void Main ()
{
int x = 3;
int y = 5;
 
Console.WriteLine("Power is "+
                   power(x, y));
}
}
 
// This code is contributed
// by aj_36


PHP

 
// Iterative Function to
// calculate (x^y) in O(logy)
 
function power($x, $y)
{
     
    // Initialize result
    $res = 1;    
 
    while ($y > 0)
    {
         
        // If y is odd, multiply
        // x with result
        if ($y & 1)
            $res = $res * $x;
 
        // n must be even now
         
        // y = y/2
        $y = $y >> 1;
         
        // Change x to x^2
        $x = $x * $x;
    }
    return $res;
}
 
    // Driver Code
    $x = 3;
    $y = 5;
 
    echo "Power is ", power($x, $y);
 
// This code is contributed by ajit
?>


Javascript


输出:

Power is 243