[英]Changing an iterative function into a recursive one
考虑:
def itr(n):
s = 0
for i in range(0, n+1):
s = s + i * i
return s
这是一个简单的迭代 function,我想将其更改为递归 function。
def rec(n):
import math
if n!=0:
s=n-(2*math.sqrt(n))
if s!=0:
return(s+rec(n))
else:
return(n)
else:
return n
这是我尝试做上述事情,但我不太正确。
为什么我的解决方案不起作用? 解决办法是什么?
利用:
def recursive(total, n):
if n == 0:
return total
else:
return recursive(total + n * n, n - 1)
几个想法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.