Implement power function without using multiplication and division operators
Given two positive integers, implement the power function without using multiplication and division operators.
For example, for given two integers, x and y, pow(x, y) should return x raised to the power of y, i.e., xy.
Method 1: Using Recursion
We know that pow(x, y) can be recursively written as:
pow(x, 0) = 1
For example, if x = 7, we can get 7y by adding 7y-1 exactly 7 times to the result. Following is the C++, Java, and Python program that demonstrates it:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> using namespace std; int pow(int a, int b) { if (b == 0) { return 1; } int result = 0; int power = pow(a, b - 1); for (int i = 0; i < a; i++) { result += power; } return result; } int main() { cout << pow(7, 3); return 0; } |
Output:
343
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Main { public static int pow(int a, int b) { if (b == 0) { return 1; } int result = 0; int power = pow(a, b - 1); for (int i = 0; i < a; i++) { result += power; } return result; } public static void main(String[] args) { System.out.print(pow(7, 3)); } } |
Output:
343
Python
Method 2
The idea is if x = ab, then log(x) = b.log(a). Since, x can be expressed as x = elog(x), by substituting the value of log(x) in the equation, we get x = eb.log(a).
Following is the C++, Java, and Python implementation of the idea:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <cmath> using namespace std; int pow(int a, int b) { float logx = 0; for (int i = 0; i < b; i++) { logx += log(a); } return exp(logx); } int main() { cout << pow(2, 10); return 0; } |
Output:
1024
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Main { public static int pow(int a, int b) { float logx = 0; for (int i = 0; i < b; i++) { logx += Math.log(a); } return (int)Math.exp(logx); } public static void main(String[] args) { System.out.print(pow(2, 10)); } } |
Output:
1024
Python
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)