Sum of even Fibonacci terms in JavaScript


In the given problem statement we have to calculate the sum of even fibonacci numbers with the help of Javascript functionalities. So for solving this task we will use basic functionalities of the language.

Understanding the Problem

The problem at hand is to calculate the sum of even fibonacci numbers or items up to the given limit of the fibonacci number. As you may know, the fibonacci sequence is a series of numbers in which every number is the sum of the two previous numbers. The series usually starts from 0 and 1. So in this problem our task is to calculate the sum of even numbers up to a given range. For example: if the range of the fibonacci series is 35. So first we will find the fibonacci numbers within the given range 35. So the numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34.... So the sum of even numbers is 2 + 8 + 34 = 44. In this 44 is the sum of even numbers of fibonacci sequence up to the given range.

Logic for the given Problem

To solve the above problem we will use Javascript to implement the function which will produce the fibonacci sequence and calculate the sum of the even numbers. The function will have a limit variable as input and will define the maximum value for the fibonacci numbers which will be considered. After that we will initialize the variables to keep track the sum, previous number and the current number in the series.Then we will use a loop which will produce the fibonacci series up to the given limit. After that we will verify the condition for even numbers and in the continuation we will also calculate the sum of those even numbers. And finally we will return the sum of even fibonacci numbers up to the given limit.

Algorithm

Step 1: As we know in this problem we have to calculate the sum of even fibonacci terms. So we will define a function called sumEvenFibonacci and inside this function we will use a variable as a parameter called limit. This limit variable will be the maximum value for the fibonacci number.

Step 2: After defining the function we will use three variables and name them as sum, previous and current. These variables will be used for storing the sum of even numbers, the previous number in the series and the current number which is being iterated in the loop respectively.

Step 3: Now we have defined the variables which are to be used in the loop and computation of the sum. At this stage we will use a loop to iterate the numbers of the series. The condition for the loop is to run the loop until the current value is less than or equal to the limit value.

Step 4: Inside this loop we will have another condition that the current number is an even number or not. If the number is even then add it to the sum variable. Otherwise come out from the if condition and change the value for the current value to the next number in the series.

Step 5: If the current number has exceeded the limit value then break the loop and return the result of sum.

Example

// Calculate sum of even Fibonacci
function sumEvenFibonacci(limit) {
   let sum = 0;
   let previous = 0;
   let current = 1;
 
   while (current <= limit) {
     if (current % 2 === 0) {
      sum += current;
     }
 
     const next = previous + current;
     previous = current;
     current = next;
   }
 
   return sum;
  }
 
  const limit = 40000;
  const result = sumEvenFibonacci(limit);
  console.log(result);

Output

14328

Complexity

The time complexity for calculating the sum of all the even numbers in the fibonacci sequence is O(n), in which n is the number of terms in the sequence up to the given limit. As we have iterated the numbers in the fibonacci series once. So we can say that we are performing constant time operations in this function so the time complexity is linear. The space complexity of the function is O(1) as we have only used a constant amount of memory to store the sum.

Conclusion

So in the problem we have successfully programmed the given problem for calculating the sum of even fibonacci numbers in Javascript. The code first generates the fibonacci numbers and then check for even numbers for getting the sum.

Updated on: 16-Aug-2023

334 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements