Home Misc Find the sum of even Fibonacci numbers.

# Find the sum of even Fibonacci numbers.

0 comment

Question: Find the sum of even fibonacci numbers upto a limit N.

Input:
100
Output: 44

Fibonacci numbers are a miracle of Math and are defined as:-

.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
f(0) = 0
f(1) = 1
f(n) = f(n-1) + f(n-2); for n >= 2Code language: TeX (tex)

Thus the Fibonacci series can be given as
0, 1, 1, 2, 3, 5, 8, 13, 21…

Let us try to understand the question. We need to find all the Fibonacci numbers till a limit ‘N’ and then sum only the even numbers in them.
Example:
N = 10
Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8
Sum of even numbers: 2 + 8 = 10

N = 100
Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
Sum of even numbers: 2 + 8 + 34 = 44

If the value of ‘N’ is less than 10^6, this problem can be approached by a naive method of finding the Fibonacci numbers and then summing the even ones.
Surprisingly, there are very few even Fibonacci numbers till very large limits of N.
Have a look at the Fibonacci series and this interesting page about the first 300 Fibonacci numbers.

For this problem let us consider the maximum limit of N to be 10^16. That should be a pretty good limit. We need to understand the concept of pre-processing in this case. Pre-processing is used in scenarios where we can save a lot of computation power if we have some insight into what we are doing.

For example, here is a list of even Fibonacci numbers till 10^16. It is a very small list.

0
2
8
34
144
610
2584
10946
46368
196418
832040
3524578
14930352
63245986
267914296
1134903170
4807526976
20365011074
86267571272
365435296162
1548008755920
6557470319842
27777890035288
117669030460994
498454011879264
2111485077978050
8944394323791464
37889062373143906Code language: TeX (tex)

Thus we just need to query this list for prime numbers till ‘N’

.hljs > mark.shcb-loc { background-color: #ddf6ff; }public class EvenFibonacciNumbers {

public static void main(String[] args) {

List<Long> evenFibonacciList = new ArrayList<>();

Scanner scanner = new Scanner(System.in);
int cases = scanner.nextInt();
for (int i = 0; i < cases; i++) {

int N = scanner.nextInt();

long sum = 0;
for (Long aLong : evenFibonacciList) {
if (N > aLong)
sum += aLong;
else
break;
}

System.out.println(sum);
}

scanner.close();
}
}
Code language: Java (java)

You might also want to look at:

A working implementation of the code can be found here.
This problem can also be found on Hackerrank and ProjectEuler.

Time Complexity: O(1)

#### You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More