欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 递归\for循环_斐波那契数列

程序员文章站 2022-05-03 22:01:14
# 递归 def myAdd(a, b): c = a + b print(c) if c > 100: return return myAdd(a + 1, c) #最大递归深度是1000 myAdd(2, 3) # 功能同上递归 a = 2 b = 3 for i in range(1000):... ......
# 递归
def myadd(a, b):
    c = a + b
    print(c)
    if c > 100:
        return
    return myadd(a + 1, c)   #最大递归深度是1000
myadd(2, 3)

# 功能同上递归
a = 2
b = 3
for i in range(1000):
    c = a + b
    print(c)
    a += 1
    b = c

def myfibo(a, b):
    c = a + b
    print(c)
    if c > 500:
        return
    return myfibo(b, c)

myfibo(0, 1)

a = 0
b = 1
for i in range(1000):
    c = a + b
    print(c)
    a = b
    b = c
    if c > 500:
        break