recursive dunction in python to find x^y code example
Example 1: is power of python recursion
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))
Example 2: if number is power of base in python recursion
import math
def isPower (n, base):
if base == 1 and n != 1:
return False
if base == 1 and n == 1:
return True
if base == 0 and n != 1:
return False
power = int (math.log(n, base) + 0.5)
return base ** power == n
print(isPower(127,2))
print(isPower(128,2))
print(isPower(27,2))
print(isPower (27,3))
print(isPower (28,3))
print(isPower (2**10,2))
print(isPower (2**12,2))
print(isPower(2,2))
print(isPower(5,5))
print(isPower(10,1))