Sum of even Fibonacci terms in JavaScript

In this article, we'll learn how to calculate the sum of even Fibonacci numbers using JavaScript. The Fibonacci sequence is a series where each number is the sum of the two preceding ones, usually starting with 0 and 1.

Understanding the Problem

The Fibonacci sequence starts as: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55... Our task is to find all even Fibonacci numbers up to a given limit and calculate their sum. For example, if the limit is 35, the Fibonacci numbers within this range are: 1, 1, 2, 3, 5, 8, 13, 21, 34. The even numbers are 2, 8, and 34, so their sum is 2 + 8 + 34 = 44.

Algorithm

Here's the step-by-step approach:

Step 1: Define a function sumEvenFibonacci() that takes a limit parameter.

Step 2: Initialize variables: sum (for storing even numbers sum), previous and current (for Fibonacci sequence generation).

Step 3: Use a loop to generate Fibonacci numbers while they're within the limit.

Step 4: Check if each number is even using the modulo operator, and add it to the sum if true.

Step 5: Return the final sum of all even Fibonacci numbers.

Implementation

// Calculate sum of even Fibonacci numbers
function sumEvenFibonacci(limit) {
    let sum = 0;
    let previous = 0;
    let current = 1;

    while (current <= limit) {
        // Check if current number is even
        if (current % 2 === 0) {
            sum += current;
            console.log(`Adding even Fibonacci number: ${current}`);
        }

        // Generate next Fibonacci number
        const next = previous + current;
        previous = current;
        current = next;
    }

    return sum;
}

// Test with different limits
console.log("Sum of even Fibonacci numbers up to 100:");
console.log(sumEvenFibonacci(100));

console.log("\nSum of even Fibonacci numbers up to 4000000:");
console.log(sumEvenFibonacci(4000000));
Sum of even Fibonacci numbers up to 100:
Adding even Fibonacci number: 2
Adding even Fibonacci number: 8
Adding even Fibonacci number: 34
44

Sum of even Fibonacci numbers up to 4000000:
Adding even Fibonacci number: 2
Adding even Fibonacci number: 8
Adding even Fibonacci number: 34
Adding even Fibonacci number: 144
Adding even Fibonacci number: 610
Adding even Fibonacci number: 2584
Adding even Fibonacci number: 10946
Adding even Fibonacci number: 46368
Adding even Fibonacci number: 196418
Adding even Fibonacci number: 832040
4613732

Alternative Approach: Using Recursion

// Recursive approach to generate Fibonacci and sum even numbers
function sumEvenFibonacciRecursive(limit, prev = 0, curr = 1, sum = 0) {
    if (curr > limit) {
        return sum;
    }
    
    // Add to sum if current number is even
    if (curr % 2 === 0) {
        sum += curr;
    }
    
    // Recursive call with next Fibonacci number
    return sumEvenFibonacciRecursive(limit, curr, prev + curr, sum);
}

console.log("Recursive approach result:");
console.log(sumEvenFibonacciRecursive(100));
Recursive approach result:
44

Complexity Analysis

Approach Time Complexity Space Complexity
Iterative O(n) O(1)
Recursive O(n) O(n) - due to call stack

The time complexity is O(n) where n is the number of Fibonacci terms up to the given limit. The iterative approach uses constant space O(1), making it more memory-efficient than the recursive version.

Conclusion

We successfully implemented a solution to calculate the sum of even Fibonacci numbers in JavaScript. The iterative approach is preferred for its constant space complexity, while the recursive version offers a more elegant mathematical representation.

Updated on: 2026-03-15T23:19:00+05:30

646 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements