- 2
Fibonacci sequence
# As I wrote elsewhere, recursion can be # a very elegant, but sometimes also # computationally costly to solve problems. # A good example is the Fibonacci sequence. def badFibonacci(n): # By convention, the first two numbers # in the Fibonacci sequence are 1: if (n == 1) or (n == 2): return 1 else: return badFibonacci(n-1) + badFibonacci(n-2) def goodFibonacci(n): # We make use of Python's way # to assign values to two # variables at once: f1, f2
0 Answers