-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hacker Rank]: Project Euler #2: Even Fibonacci numbers solved ✓
- Loading branch information
Gonzalo Diaz
committed
Aug 23, 2023
1 parent
08b50d3
commit a32c1f8
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 $. |
This file contains 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,22 @@ | ||
# pylint: disable=C0103:invalid-name | ||
|
||
def fibo_even_sum(n: int) -> int: | ||
total = 0 | ||
fibo = 0 | ||
fibo1 = 1 | ||
fibo2 = 1 | ||
|
||
while fibo1 + fibo2 < n: | ||
fibo = fibo1 + fibo2 | ||
fibo1 = fibo2 | ||
fibo2 = fibo | ||
|
||
if fibo % 2 == 0: | ||
total += fibo | ||
|
||
return total | ||
|
||
|
||
def euler002(n: int) -> int: | ||
|
||
return fibo_even_sum(n) |
This file contains 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,19 @@ | ||
import unittest | ||
from .euler002 import euler002 | ||
|
||
|
||
class TestEuler001(unittest.TestCase): | ||
|
||
def test_euler001(self): | ||
|
||
tests = [ | ||
{'n': 10, 'answer': 10}, | ||
{'n': 100, 'answer': 44} | ||
] | ||
|
||
for _, _tt in enumerate(tests): | ||
|
||
self.assertEqual( | ||
euler002(_tt['n']), _tt['answer'], | ||
f"{_} | euler001({_tt['n']}) must be " | ||
f"=> {_tt['answer']}") |