Skip to content

Commit 2aa408b

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Project Euler #2: Even Fibonacci numbers solved ✓
1 parent a53bfbe commit 2aa408b

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [Even Fibonacci numbers](https://www.hackerrank.com/contests/projecteuler/challenges/euler002)
2+
3+
- Difficulty: #easy
4+
- Category: #ProjectEuler+
5+
6+
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
7+
By starting with $ 1 $ and $ 2 $, the first $ 10 $ terms will be:
8+
9+
$$ 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 $$
10+
11+
By considering the terms in the Fibonacci sequence whose values do not exceed
12+
$ N $, find the sum of the even-valued terms.
13+
14+
## Input Format
15+
16+
First line contains $ T $ that denotes the number of test cases. This is
17+
followed by $ T $ lines, each containing an integer, $ N $.
18+
19+
## Constraints
20+
21+
- $ 1 \leq T \leq 10^5 $
22+
- $ 10 \leq N \leq 4 × 10^{16} $
23+
24+
## Output Format
25+
26+
Print the required answer for each test case.
27+
28+
## Sample Input 0
29+
30+
```text
31+
2
32+
10
33+
100
34+
```
35+
36+
## Sample Output 0
37+
38+
```text
39+
10
40+
44
41+
```
42+
43+
## Explanation 0
44+
45+
- For $ N = 10 $, we have $ \{2, 8\} $, sum is $ 10 $.
46+
47+
- For $ N = 100 $, we have $ \{2, 8, 34 \} $, sum is $ 44 $.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace hackerrank::projecteuler {
4+
5+
int euler002(int n);
6+
7+
} // namespace hackerrank::projecteuler
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <exercises/hackerrank/projecteuler/euler002.hpp>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/projecteuler/euler002.md]]
5+
*/
6+
7+
#include <algorithm>
8+
9+
namespace hackerrank::projecteuler {
10+
11+
int fibo_even_sum(int n) {
12+
int total = 0;
13+
int fibo = 0;
14+
int fibo1 = 1;
15+
int fibo2 = 1;
16+
17+
while (fibo1 + fibo2 < n) {
18+
fibo = fibo1 + fibo2;
19+
fibo1 = fibo2;
20+
fibo2 = fibo;
21+
22+
if (fibo % 2 == 0) {
23+
total += fibo;
24+
}
25+
}
26+
27+
return total;
28+
}
29+
30+
int euler002(int n) { return fibo_even_sum(n); }
31+
32+
} // namespace hackerrank::projecteuler
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/projecteuler/euler002.hpp>
4+
#include <iostream>
5+
#include <vector>
6+
7+
#include <filesystem>
8+
#include <fstream>
9+
#include <nlohmann/json.hpp>
10+
using json = nlohmann::json;
11+
12+
TEST_CASE("euler002 JSON Test Cases",
13+
"[hackerrank] [jsontestcase] [projecteuler]") {
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path =
16+
cwd.string() +
17+
"/unit/lib/hackerrank/projecteuler/euler002.testcases.json";
18+
19+
INFO("euler002 JSON test cases FILE: " << path);
20+
21+
std::ifstream f(path);
22+
json data = json::parse(f);
23+
24+
for (auto testcase : data) {
25+
int result = hackerrank::projecteuler::euler002(testcase["n"]);
26+
CHECK(result == testcase["expected"]);
27+
}
28+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{ "n": 10, "expected": 10 },
3+
{ "n": 100, "expected": 44 }
4+
]

0 commit comments

Comments
 (0)