Skip to content

Commit ce45cf5

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

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-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,34 @@
1+
namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
2+
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
public class Euler002Problem
6+
{
7+
[ExcludeFromCodeCoverage]
8+
protected Euler002Problem() {}
9+
10+
public static int FiboEvenSum(int n) {
11+
int total = 0;
12+
int fibo;
13+
int fibo1 = 1;
14+
int fibo2 = 1;
15+
16+
while (fibo1 + fibo2 < n) {
17+
fibo = fibo1 + fibo2;
18+
fibo1 = fibo2;
19+
fibo2 = fibo;
20+
21+
if(fibo % 2 == 0) {
22+
total += fibo;
23+
}
24+
}
25+
26+
return total;
27+
}
28+
29+
// Function to find the sum of all multiples of a and b below n
30+
public static int Euler002(int n)
31+
{
32+
return FiboEvenSum(n);
33+
}
34+
}

0 commit comments

Comments
 (0)