Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Fast-Exponential ) Popular Algorithms | Fast Exponential | THE INQUISITIVE





Fast Exponential

LEVEL:Beginner

Description

Take the two inputs and calculate the power

Input Format

input contains two integers base and exponent

Output Format

print the base power exponenet


Example 1:

Input
10 2
Output
100
Example 2:

Input
2 3
Output
8
Example 3:

Input
5 3
Output
125

oops

Login to see Discussion




Approach


i)create a method power which takes base and exponent as arguments
ii)if exponent is 0 return 1 if exponent is 1 return base
iii) calculate power (base,exponent/2) and store in r
iv)if exponent is even return r*r else return r*baser

v)in main method take base and exponent as input from the user

Time Complexity: O(log m)
Space Complexity: O(1)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution