Tyl*_*ler -3 python recursion python-3.x
我目前正在尝试使用递归将基数提高到 2 的幂,然后将其提高到指数,所以它看起来像x^2^y.
这是我的代码:
def real_multiply(x:int, y:int):
if y == 0:
return x
else:
return x * real_multiply(x,(2**y)-1)
Run Code Online (Sandbox Code Playgroud)
基本情况y==0是2^0返回 1,输出最终是x^1,这将返回x。但是,当我运行此代码时,它会达到递归限制。
有任何想法吗?
我不明白你在递归中做什么,但这是一个典型的幂函数:
def power(x, y):
if y == 1:
return x
else:
return x * power(x, y - 1)
Run Code Online (Sandbox Code Playgroud)