Skip to content

Commit 2a22c23

Browse files
authored
Merge pull request #15 from sir-gon/feature/euler002
[Hacker Rank]: Project Euler #2: Even Fibonacci numbers solved ✓
2 parents f73ac89 + e70791b commit 2a22c23

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
2+
3+
[TestClass]
4+
public class Euler002Test
5+
{
6+
public class Euler002TestCase {
7+
public int n; public int answer;
8+
}
9+
10+
// dotnet_style_readonly_field = true
11+
private static readonly Euler002TestCase[] tests = [
12+
new() { n = 10, answer = 10},
13+
new() { n = 100, answer = 44}
14+
];
15+
16+
[TestMethod]
17+
public void Euler002ProblemTest()
18+
{
19+
int result;
20+
21+
foreach (Euler002TestCase test in tests) {
22+
result = Euler002Problem.Euler002(test.n);
23+
Assert.AreEqual(test.answer, result);
24+
}
25+
}
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @link Problem definition [[docs/hackerrank/projecteuler/euler002.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class Euler002Problem
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected Euler002Problem() {}
11+
12+
public static int FiboEvenSum(int n) {
13+
int total = 0;
14+
int fibo;
15+
int fibo1 = 1;
16+
int fibo2 = 1;
17+
18+
while (fibo1 + fibo2 < n) {
19+
fibo = fibo1 + fibo2;
20+
fibo1 = fibo2;
21+
fibo2 = fibo;
22+
23+
if(fibo % 2 == 0) {
24+
total += fibo;
25+
}
26+
}
27+
28+
return total;
29+
}
30+
31+
// Function to find the sum of all multiples of a and b below n
32+
public static int Euler002(int n)
33+
{
34+
return FiboEvenSum(n);
35+
}
36+
}
+47
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 $.

0 commit comments

Comments
 (0)