Python-program for å skrive ut Fibonacci-sekvensen
Fibonacci-tallene er tallene i følgende heltallssekvens. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. I matematiske termer er sekvensen Fn til Fibonacci-tall definert av gjentaksrelasjonen.
F n = F n-1 + F n-2
med frøverdier: F 0 = 0 og F 1 = 1.
Fibonacci-tall ved bruk av Native Approach
Fibonacci-serien med en Python while loop er implementert.
Python3
n> => 10> num1> => 0> num2> => 1> next_number> => num2> count> => 1> while> count <> => n:> > print> (next_number, end> => ' '> )> > count> +> => 1> > num1, num2> => num2, next_number> > next_number> => num1> +> num2> print> ()> |
Produksjon
1 2 3 5 8 13 21 34 55 89
Python-program for Fibonacci-tall som bruker rekursjon
Python Funksjon for å finne det n-te Fibonacci-tallet ved hjelp av Python rekursjon .
Python3
def> Fibonacci(n):> > # Check if input is 0 then it will> > # print incorrect input> > if> n <> 0> :> > print> (> 'Incorrect input'> )> > # Check if n is 0> > # then it will return 0> > elif> n> => => 0> :> > return> 0> > # Check if n is 1,2> > # it will return 1> > elif> n> => => 1> or> n> => => 2> :> > return> 1> > else> :> > return> Fibonacci(n> -> 1> )> +> Fibonacci(n> -> 2> )> # Driver Program> print> (Fibonacci(> 9> ))> |
Produksjon
34
Tidskompleksitet: O(2 ^ n) Eksponentiell
Hjelpeplass: På)
Fibonacci-sekvens ved bruk av DP (dynamisk programmering)
Python dynamisk programmering tar de to første Fibonacci-tallene som 0 og 1.
Python3
# Function for nth fibonacci> # number> FibArray> => [> 0> ,> 1> ]> def> fibonacci(n):> > > # Check is n is less> > # than 0> > if> n <> 0> :> > print> (> 'Incorrect input'> )> > > # Check is n is less> > # than len(FibArray)> > elif> n <> len> (FibArray):> > return> FibArray[n]> > else> :> > FibArray.append(fibonacci(n> -> 1> )> +> fibonacci(n> -> 2> ))> > return> FibArray[n]> # Driver Program> print> (fibonacci(> 9> ))> |
Produksjon
34
Tidskompleksitet: På)
Hjelpeplass: På)
Optimalisering av Fibonacci-sekvensen
Her, også Space Optimization Tar de to første Fibonacci-tallene som 0 og 1.
Python3
# Function for nth fibonacci number> def> fibonacci(n):> > a> => 0> > b> => 1> > > # Check is n is less> > # than 0> > if> n <> 0> :> > print> (> 'Incorrect input'> )> > > # Check is n is equal> > # to 0> > elif> n> => => 0> :> > return> 0> > > # Check if n is equal to 1> > elif> n> => => 1> :> > return> b> > else> :> > for> i> in> range> (> 1> , n):> > c> => a> +> b> > a> => b> > b> => c> > return> b> # Driver Program> print> (fibonacci(> 9> ))> |
Produksjon
34
Tidskompleksitet: På)
Hjelpeplass: O(1)
Fibonacci-sekvens ved hjelp av cache
lru_cache vil lagre resultatet slik at vi ikke trenger å finne Fibonacci for samme nummer igjen.
Python3
from> functools> import> lru_cache> # Function for nth Fibonacci number> @lru_cache> (> None> )> def> fibonacci(num:> int> )> -> >> int> :> > # check if num is less than 0> > # it will return none> > if> num <> 0> :> > print> (> 'Incorrect input'> )> > return> > # check if num between 1, 0> > # it will return num> > elif> num <> 2> :> > return> num> > # return the fibonacci of num - 1 & num - 2> > return> fibonacci(num> -> 1> )> +> fibonacci(num> -> 2> )> # Driver Program> print> (fibonacci(> 9> ))> |
Produksjon
34
Tidskompleksitet: På)
Hjelpeplass: På)
Fibonacci-sekvens ved hjelp av tilbakesporing
Funksjon for n-te Fibonacci-nummer ved hjelp av Python3
def> fibonacci(n, memo> => {}):> > if> n <> => 0> :> > return> 0> > elif> n> => => 1> :> > return> 1> > elif> n> in> memo:> > return> memo[n]> > else> :> > memo[n]> => fibonacci(n> -> 1> )> +> fibonacci(n> -> 2> )> > return> memo[n]> # Driver Program> print> (fibonacci(> 9> ))> |
Produksjon
34
Tidskompleksitet: På)
Hjelpeplass: På)
Vennligst se fullstendig artikkel om Program for Fibonacci-tall for flere detaljer!