That value of Number of n in number of n - and with a recursive function factorial Fibonacci

Topics requirements: recursive function evaluations n factorial and Fibonacci value of the n items in the Fibonacci series.

1. find the factorial of n:

A positive integer factorial (factorial) is the product of all positive integers less than and equal to the number, and 0 is 1 factorial.

! Calculated as n = 1 × 2 × 3 × ... × n; recursion formula can be written as n = n × (n-1)!!

Then there are:

def fact(n):
if n==1:
return 1
return n*fact(n-1)
n = int (input ( "Enter a positive integer:")) 
Print (FACT (n-))

 

2. evaluation of the first n items of Number of Fibonacci:

...... 1,1,2,3,5,8,13,21,34 shaped like item 3 from the start, each of which is equal to the sum of the first two, the number of columns is called Fibonacci number ( the Fibonacci  Sequence ).

Recursion formulas is F (n) = F (n -1) + F (n-2), n≥3, F (1) = 1, F (2) = 1.

As a result, there are:

def fib(n):
if n<3:
return 1
return fib(n-1)+ fib(n -2)
n = int (input ( "Enter a positive integer:")) 
Print (FIB (n-))

 
 

Guess you like

Origin www.cnblogs.com/moranqiuxin/p/12109775.html