Donat un valor n, trobeu el n'èsim parell Nombre de Fibonacci .
Exemples:
Entrada n = 3
Sortida 34
Explicació Els 3 primers nombres parells de Fibonacci són 0 2 8 34 144 i el tercer és 34.Entrada n = 4
Sortida 144
Explicació Els 4 primers nombres parells de Fibonacci són 0 2 8 34 144 i el quart és 144.
[Enfocament ingenu] Comproveu cada número de Fibonacci un per un
Nosaltres genera tots els nombres de Fibonacci i comproveu cada número un per un si alguna vegada ho és o no
[Enfocament eficient] Ús de la fórmula directa: O(n) temps i O(1) espai
La successió de nombres parells de Fibonacci és 0 2 8 34 144 610 2584... A partir d'aquesta seqüència podem fer-nos una idea que cada tercer nombre en seqüència és parell i la seqüència segueix la fórmula recursiva.
La recurrència de la seqüència parell de Fibonacci és:
Eefn = 4fn-1 + Efn-2
Com funciona la fórmula anterior?
Fem una ullada a la fórmula de Fibonacci original i escrivim-la en forma de Fn-3 i Fn-6 a causa del fet que cada tercer nombre de Fibonacci és parell.
Fn = Fn-1 + Fn-2 [Ampliant els dos termes]
= Fn-2 + Fn-3 + Fn-3 + Fn-4
= Fn-2 + 2Fn-3 + Fn-4 [Ampliació del primer terme]
= Fn-3 + Fn-4 + 2Fn-3 + Fn-4
= 3Fn-3 + 2Fn-4 [Ampliant un Fn-4]
= 3Fn-3 + Fn-4 + Fn-5 + Fn-6 [Combinant Fn-4 i Fn-5]
= 4Fn-3 + Fn-6
Com que cada tercer nombre de Fibonacci és parell Així que si Fn ho és
fins i tot llavors Fn-3 és parell i Fn-6 també és parell. Sigui Fn
xè element parell i marqueu-lo com a EFx.
mvc javaSi Fn és EFx, Fn-3 és el número parell anterior, és a dir, EFx-1
i Fn-6 és anterior a EFx-1, és a dir, EFx-2
Així Fn = 4Fn-3 + Fn-6
que vol dir
EFx = 4EFx-1 + EFx-2
A continuació es mostra una implementació senzilla de la idea
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);
Sortida
8