Write an iterative O(Log y) function for pow(x, y)
Given an integer x and a positive number y, write a function that computes xy under following conditions.
a) Time complexity of the function should be O(Log y)
b) Extra Space is O(1)
Examples:
Input: x = 3, y = 5 Output: 243 Input: x = 2, y = 5 Output: 32
We strongly recommend that you click here and practice it, before moving on to the solution.
We have discussed recursive O(Log y) solution for power. The recursive solutions are generally not preferred as they require space on call stack and they involve function call overhead.
Following is implementation to compute xy.
C
// Iterative C program to implement pow(x, n) #include <stdio.h> /* 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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 = 3y = 5 print("Power is ", power(x, y)) # This code is contributed # by ihritik |
chevron_right
filter_none
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 |
chevron_right
filter_none
PHP
<?php // Iterative php program // to implement pow(x, n)> // 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 ?> |
chevron_right
filter_none
Output:
Power is 243
This article is contributed by Udit Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Write a program to calculate pow(x,n)
- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Write a program to print all permutations of a given string
- Write a program to add two numbers in base 14
- Write you own Power without using multiplication(*) and division(/) operators
- Write a function that generates one of 3 numbers according to given probabilities
- Euler's Totient Function
- Euler's Totient function for all numbers smaller than or equal to n
- Reverse and Add Function
- Optimized Euler Totient Function for Multiple Evaluations
- Inbuilt function for calculating LCM in C++
- Maximum and Minimum value of a quadratic function
- One line function for factorial of a number



