[ edit ]

   1 def rec_fibonacci( n ):
   2     if n == 0:
   3         return( 0 )
   4     elif n == 1:
   5         return( 1 )
   6     else:
   7         return( rec_fibonacci( n - 2 ) + rec_fibonacci( n - 1 ) )

   1 def fibonacci( n ):
   2     fib = int()
   3     fib_minus_2 = 0 # 
   4     fib_minus_1 = 1 # 
   5     if n == 0:
   6         fib = fib_minus_2
   7     elif n == 1: 
   8         fib = fib_minus_1
   9     else:
  10         for i in range( 2, n+1 ):
  11             fib = fib_minus_2 + fib_minus_1
  12             fib_minus_2 = fib_minus_1
  13             fib_minus_1 = fib
  14     return( fib )

SoftEng: Python/Prog/Practice/Basic/02/BP_005a/Digest (last edited 2019-10-16 14:37:04 by TadashiIijima)