python yieldキーワードによるフィボナッチ数列の実現
フィボナッチ数列(Fibonacci):
F(1) = 1
F(2) = 1
F(3) = 2
......
F(n) = F(n-1) + F(n-2)
コードは次のとおりです.
F(1) = 1
F(2) = 1
F(3) = 2
......
F(n) = F(n-1) + F(n-2)
python , , ,
コードは次のとおりです.
#
def Fibonacci():
n, a, b = 0, 0, 1
number = int(input(" N :"))
if number <= 0:
number = int(input(" :"))
while n < number:
# next() ,
yield b
# ,
a, b = b, a + b
n = n + 1
# , s
s = Fibonacci()
# N , next(s) , N next(s) ,
# for , s , N
for item in s:
print(item)