Power Recursive
Python
Medium
2 views
Problem Description
Read integers a and b (b>=0), write recursive function powr(a,b) and output a^b.
Input Format
One line: a b.
Output Format
One integer power.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<2: sys.exit(0)
a=int(p[0]); b=int(p[1])
def powr(x,y):
if y==0:
return 1
if y==1:
return x
half=powr(x,y//2)
if y%2==0:
return half*half
return half*half*x
sys.stdout.write(str(powr(a,b)))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!