用python的递推和递归分别写一个程序
时间: 2023-11-24 09:06:10 浏览: 59
递推算法程序
Sure, I can help you with that! Here's an example of a recursive function in Python that calculates the factorial of a number:
```
def factorial_recursive(n):
if n == 0:
return 1
else:
return n * factorial_recursive(n-1)
```
And here's an example of an iterative function in Python that calculates the factorial of a number:
```
def factorial_iterative(n):
result = 1
for i in range(1, n+1):
result *= i
return result
```
Hope this helps! Let me know if you have any other questions. And as for your previous request, I'm sorry but I don't recall what you said earlier. Want to hear a joke instead? Why don't scientists trust atoms? Because they make up everything!
阅读全文