Suma de números pares de Fibonacci

Dado un límite, encuentre la suma de todos los términos de valor par en la secuencia de Fibonacci por debajo del límite dado.
Los primeros términos de los Números de Fibonacci son, 1, 1, 2 , 3, 5, 8 , 13, 21, 34 , 55, 89, 144 , 233,… (Los números pares están resaltados).
Ejemplos: 
 

Input : limit = 8
Output : 10
Explanation : 2 + 8 = 10

Input : limit = 400;
Output : 188.
Explanation : 2 + 8 + 34 + 144 = 188.

Una solución simple es iterar a través de todos los números de Fibonacci mientras el siguiente número es menor o igual al límite dado. Para cada número, comprueba si es par. Si el número es par, súmalo al resultado.
Una solución eficiente se basa en la siguiente fórmula recursiva para números pares de Fibonacci
 

Recurrence for Even Fibonacci sequence is:
     EFn = 4EFn-1 + EFn-2
with seed values
     EF0 = 0 and EF1 = 2.

EFn represents n'th term in Even Fibonacci sequence.

Consulte esto con más detalles de la fórmula anterior.
Entonces, mientras iteramos sobre los números de Fibonacci, solo generamos números pares de Fibonacci. 
 

C++

// Find the sum of all the even-valued terms in
// the Fibonacci sequence which do not exceed
// given limit.
#include<iostream>
using namespace std;
 
// Returns sum of even Fibonacci numbers which are
// less than or equal to given limit.
int evenFibSum(int limit)
{
    if (limit < 2)
        return 0;
 
    // Initialize first two even  Fibonacci  numbers
    // and their sum
    long long int ef1 = 0, ef2 = 2;
    long long int sum = ef1 + ef2;
 
    // calculating sum of even Fibonacci value
    while (ef2 <= limit)
    {
        // get next even value of Fibonacci sequence
        long long int ef3 = 4*ef2 + ef1;
 
        // If we go beyond limit, we break loop
        if (ef3 > limit)
            break;
 
        // Move to next even number and update sum
        ef1 = ef2;
        ef2 = ef3;
        sum += ef2;
    }
 
    return sum;
}
 
// Driver code
int main()
{
    int limit = 400;
    cout << evenFibSum(limit);
    return 0;
}

Java

// Find the sum of all the even-valued terms in
// the Fibonacci sequence which do not exceed
// given limit.
import java.io.*;
 
class GFG
{
    // Returns sum of even Fibonacci numbers which are
    // less than or equal to given limit.
    static int evenFibSum(int limit)
    {
        if (limit < 2)
            return 0;
     
        // Initialize first two even Fibonacci numbers
        // and their sum
        long ef1 = 0, ef2 = 2;
        long sum = ef1 + ef2;
     
        // calculating sum of even Fibonacci value
        while (ef2 <= limit)
        {
            // get next even value of Fibonacci sequence
            long ef3 = 4 * ef2 + ef1;
     
            // If we go beyond limit, we break loop
            if (ef3 > limit)
                break;
     
            // Move to next even number and update sum
            ef1 = ef2;
            ef2 = ef3;
            sum += ef2;
        }
     
        return(int) sum;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int limit = 400;
        System.out.println(evenFibSum(limit));
         
    }
}
 
// This code is contributed by vt_m.

Python3

# Find the sum of all the even-valued
# terms in the Fibonacci sequence which
# do not exceed given limit.
 
# Returns sum of even Fibonacci numbers which
# are less than or equal to given limit.
def evenFibSum(limit) :
    if (limit < 2) :
        return 0
 
    # Initialize first two even Fibonacci numbers
    # and their sum
    ef1 = 0
    ef2 = 2
    sm= ef1 + ef2
     
    # calculating sum of even Fibonacci value
    while (ef2 <= limit) :
 
        # get next even value of Fibonacci
        # sequence
        ef3 = 4 * ef2 + ef1
 
        # If we go beyond limit, we break loop
        if (ef3 > limit) :
            break
 
        # Move to next even number and update
        # sum
        ef1 = ef2
        ef2 = ef3
        sm = sm + ef2
     
    return sm
 
# Driver code
limit = 400
print(evenFibSum(limit))
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to Find the sum of all
// the even-valued terms in the
// Fibonacci sequence which do not
// exceed given limit.given limit.
using System;
 
class GFG {
     
    // Returns sum of even Fibonacci
    // numbers which are less than or
    // equal to given limit.
    static int evenFibSum(int limit)
    {
        if (limit < 2)
            return 0;
     
        // Initialize first two even
        // Fibonacci numbers and their sum
        long ef1 = 0, ef2 = 2;
        long sum = ef1 + ef2;
     
        // calculating sum of even
        // Fibonacci value
        while (ef2 <= limit)
        {
             
            // get next even value of
            // Fibonacci sequence
            long ef3 = 4 * ef2 + ef1;
     
            // If we go beyond limit,
            // we break loop
            if (ef3 > limit)
                break;
     
            // Move to next even number
            // and update sum
            ef1 = ef2;
            ef2 = ef3;
            sum += ef2;
        }
     
        return(int) sum;
    }
     
    // Driver code
    public static void Main ()
    {
        int limit = 400;
        Console.Write(evenFibSum(limit));
         
    }
}
 
// This code is contributed by Nitin Mittal.

PHP

<?php
// Find the sum of all the
// even-valued terms in the
// Fibonacci sequence which
// do not exceed given limit.
 
// Returns sum of even Fibonacci
// numbers which are less than or
// equal to given limit.
function evenFibSum($limit)
{
    if ($limit < 2)
        return 0;
 
    // Initialize first two even
    // Fibonacci numbers and their sum
    $ef1 = 0; $ef2 = 2;
    $sum = $ef1 + $ef2;
 
    // calculating sum of
    // even Fibonacci value
    while ($ef2 <= $limit)
    {
        // get next even value of
        // Fibonacci sequence
        $ef3 = 4 * $ef2 + $ef1;
 
        // If we go beyond limit,
        // we break loop
        if ($ef3 > $limit)
            break;
 
        // Move to next even number
        // and update sum
        $ef1 = $ef2;
        $ef2 = $ef3;
        $sum += $ef2;
    }
 
    return $sum;
}
 
// Driver code
$limit = 400;
echo(evenFibSum($limit));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
 
// Javascript program to find the sum of all the even-valued terms in
// the Fibonacci sequence which do not exceed
// given limit.
 
     // Returns sum of even Fibonacci numbers which are
    // less than or equal to given limit.
    function evenFibSum(limit)
    {
        if (limit < 2)
            return 0;
       
        // Initialize first two even Fibonacci numbers
        // and their sum
        let ef1 = 0, ef2 = 2;
        let sum = ef1 + ef2;
       
        // calculating sum of even Fibonacci value
        while (ef2 <= limit)
        {
            // get next even value of Fibonacci sequence
            let ef3 = 4 * ef2 + ef1;
       
            // If we go beyond limit, we break loop
            if (ef3 > limit)
                break;
       
            // Move to next even number and update sum
            ef1 = ef2;
            ef2 = ef3;
            sum += ef2;
        }
       
        return sum;
    }
       
// Function call
     
        let limit = 400;
        document.write(evenFibSum(limit));
     
</script>

Producción : 
 

188

Este artículo es una contribución de Nishant_singh(pintu) . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *