Find the N-th Fibonacci
1 1 2 3 5 8 ……
method one:
DEF fbnq (n-): IF n == 1 or n-== 2 : # of the first and the second number is the number 1, if you want to start from another, such as 1, 2, it is necessary to customize n == 1 when return 1, n == 2 2 when return return 1 the else : return fbnq (the n--1) + fbnq (the n--2) # here we must return to the n-= int (the INPUT ( " you want to ask the first few Fibonacci number of lease -? " )) f = fbnq (the n-) Print (f)
# At this point requires two recursive cases, not suitable for small memory
Method Two:
DEF fbnq2 (n-, L = [0]): L [0] + =. 1 IF n-== 2 : L [0] - =. 1 return 1,1 the else : A, B = fbnq2 (. 1-n- ) L [ 0] -. 1 = # n-2, when reduced, l [0] = - n + 1, return slowly until l [0] == 0, the value is returned, instead of (a, a + b) IF L [0] == 0: return A + b return b, A + b the n- = int (the INPUT ( " ? you want to ask the first few Fibonacci - " )) f = fbnq2 (the n-) Print (f)
# At this point more perfect, it's easy to calculate the larger Fibonacci