Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Hacker Rank]: Project Euler #2: Even Fibonacci numbers solved ✓ #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
sir-gon merged 2 commits into main from feature/euler002
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Hacker Rank]: Project Euler #2: Even Fibonacci numbers solved ✓
  • Loading branch information
Gonzalo Diaz committed May 8, 2024
commit ce45cf52cad7c49a933ac6259655f3849af5f718
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace algorithm_exercises_csharp.hackerrank.prohecteuler;

[TestClass]
public class Euler002Test
{
public class Euler002TestCase {
public int n; public int answer;
}

// dotnet_style_readonly_field = true
private static readonly Euler002TestCase[] tests = [
new() { n = 10, answer = 10},
new() { n = 100, answer = 44}
];

[TestMethod]
public void Euler002ProblemTest()
{
int result;

foreach (Euler002TestCase test in tests) {
result = Euler002Problem.Euler002(test.n);
Assert.AreEqual(test.answer, result);
}
}
}

34 changes: 34 additions & 0 deletions algorithm-exercises-csharp/src/hackerrank/projecteuler/Euler002.cs
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace algorithm_exercises_csharp.hackerrank.prohecteuler;

using System.Diagnostics.CodeAnalysis;

public class Euler002Problem
{
[ExcludeFromCodeCoverage]
protected Euler002Problem() {}

public static int FiboEvenSum(int n) {
int total = 0;
int fibo;
int fibo1 = 1;
int fibo2 = 1;

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

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

return total;
}

// Function to find the sum of all multiples of a and b below n
public static int Euler002(int n)
{
return FiboEvenSum(n);
}
}

AltStyle によって変換されたページ (->オリジナル) /