First page Back Continue Last page Overview Graphics

Way of Working

def fib(n):

""" n-th Fibonacci number """

a, b = 0, 1

for i in range(n):

a, b = b, a + b

return a

>>> from fibonacci import fib

>>> fib(0)

0

>>> fib(1)

1

>>> fib(10)

55

>>> fib(15)

610

>>>

+

def fib(n):

""" n-th Fibonacci number

>>> fib(0)

0

>>> fib(1)

1

>>> fib(10)

55

>>> fib(15)

610

>>>

"""

a, b = 0, 1

for i in range(n):

a, b = b, a + b

return a

if __name__ == "__main__":

import doctest

doctest.testmod()