Python_ONLINE_ recursive problem sets _1

1.1 uses a recursive implementation: calculate the factorial of a number

def func(x):
    if x == 2:
        return 2
    else:
        return x*func(x-1)
a = func(4)
print(a)

24

1.2 to achieve access to the value of the number of columns Feibolaqi n-th number in a recursive function

ps (Fibonacci number: 3 starting from the number three, a number equal to the front and rear two numbers: 0,1,1,2,3,5,8,13,21,34,55,89, 144 ......

. 1  DEF an_func (n):     # the number of the n number of rows required Feibolaqi 
2      IF n. 1 == :
 . 3          return 0
 . 4      elif n == 2 :
 . 5          return . 1
 . 6      the else :
 . 7          return an_func (. 1-n) an_func + (2-n- )
 . 8 n-int = (INPUT ( " you want to see the number Feibolaqi which the number of columns (e.g. 3 represents the number 3): " ))
 . 9  Print ( " Feibolaqi {} th column number number: {} " .format (n-, an_func (n-)))

What would you like to see Feibolaqi number of columns (such as the number 3 represents 3): 10
Feibolaqi number of columns to the number 10: 34

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/liulian999/p/12081897.html