Write you own Power without using multiplication(*) and division(/) operators
Last Updated :
19 Apr, 2023
Method 1 (Using Nested Loops): We can calculate power by using repeated addition. For example to calculate 5^6.
1) First 5 times add 5, we get 25. (5^2)
2) Then 5 times add 25, we get 125. (5^3)
3) Then 5 times add 125, we get 625 (5^4)
4) Then 5 times add 625, we get 3125 (5^5)
5) Then 5 times add 3125, we get 15625 (5^6)
C++
// C++ code for power function
#include <bits/stdc++.h>
using namespace std;
/* Works only if a >= 0 and b >= 0 */
int pow(int a, int b)
{
if (b == 0)
return 1;
int answer = a;
int increment = a;
int i, j;
for(i = 1; i < b; i++)
{
for(j = 1; j < a; j++)
{
answer += increment;
}
increment = answer;
}
return answer;
}
// Driver Code
int main()
{
cout << pow(5, 3);
return 0;
}
// This code is contributed
// by rathbhupendra
C
#include<stdio.h>
/* Works only if a >= 0 and b >= 0 */
int pow(int a, int b)
{
//base case : anything raised to the power 0 is 1
if (b == 0)
return 1;
int answer = a;
int increment = a;
int i, j;
for(i = 1; i < b; i++)
{
for(j = 1; j < a; j++)
{
answer += increment;
}
increment = answer;
}
return answer;
}
/* driver program to test above function */
int main()
{
printf("\n %d", pow(5, 3));
getchar();
return 0;
}
Java
import java.io.*;
class GFG {
/* Works only if a >= 0 and b >= 0 */
static int pow(int a, int b)
{
if (b == 0)
return 1;
int answer = a;
int increment = a;
int i, j;
for (i = 1; i < b; i++) {
for (j = 1; j < a; j++) {
answer += increment;
}
increment = answer;
}
return answer;
}
// driver program to test above function
public static void main(String[] args)
{
System.out.println(pow(5, 3));
}
}
// This code is contributed by vt_m.
Python
# Python 3 code for power
# function
# Works only if a >= 0 and b >= 0
def pow(a,b):
if(b==0):
return 1
answer=a
increment=a
for i in range(1,b):
for j in range (1,a):
answer+=increment
increment=answer
return answer
# driver code
print(pow(5,3))
# this code is contributed
# by Sam007
C#
using System;
class GFG
{
/* Works only if a >= 0 and b >= 0 */
static int pow(int a, int b)
{
if (b == 0)
return 1;
int answer = a;
int increment = a;
int i, j;
for (i = 1; i < b; i++) {
for (j = 1; j < a; j++) {
answer += increment;
}
increment = answer;
}
return answer;
}
// driver program to test
// above function
public static void Main()
{
Console.Write(pow(5, 3));
}
}
// This code is contributed by Sam007
PHP
<?php
// Works only if a >= 0
// and b >= 0
function poww($a, $b)
{
if ($b == 0)
return 1;
$answer = $a;
$increment = $a;
$i;
$j;
for($i = 1; $i < $b; $i++)
{
for($j = 1; $j < $a; $j++)
{
$answer += $increment;
}
$increment = $answer;
}
return $answer;
}
// Driver Code
echo( poww(5, 3));
// This code is contributed by nitin mittal.
?>
JavaScript
<script>
/* Works only if a >= 0 and b >= 0 */
function pow(a , b)
{
if (b == 0)
return 1;
var answer = a;
var increment = a;
var i, j;
for (i = 1; i < b; i++)
{
for (j = 1; j < a; j++)
{
answer += increment;
}
increment = answer;
}
return answer;
}
// driver program to test above function
document.write(pow(5, 3));
// This code is contributed by shikhasingrajput
</script>
Output :
125
Time Complexity: O(a * b)
Auxiliary Space: O(1)
Method 2 (Using Recursion): Recursively add a to get the multiplication of two numbers. And recursively multiply to get a raise to the power b.
C++
#include<bits/stdc++.h>
using namespace std;
/* A recursive function to get x*y */
int multiply(int x, int y)
{
if(y)
return (x + multiply(x, y - 1));
else
return 0;
}
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
int pow(int a, int b)
{
if(b)
return multiply(a, pow(a, b - 1));
else
return 1;
}
// Driver Code
int main()
{
cout << pow(5, 3);
getchar();
return 0;
}
// This code is contributed
// by Akanksha Rai
C
#include<stdio.h>
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
int pow(int a, int b)
{
if(b)
return multiply(a, pow(a, b-1));
else
return 1;
}
/* A recursive function to get x*y */
int multiply(int x, int y)
{
if(y)
return (x + multiply(x, y-1));
else
return 0;
}
/* driver program to test above functions */
int main()
{
printf("\n %d", pow(5, 3));
getchar();
return 0;
}
Java
import java.io.*;
class GFG {
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
static int pow(int a, int b)
{
if (b > 0)
return multiply(a, pow(a, b - 1));
else
return 1;
}
/* A recursive function to get x*y */
static int multiply(int x, int y)
{
if (y > 0)
return (x + multiply(x, y - 1));
else
return 0;
}
/* driver program to test above functions */
public static void main(String[] args)
{
System.out.println(pow(5, 3));
}
}
// This code is contributed by vt_m.
Python3
def pow(a,b):
if(b):
return multiply(a, pow(a, b-1));
else:
return 1;
# A recursive function to get x*y *
def multiply(x, y):
if (y):
return (x + multiply(x, y-1));
else:
return 0;
# driver program to test above functions *
print(pow(5, 3));
# This code is contributed
# by Sam007
C#
using System;
class GFG
{
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
static int pow(int a, int b)
{
if (b > 0)
return multiply(a, pow(a, b - 1));
else
return 1;
}
/* A recursive function to get x*y */
static int multiply(int x, int y)
{
if (y > 0)
return (x + multiply(x, y - 1));
else
return 0;
}
/* driver program to test above functions */
public static void Main()
{
Console.Write(pow(5, 3));
}
}
// This code is contributed by Sam007
PHP
<?php
/* A recursive function to get a^b
Works only if a >= 0 and b >= 0 */
function p_ow( $a, $b)
{
if($b)
return multiply($a,
p_ow($a, $b - 1));
else
return 1;
}
/* A recursive function
to get x*y */
function multiply($x, $y)
{
if($y)
return ($x + multiply($x, $y - 1));
else
return 0;
}
// Driver Code
echo pow(5, 3);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// A recursive function to get a^b
// Works only if a >= 0 and b >= 0
function pow(a, b)
{
if (b > 0)
return multiply(a, pow(a, b - 1));
else
return 1;
}
// A recursive function to get x*y
function multiply(x, y)
{
if (y > 0)
return (x + multiply(x, y - 1));
else
return 0;
}
// Driver code
document.write(pow(5, 3));
// This code is contributed by gauravrajput1
</script>
Output :
125
Time Complexity: O(b)
Auxiliary Space: O(b)
Method 3 (Using bit masking)
Approach: We can a^n (let's say 3^5) as 3^4 * 3^0 * 3^1 = 3^5, so we can represent 5 as its binary i.e. 101
C++
#include <iostream>
using namespace std;
//function calculating power
long long pow(int a, int n){
int ans=1;
while(n>0){
// calculate last bit(right most) bit of n
int last_bit = n&1;
//if last bit is 1 then multiply ans and a
if(last_bit){
ans = ans*a;
}
//make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
return ans;
}
//driver code
int main() {
cout<<pow(3,5);
return 0;
}
C
#include <stdio.h>
// function calculating power
long long pow_(int a, int n){
int ans = 1;
while(n > 0)
{
// calculate last bit(right most) bit of n
int last_bit = n&1;
// if last bit is 1 then multiply ans and a
if(last_bit){
ans = ans*a;
}
//make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
return ans;
}
// driver code
int main()
{
// pow is an inbuilt function so I have used pow_ as a function name
printf("%lld",pow_(3,5));
return 0;
}
// This code is contributed by akashish_.
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// function calculating power
static int pow(int a, int n){
int ans = 1;
while(n > 0)
{
// calculate last bit(right most) bit of n
int last_bit = n&1;
//if last bit is 1 then multiply ans and a
if(last_bit != 0){
ans = ans*a;
}
//make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
System.out.print(pow(3,5));
}
}
// This code is contributed by code_hunt.
Python3
# function calculating power
def pow(a, n):
ans = 1
while(n > 0):
# calculate last bit(right most) bit of n
last_bit = n&1
# if last bit is 1 then multiply ans and a
if(last_bit):
ans = ans*a
# make a equal to square of a as on
# every succeeding bit it got squared
# like a^0, a^1, a^2, a^4, a^8
a = a*a
n = n >> 1
return ans
# driver code
print(pow(3, 5))
# This code is contributed by shinjanpatra
C#
// C# code to implement the approach
using System;
using System.Numerics;
using System.Collections.Generic;
public class GFG {
// function calculating power
static int pow(int a, int n){
int ans = 1;
while(n > 0)
{
// calculate last bit(right most) bit of n
int last_bit = n&1;
//if last bit is 1 then multiply ans and a
if(last_bit != 0){
ans = ans*a;
}
//make a equal to square of a as on every succeeding bit it got squared like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
return ans;
}
// Driver Code
public static void Main(string[] args)
{
Console.Write(pow(3,5));
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// function calculating power
function pow(a, n){
let ans = 1;
while(n > 0)
{
// calculate last bit(right most) bit of n
let last_bit = n&1;
// if last bit is 1 then multiply ans and a
if(last_bit)
{
ans = ans*a;
}
// make a equal to square of a as on
// every succeeding bit it got squared
// like a^0, a^1, a^2, a^4, a^8
a = a*a;
n = n >> 1;
}
return ans;
}
// driver code
document.write(pow(3,5),"</br>");
// This code is contributed by shinjanpatra
</script>
PHP
<?php
// function calculating power
function p_ow($a, $n){
$ans = 1;
while($n > 0)
{
// calculate last bit(right most) bit of n
$last_bit = $n&1;
// if last bit is 1 then multiply ans and a
if($last_bit)
{
$ans = $ans*$a;
}
// make a equal to square of a as on
// every succeeding bit it got squared
// like a^0, a^1, a^2, a^4, a^8
$a = $a*$a;
$n = $n >> 1;
}
return $ans;
}
// driver code
echo(p_ow(5,3));
?>
Time Complexity: O(log n)
Auxiliary Space: O(1)
Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.
Method 4 (Using Exponentiation by Squaring):
This method is based on the idea that the result of a^b can be obtained by dividing the problem into smaller sub-problems of size b/2 and solving them recursively.
Algorithm:
- If b==0, return 1.
- If b is even, return pow(a*a, b/2).
- If b is odd, return apow(aa, (b-1)/2).
C++
#include <iostream>
using namespace std;
int Pow(int a, int b) {
if (b == 0) {
return 1;
}
if (b % 2 == 0) {
return Pow(a * a, b / 2);
}
else {
return a * Pow(a * a, (b - 1) / 2);
}
}
int main() {
cout << Pow(5, 3); // Output: 125
return 0;
}
// This code is contributed by shivhack999
Java
public class Main {
public static long pow(long a, long b)
{
if (b == 0) {
return 1;
}
if (b % 2 == 0) {
return pow(a * a, b / 2);
}
else {
return a * pow(a * a, (b - 1) / 2);
}
}
public static void main(String[] args)
{
System.out.println(pow(5, 3)); // Output: 125
}
}
// This code is contributed by user_dtewbxkn77n
Python3
def pow(a, b):
if b == 0:
return 1
if b % 2 == 0:
return pow(a*a, b//2)
else:
return a*pow(a*a, (b-1)//2)
print(pow(5,3)) # Output: 125
C#
using System;
class Program {
static int Pow(int a, int b)
{
if (b == 0) {
return 1;
}
if (b % 2 == 0) {
return Pow(a * a, b / 2);
}
else {
return a * Pow(a * a, (b - 1) / 2);
}
}
static void Main(string[] args)
{
Console.WriteLine(Pow(5, 3)); // Output: 125
}
}
JavaScript
function pow(a, b) {
if (b == 0) {
return 1;
}
if (b % 2 == 0) {
return pow(a * a, b / 2);
} else {
return a * pow(a * a, (b - 1) / 2);
}
}
console.log(pow(5, 3)); // Output: 125
Time complexity of the above pow function is O(log b) because the function is dividing the problem into half in each recursive call.
The auxiliary space or space complexity of the function is also O(log b) because the maximum number of recursive calls will be equal to the log base 2 of the input value b.
Similar Reads
Multiplication with a power of 2 Given two numbers x and n, we need to multiply x with 2nExamples : Input : x = 25, n = 3 Output : 200 25 multiplied by 2 raised to power 3 is 200. Input : x = 70, n = 2 Output : 280 A simple solution is to compute n-th power of 2 and then multiply with x. C++ // Simple C/C++ program // to compute x
5 min read
Form a number using corner digits of powers Given two integers N and X. Make a number in such a way that the number contains the first and last digit occurring in N^1, N^2, N^3, .... N^X . Examples : Input : N = 10, X = 5 Output : 1010101010 Explanation : 10^1 = 10 10^2 = 100 10^3 = 1000 10^4 = 10000 10^5 = 100000 Take First and Last Digit of
8 min read
Find unit digit of x raised to power y Given two numbers x and y, find unit digit of xy. Examples : Input : x = 2, y = 1 Output : 2 Explanation 2^1 = 2 so units digit is 2. Input : x = 4, y = 2 Output : 6 Explanation 4^2 = 16 so units digit is 6. Method 1 (Simple) Compute value of xy and find its last digit. This method causes overflow f
11 min read
Writing power function for large numbers We have given two numbers x and n which are base and exponent respectively. Write a function to compute x^n where 1 <= x, n <= 10000 and overflow may happenExamples: Input : x = 5, n = 20Output : 95367431640625Input : x = 2, n = 100Output : 1267650600228229401496703205376In the above example,
11 min read
Check if a number can be expressed as x^y (x raised to power y) Given a positive integer n, find if it can be expressed as xy where y > 1 and x > 0. x and y both are integers. Examples : Input: n = 8 Output: true 8 can be expressed as 23 Input: n = 49 Output: true 49 can be expressed as 72 Input: n = 48 Output: false 48 can't be expressed as xyRecommended
11 min read