Kun arvo n on annettu, etsi n:s parillinen Fibonaccin numero .
Esimerkkejä:
Syöte n = 3
Lähtö 34
Selitys Ensimmäiset 3 parillista Fibonacci-lukua ovat 0 2 8 34 144 ja kolmas on 34.Syöte n = 4
Lähtö 144
Selitys Ensimmäiset 4 parillista Fibonacci-lukua ovat 0 2 8 34 144 ja neljäs on 144.
[Naiivi lähestymistapa] Tarkista jokainen Fibonacci numero yksitellen
Me luo kaikki Fibonacci-luvut ja tarkista jokainen numero yksitellen, onko se koskaan vai ei
[Tehokas lähestymistapa] Suoran kaavan käyttäminen - O(n) aika ja O(1)-avaruus
Parillinen Fibonaccin sekvenssi on 0 2 8 34 144 610 2584... Tästä sekvenssistä voimme päätellä, että joka kolmas numero peräkkäin on parillinen ja sekvenssi seuraa seuraavaa rekursiivista kaavaa.
Even Fibonacci -sekvenssin toistuminen on:
Eefn = 4fn-1 + Efn-2
Miten yllä oleva kaava toimii?
Katsotaanpa alkuperäistä Fibonacci-kaavaa ja kirjoitetaan se muotoon Fn-3 ja Fn-6, koska joka kolmas Fibonacci-luku on parillinen.
Fn = Fn-1 + Fn-2 [Laajenna molemmat termit]
= Fn-2 + Fn-3 + Fn-3 + Fn-4
= Fn-2 + 2Fn-3 + Fn-4 [Laajentuu ensimmäinen termi]
= Fn-3 + Fn-4 + 2Fn-3 + Fn-4
= 3Fn-3 + 2Fn-4 [Laajenna yksi Fn-4]
= 3Fn-3 + Fn-4 + Fn-5 + Fn-6 [Fn-4:n ja Fn-5:n yhdistäminen]
= 4Fn-3 + Fn-6
Koska joka kolmas Fibonaccin luku on parillinen Joten jos Fn on
silloinkin Fn-3 on parillinen ja Fn-6 myös parillinen. Olkoon Fn
x. parillinen elementti ja merkitse se EFx:ksi.
verilog ainaJos Fn on EFx, niin Fn-3 on edellinen parillinen luku eli EFx-1
ja Fn-6 on EFx-1:tä eli EFx-2:ta edeltävä
Joten Fn = 4Fn-3 + Fn-6
mikä tarkoittaa
EFx = 4EFx-1 + EFx-2
Alla on yksinkertainen toteutus ideasta
C++#include using namespace std; // Optimized function to calculate the nth // even Fibonacci number int nthEvenFibonacci(int n) { // Base case: the first even Fibonacci number is 2 if (n == 1) return 2; // Start with the first two even Fibonacci numbers int prev = 0; // F(0) int curr = 2; // F(3) // We need to find the nth even Fibonacci number for (int i = 2; i <= n; i++) { // Next even Fibonacci number is 4 times // the previous even Fibonacci number plus // the one before that int nextEvenFib = 4 * curr + prev; prev = curr; curr = nextEvenFib; } return curr; } int main() { int n = 2; int result = nthEvenFibonacci(n); cout << result << endl; return 0; }
Java public class GfG { // Function to calculate the nth even Fibonacci // number using dynamic programming public static int nthEvenFibonacci(int n) { // Base case: the first even // Fibonacci number is 2 if (n == 1) return 2; // Start with the first two Fibonacci // numbers (even ones) int prev = 0; // F(0) int curr = 2; // F(3) // We need to find the nth even Fibonacci number for (int i = 2; i <= n; i++) { // Next even Fibonacci number is 4 // times the previous even Fibonacci // number plus the one before that int nextEvenFib = 4 * curr + prev; prev = curr; curr = nextEvenFib; } return curr; } public static void main(String[] args) { int n = 2; int result = nthEvenFibonacci(n); System.out.println(result); } }
Python # Function to calculate the nth even # Fibonacci number using dynamic programming def nthEvenFibonacci(n): # Base case: the first even Fibonacci number is 2 if n == 1: return 2 # Start with the first two Fibonacci numbers (even ones) prev = 0 # F(0) curr = 2 # F(3) # We need to find the nth even Fibonacci number for i in range(2 n + 1): # Next even Fibonacci number is 4 times the # previous even Fibonacci number plus the # one before that next_even_fib = 4 * curr + prev prev = curr curr = next_even_fib return curr # Driver code if __name__ == '__main__': n = 2 # Setting n to 2 result = nthEvenFibonacci(n) print(result)
C# using System; class GfG { // Function to calculate the nth even Fibonacci // number using dynamic programming public int NthEvenFibonacci(int n) { // Base case: the first even Fibonacci number is 2 if (n == 1) return 2; // Start with the first two Fibonacci numbers (even ones) int prev = 0; // F(0) int curr = 2; // F(3) // We need to find the nth even Fibonacci number for (int i = 2; i <= n; i++) { // Next even Fibonacci number is 4 times the // previous even Fibonacci number plus the // one before that int nextEvenFib = 4 * curr + prev; prev = curr; curr = nextEvenFib; } return curr; } static void Main() { GfG gfg = new GfG(); int n = 2; int result = gfg.NthEvenFibonacci(n); Console.WriteLine(result); // Output: The nth even Fibonacci number } }
JavaScript // Function to calculate the nth even Fibonacci number using dynamic programming function nthEvenFibonacci(n) { // Base case: the first even Fibonacci number is 2 if (n === 1) return 2; // Start with the first two Fibonacci numbers (even ones) let prev = 0; // F(0) let curr = 2; // F(3) // We need to find the nth even Fibonacci number for (let i = 2; i <= n; i++) { // Next even Fibonacci number is 4 times // the previous even Fibonacci number plus // the one before that let nextEvenFib = 4 * curr + prev; prev = curr; curr = nextEvenFib; } return curr; } // Example usage: const n = 2; // Setting n to 2 const result = nthEvenFibonacci(n); console.log(result);
Lähtö
8