-
-
Couldn't load subscription status.
- Fork 0
[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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
algorithm-exercises-csharp/src/hackerrank/projecteuler/Euler002.Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
36 changes: 36 additions & 0 deletions
algorithm-exercises-csharp/src/hackerrank/projecteuler/Euler002.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // @link Problem definition [[docs/hackerrank/projecteuler/euler002.md]] | ||
|
|
||
| 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); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
docs/hackerrank/projecteuler/euler002.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 $. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.