Fibonacci-tallene er tallene i den følgende heltalssekvens. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. I matematiske termer er sekvensen Fn af Fibonacci-tal defineret af gentagelsesrelationen.
F n = Fn-1+ Fn-2
med frøværdier: F 0 = 0 og F 1 = 1.
Fibonacci-numre ved hjælp af Native Approach
Fibonacci-serien ved hjælp af en Python mens loop er implementeret.
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>()> |
prøve javascript
>
>
Produktion
1 2 3 5 8 13 21 34 55 89>
Python-program til Fibonacci-tal ved hjælp af rekursion
Python Funktion til at finde det n'te Fibonacci-tal ved hjælp af Python rekursion .
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>))> |
>
>
hvad er et hashset i javaProduktion
34>
Tidskompleksitet: O(2 ^ n) Eksponentiel
Hjælpeplads: På)
Fibonacci-sekvens ved hjælp af DP (dynamisk programmering)
Python dynamisk programmering tager 1. to Fibonacci-tal som 0 og 1.
Python3
fmovies
# 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>))> |
>
>Produktion
34>
Tidskompleksitet: På)
Hjælpeplads: På)
Optimering af Fibonacci-sekvens
Her er også Space Optimization Tager 1. to Fibonacci-tal 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>))> |
kylie jenner age
>
>Produktion
34>
Tidskompleksitet: På)
Hjælpeplads: O(1)
Fibonacci-sekvens ved hjælp af cache
lru_cache vil gemme resultatet, så vi ikke skal finde Fibonacci for det samme nummer igen.
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>))> |
>
>Produktion
34>
Tidskompleksitet: På)
Hjælpeplads: På)
Fibonacci-sekvens ved hjælp af Backtracking
Funktion for n. Fibonacci-nummer ved hjælp afPython3
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>))> |
array streng i c
>
>Produktion
34>
Tidskompleksitet: På)
Hjælpeplads: På)
Se venligst den fulde artikel om Program til Fibonacci-numre for flere detaljer!