Wprowadzenie do serii Fibonacciego w C #
Seria Fibonacciego w C # w serii Fibonacciego jest jedną ze słynnych serii sekwencji. Sekwencja to 0, 1, 1, 2, 3, 5, 8… Szereg Fibonacciego zaczyna się od zera, a jeden i następny numer jest sumą dwóch poprzednich liczb. Mówi się, że seria Fibonacciego stworzona przez pana Leonarda Pisano Bigollo w XIII wieku. Seria Fibonacciego jest przydatna w niektórych scenariuszach. Zasadniczo był on pierwotnie używany do rozwiązania problemu królika, tj. Liczby królików urodzonych z pary. Istnieją również inne problemy, w których sekwencja Fibonacciego jest przydatna.
Logika Fibonacciego
Podobnie jak w serii Fibonacciego, liczba jest sumą dwóch poprzednich liczb. Więc jeśli mamy serię Fibonacciego, powiedzmy 0, 1, 1, 2, 3, 5, 8, 13, 21… Zgodnie z tym następnym numerem byłaby suma dwóch poprzednich, takich jak 13 i 21. Więc następny numer to 13 + 21 = 34.
Oto logika generowania serii Fibonacciego
F (n) = F (n-1) + F (n-2)
Gdzie F (n) jest liczbą członów, a F (n-1) + F (n-2) jest sumą poprzednich wartości.
Więc jeśli mamy serie 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89…
Zgodnie z logiką F (n) = F (n-1) + F (n-2)
F (n) = 55 + 89
F (n) = 144
Następny termin to 144.
Różne metody tworzenia serii Fibonacciego
Serie Fibonacciego można generować na wiele sposobów
1. Podejście iteracyjne
Ten sposób jest najłatwiejszym sposobem generowania serii.
Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceFibonacciDemo
(
classProgram
(
staticint Fibonacci(int n)
(
intfirstnumber = 0, secondnumber = 1, result = 0;
if (n == 0) return 0; //It will return the first number of the series
if (n == 1) return 1; // it will return the second number of the series
for (int i = 2; i<= n; i++) // main processing starts from here
(
result = firstnumber + secondnumber;
firstnumber = secondnumber;
secondnumber = result;
)
return result;
)
staticvoid Main(string() args)
(
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i< length; i++)
(
Console.Write("(0) ", Fibonacci(i));
)
Console.ReadKey();
)
)
)
2. Metoda rekurencyjna
To kolejna metoda rozwiązania tego problemu.
Metoda 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceFibonacciDemo
(
classProgram
(
staticint Fibonacci(int n)
(
intfirstnumber = 0, secondnumber = 1, result = 0;
if (n == 0) return 0; //it will return the first number of the series
if (n == 1) return 1; // it will return the second number of the series
return Fibonacci(n-1) + Fibonacci(n-2);
)
staticvoid Main(string() args)
(
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i< length; i++)
(
Console.Write("(0) ", Fibonacci(i));
)
Console.ReadKey();
)
)
)
Metoda 2
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FibonacciSeries
(
class Program
(
public static void Fibonacci
(
int firstnumber,
int secondnumber,
int count,
int length,
)
(
if (count <= length)
(
Console.Write("(0) ", firstnumber);
Fibonacci(secondnumber, firstnumber + secondnumber, count + 1, length);
)
)
public static void Main(string() args)
(
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
Fibonacci(0, 1, 1, length);
Console.ReadKey();
)
)
)
Wynik:
3. Fibonacciego za pomocą Array
Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
(
public static int() Fibonacci(int number)
(
int() a = new int(number);
a(0) = 0;
a(1) = 1;
for (int i = 2; i < number; i++)
(
a(i) = a(i - 2) + a(i - 1);
)
return a;
)
public static void Main(string() args)
(
var b = Fibonacci(10);
foreach (var elements in b)
(
Console.WriteLine(elements);
)
)
)
Wynik:
Jak znaleźć N-ty termin Fibonacciego?
Oto metody
Metoda 1
Kod:
using System;
namespace FibonacciSeries
(
class Program (
public static int NthTerm(int n)
(
if ((n == 0) || (n == 1))
(
return n;
)
else
(
return (NthTerm(n - 1) + NthTerm(n - 2));
)
)
public static void Main(string() args)
(
Console.Write("Enter the nth term of the Fibonacci Series: ");
int number = Convert.ToInt32(Console.ReadLine());
number = number - 1;
Console.Write(NthTerm(number));
Console.ReadKey();
)
)
)
Powyższy kod ma znaleźć n-ty termin z serii Fibonacciego. Na przykład, jeśli chcemy znaleźć 12 termin z serii, wynik wyniósłby 89.
Metoda 2
(O (Log t) Time).
Istnieje jeszcze jedna formuła rekurencji, której można użyć do znalezienia czwartej liczby Fibonacciego Jeśli t jest parzyste, to = t / 2:
F (t) = (2 * F (k-1) + F (k)) * F (k)
Jeśli t jest nieparzyste, to k = (t + 1) / 2
F (t) = F (k) * F (k) + F (k-1) * F (k-1)
Macierz Fibonacciego
Po uzyskaniu wyznacznika otrzymamy (-1) t = Ft + 1Ft-1 - Ft2
FmFt + Fm-1Ft-1 = Fm + t-1
Stawiając t = t + 1,
FmFt + 1 + Fm-1Ft = Fm + t
Wprowadzenie m = t
F2t-1 = Ft2 + Ft-12
F2t = (Ft-1 + Ft + 1) Ft = (2Ft-1 + Ft) Ft
Aby uzyskać formułę, wykonaj następujące czynności
Jeśli t jest parzyste, wstaw k = t / 2
Jeśli t jest nieparzyste, wstaw k = (t + 1) / 2
Tak więc, sortując te liczby, możemy zapobiec ciągłemu wykorzystywaniu przestrzeni pamięci STACK. Daje złożoność czasową O (n). Algorytm rekurencyjny jest mniej wydajny.
Kod:
int f(n) :
if( n==0 || n==1 )
return n;
else
return f(n-1) + f(n-2)
Teraz, gdy powyższy algorytm działa dla n = 4
fn (4)
f (3) f (2)
f (2) f (1) f (1) f (0)
f (1) f (0)
Więc to jest drzewo. Aby obliczyć f (4), musimy obliczyć f (3) if (2) itd. Dla małej wartości 4 f (2) oblicza się dwukrotnie, a f (1) oblicza trzykrotnie. Ta liczba dodatków wzrośnie dla dużych liczb.
Istnieje przypuszczenie, że liczba dodatków wymaganych do obliczenia f (n) wynosi f (n + 1) -1.
Wniosek
Tutaj metoda iteracji jest zawsze preferowana, ponieważ ma szybsze podejście do rozwiązania tego rodzaju problemu. Tutaj przechowujemy pierwszą i drugą liczbę serii Fibonacciego w poprzednim numerze i poprzednim numerze (są to dwie zmienne), a także używamy bieżącej liczby do przechowywania liczby Fibonacciego.
Polecane artykuły
To jest przewodnik po serii Fibonacciego w C #. Tutaj omawiamy logikę serii Fibonacciego za pomocą różnych metod i jak znaleźć n-ty termin serii Fibonacciego. Możesz również przejrzeć nasze inne powiązane artykuły, aby dowiedzieć się więcej-
- Seria Fibonacciego w C
- Kompilatory C #
- Komendy C #
- C # dla pętli
- Przewodnik po serii Fibonacciego w C ++
- Seria Fibonacciego W JavaScript