📌  相关文章
📜  第 n 个斐波那契数的Python程序

📅  最后修改于: 2022-05-13 01:56:56.385000             🧑  作者: Mango

第 n 个斐波那契数的Python程序

在数学术语中,斐波那契数列 Fn 由递归关系定义

Fn = Fn-1 + Fn-2

带有种子值

F0 = 0 and F1 = 1.

方法1(使用递归):

Python3
# Function for nth Fibonacci number
 
def Fibonacci(n):
    if n<= 0:
        print("Incorrect input")
    # First Fibonacci number is 0
    elif n == 1:
        return 0
    # Second Fibonacci number is 1
    elif n == 2:
        return 1
    else:
        return Fibonacci(n-1)+Fibonacci(n-2)
 
# Driver Program
 
print(Fibonacci(10))
 
# This code is contributed by Saket Modi


Python3
# Function for nth fibonacci number - Dynamic Programming
# Taking 1st two fibonacci numbers as 0 and 1
 
FibArray = [0, 1]
 
def fibonacci(n):
    if n<0:
        print("Incorrect input")
    elif n<= len(FibArray):
        return FibArray[n-1]
    else:
        temp_fib = fibonacci(n-1)+fibonacci(n-2)
        FibArray.append(temp_fib)
        return temp_fib
 
# Driver Program
 
print(fibonacci(9))
 
# This code is contributed by Saket Modi


Python3
# Function for nth fibonacci number - Space Optimisation
# Taking 1st two fibonacci numbers as 0 and 1
 
def fibonacci(n):
    a = 0
    b = 1
    if n < 0:
        print("Incorrect input")
    elif n == 0:
        return a
    elif n == 1:
        return b
    else:
        for i in range(2, n):
            c = a + b
            a = b
            b = c
        return b
 
# Driver Program
 
print(fibonacci(9))
 
# This code is contributed by Saket Modi


Python3
# creating an array in the function to find the nth number in fibonacci series. [0, 1, 1, ...]
 
def fibonacci (n):
   if n<= 0:
     return "Incorrect Output"
   data = [0, 1]
   if n > 2:
        for i in range (2, n):
            data.append(data[i-1] + data[i-2])
    return data[n-1]
 
# Driver Program
 
print(fibonacci(9))
 
# This Code is contributed by Prasun Parate (prasun_parate)


Python3
# To find the n-th Fibonacci Number using formula
from math import sqrt
# import square-root method from math library
def nthFib(n):
    res = (((1+sqrt(5))**n)-((1-sqrt(5)))**n)/(2**n*sqrt(5))
    # compute the n-th fibonacci number
    print(int(res),'is',str(n)+'th fibonacci number')
    # format and print the number
     
# driver code
nthFib(12)
 
# This code is contributed by Kush Mehta


输出
34

方法2(使用动态规划):

Python3

# Function for nth fibonacci number - Dynamic Programming
# Taking 1st two fibonacci numbers as 0 and 1
 
FibArray = [0, 1]
 
def fibonacci(n):
    if n<0:
        print("Incorrect input")
    elif n<= len(FibArray):
        return FibArray[n-1]
    else:
        temp_fib = fibonacci(n-1)+fibonacci(n-2)
        FibArray.append(temp_fib)
        return temp_fib
 
# Driver Program
 
print(fibonacci(9))
 
# This code is contributed by Saket Modi
输出
21

输出:

21

方法 3(使用空间优化的动态规划):

Python3

# Function for nth fibonacci number - Space Optimisation
# Taking 1st two fibonacci numbers as 0 and 1
 
def fibonacci(n):
    a = 0
    b = 1
    if n < 0:
        print("Incorrect input")
    elif n == 0:
        return a
    elif n == 1:
        return b
    else:
        for i in range(2, n):
            c = a + b
            a = b
            b = c
        return b
 
# Driver Program
 
print(fibonacci(9))
 
# This code is contributed by Saket Modi
输出
21

输出:

21

方法 4(使用数组):

Python3

# creating an array in the function to find the nth number in fibonacci series. [0, 1, 1, ...]
 
def fibonacci (n):
   if n<= 0:
     return "Incorrect Output"
   data = [0, 1]
   if n > 2:
        for i in range (2, n):
            data.append(data[i-1] + data[i-2])
    return data[n-1]
 
# Driver Program
 
print(fibonacci(9))
 
# This Code is contributed by Prasun Parate (prasun_parate)

输出 :

144

解释:

[0、1、1、2、3、5、8、13、21、34、55、89、144]
我们知道斐波那契数列是前两项之和,所以如果我们在程序中输入 12 作为输入,那么我们应该得到 144 作为输出。这就是结果。

方法5(使用直接公式):

求第 n 个斐波那契数的公式如下:

\normalsize Fibonacci\ number\ F_n\\ (1)\ F_n=F_{n-1}+F_{n-2},\hspace{5px} F_1=1,\ F_2=1\\ (2)\ F_n={\large\frac{(1+\sqrt5)^n-(1-\sqrt5)^n}{2^n\sqrt5}}\\

Python3

# To find the n-th Fibonacci Number using formula
from math import sqrt
# import square-root method from math library
def nthFib(n):
    res = (((1+sqrt(5))**n)-((1-sqrt(5)))**n)/(2**n*sqrt(5))
    # compute the n-th fibonacci number
    print(int(res),'is',str(n)+'th fibonacci number')
    # format and print the number
     
# driver code
nthFib(12)
 
# This code is contributed by Kush Mehta
输出
144 is 12th fibonacci number

有关详细信息,请参阅有关斐波那契数列程序的完整文章!