Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/hackerrank/projecteuler/euler002.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# [Even Fibonacci numbers](https://www.hackerrank.com/contests/projecteuler/challenges/euler002)

- Difficulty: #easy
- Category: #ProjectEuler+

Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with $ 1 $ and $ 2 $, the first $ 10 $ terms will be:

$$ 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 $$

By considering the terms in the Fibonacci sequence whose values do not exceed
$ N $, find the sum of the even-valued terms.

## Input Format

First line contains $ T $ that denotes the number of test cases. This is
followed by $ T $ lines, each containing an integer, $ N $.

## Constraints

- $ 1 \leq T \leq 10^5 $
- $ 10 \leq N \leq 4 × 10^{16} $

## Output Format

Print the required answer for each test case.

## Sample Input 0

```text
2
10
100
```

## Sample Output 0

```text
10
44
```

## Explanation 0

- For $ N = 10 $, we have $ \{2, 8\} $, sum is $ 10 $.

- For $ N = 100 $, we have $ \{2, 8, 34 \} $, sum is $ 44 $.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

int HACKERRANK_PROJECTEULER_euler002(int n);

#ifdef __cplusplus
} // extern "C"
#endif
27 changes: 27 additions & 0 deletions src/lib/exercises/src/hackerrank/projecteuler/euler002.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <exercises/hackerrank/projecteuler/euler002.h>

/**
* @link Problem definition [[docs/hackerrank/projecteuler/euler002.md]]
*/

int HACKERRANK_PROJECTEULER_fibo_even_sum(int n) {
int fibo1 = 1;
int fibo2 = 1;
int total = 0;

while (fibo1 + fibo2 < n) {
int fibo = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibo;

if (fibo % 2 == 0) {
total += fibo;
}
}

return total;
}

int HACKERRANK_PROJECTEULER_euler002(int n) {
return HACKERRANK_PROJECTEULER_fibo_even_sum(n);
}
28 changes: 28 additions & 0 deletions src/tests/unit/lib/hackerrank/projecteuler/euler002.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/projecteuler/euler002.h>
#include <iostream>
#include <vector>

#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

TEST_CASE("euler002 JSON Test Cases",
"[hackerrank] [jsontestcase] [projecteuler]") {
std::filesystem::path cwd = std::filesystem::current_path();
std::string path =
cwd.string() +
"/unit/lib/hackerrank/projecteuler/euler002.testcases.json";

INFO("euler002 JSON test cases FILE: " << path);

std::ifstream f(path);
json data = json::parse(f);

for (auto testcase : data) {
int result = HACKERRANK_PROJECTEULER_euler002(testcase["n"]);
CHECK(result == testcase["expected"]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "n": 10, "expected": 10 },
{ "n": 100, "expected": 44 }
]