Writing power function for large numbers
Last Updated :
29 Dec, 2022
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 happen
Examples:
Input : x = 5, n = 20
Output : 95367431640625
Input : x = 2, n = 100
Output : 1267650600228229401496703205376
In the above example, 2^100 has 31 digits and it is not possible to store these digits even if we use long long int which can store maximum 18 digits. The idea behind is that multiply x, n times and store result in res[] array.
Here is the algorithm for finding power of a number.
Power(n)
1. Create an array res[] of MAX size and store x in res[] array and initialize res_size as the number of digits in x.
2. Do following for all numbers from i=2 to n
…..Multiply x with res[] and update res[] and res_size to store the multiplication result.
Multiply(res[], x)
1. Initialize carry as 0.
2. Do following for i=0 to res_size-1
….a. Find prod = res[i]*x+carry.
….b. Store last digit of prod in res[i] and remaining digits in carry.
3. Store all digits of carry in res[] and increase res_size by number of digits.
C++
#include <iostream>
using namespace std;
#define MAX 100000
int multiply( int x, int res[], int res_size) {
int carry = 0;
for ( int i = 0; i < res_size; i++) {
int prod = res[i] * x + carry;
res[i] = prod % 10;
carry = prod / 10;
}
while (carry) {
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
void power( int x, int n)
{
if (n == 0 ){
cout<< "1" ;
return ;
}
int res[MAX];
int res_size = 0;
int temp = x;
while (temp != 0) {
res[res_size++] = temp % 10;
temp = temp / 10;
}
for ( int i = 2; i <= n; i++)
res_size = multiply(x, res, res_size);
cout << x << "^" << n << " = " ;
for ( int i = res_size - 1; i >= 0; i--)
cout << res[i];
}
int main() {
int exponent = 100;
int base = 20;
power(base, exponent);
return 0;
}
|
Java
class GFG {
static final int MAX = 100000 ;
static int multiply( int x, int res[], int res_size) {
int carry = 0 ;
for ( int i = 0 ; i < res_size; i++) {
int prod = res[i] * x + carry;
res[i] = prod % 10 ;
carry = prod / 10 ;
}
while (carry > 0 ) {
res[res_size] = carry % 10 ;
carry = carry / 10 ;
res_size++;
}
return res_size;
}
static void power( int x, int n) {
if (n == 0 ){
System.out.print( "1" );
return ;
}
int res[] = new int [MAX];
int res_size = 0 ;
int temp = x;
while (temp != 0 ) {
res[res_size++] = temp % 10 ;
temp = temp / 10 ;
}
for ( int i = 2 ; i <= n; i++)
res_size = multiply(x, res, res_size);
System.out.print(x + "^" + n + " = " );
for ( int i = res_size - 1 ; i >= 0 ; i--)
System.out.print(res[i]);
}
public static void main(String[] args) {
int exponent = 100 ;
int base = 2 ;
power(base, exponent);
}
}
|
Python3
MAX = 100000
def multiply(x, res, res_size):
carry = 0
for i in range (res_size):
prod = res[i] * x + carry
res[i] = prod % 10
carry = prod / / 10
while (carry):
res[res_size] = carry % 10
carry = carry / / 10
res_size + = 1
return res_size
def power(x,n):
if (n = = 0 ) :
print ( "1" )
return
res = [ 0 for i in range ( MAX )]
res_size = 0
temp = x
while (temp ! = 0 ):
res[res_size] = temp % 10 ;
res_size + = 1
temp = temp / / 10
for i in range ( 2 , n + 1 ):
res_size = multiply(x, res, res_size)
print (x , "^" , n , " = " ,end = "")
for i in range (res_size - 1 , - 1 , - 1 ):
print (res[i], end = "")
exponent = 100
base = 2
power(base, exponent)
|
C#
using System;
class GFG {
static int MAX = 100000;
static int multiply( int x, int []res,
int res_size)
{
int carry = 0;
for ( int i = 0; i < res_size; i++)
{
int prod = res[i] * x + carry;
res[i] = prod % 10;
carry = prod / 10;
}
while (carry > 0)
{
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
static void power( int x, int n)
{
if (n == 0 ){
Console.Write( "1" );
return ;
}
int []res = new int [MAX];
int res_size = 0;
int temp = x;
while (temp != 0) {
res[res_size++] = temp % 10;
temp = temp / 10;
}
for ( int i = 2; i <= n; i++)
res_size = multiply(x, res, res_size);
Console.Write(x + "^" + n + " = " );
for ( int i = res_size - 1; i >= 0; i--)
Console.Write(res[i]);
}
public static void Main()
{
int exponent = 100;
int b_ase = 2;
power(b_ase, exponent);
}
}
|
PHP
<?php
function multiply( $x , $res )
{
$carry = 0;
$res_size = count ( $res );
for ( $i = 0;
$i < $res_size ; $i ++)
{
$prod = $res [ $i ] *
$x + $carry ;
$res [ $i ] = $prod % 10;
$carry = (int)( $prod / 10);
}
while ( $carry )
{
if ( $carry % 10)
$res [ $res_size ++] = $carry % 10;
$carry = (int)( $carry / 10);
}
return $res ;
}
function power( $x , $n )
{
if ( $n == 0 ){
echo "1" ;
return ;
}
$res_size = 0;
$res = array ();
$temp = $x ;
while ( $temp != 0)
{
$res [ $res_size ++] = $temp % 10;
$temp = $temp / 10;
}
for ( $i = 2; $i <= $n ; $i ++)
$res = multiply( $x , $res );
echo $x . "^" .
$n . " = " ;
$O = 0;
for ( $i = count ( $res ) - 1;
$i >= 0; $i --, $O ++)
if ( $res [ $i ])
break ;
for ( $i = count ( $res ) - $O - 1;
$i >= 0; $i --)
echo $res [ $i ];
}
$exponent = 100;
$base = 2;
power( $base , $exponent );
?>
|
Javascript
<script>
let MAX = 100000
function multiply( x, res, res_size) {
let carry = 0;
for (let i = 0; i < res_size; i++) {
let prod = res[i] * x + carry;
res[i] = prod % 10;
carry = Math.floor(prod / 10);
}
while (carry) {
res[res_size] = carry % 10;
carry = Math.floor(carry / 10);
res_size++;
}
return res_size;
}
function power( x, n)
{
if (n == 0 ){
document.write( "1" );
return ;
}
let res = new Array(MAX);
let res_size = 0;
let temp = x;
while (temp != 0) {
res[res_size++] = temp % 10;
temp = Math.floor(temp / 10);
}
for (let i = 2; i <= n; i++)
res_size = multiply(x, res, res_size);
document.write( x + "^" + n + " = " );
for (let i = res_size - 1; i >= 0; i--)
document.write(res[i]);
}
let exponent = 100;
let base = 2;
power(base, exponent);
</script>
|
Output
20^100 = 12676506002282294014967032053760000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Time Complexity: O(log(n)+m*log(n)) where n= base and m= exponent.
Auxiliary Space: O(MAX) where MAX = 100000
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...