Skip to content

Commit 7a1c05c

Browse files
authored
Merge pull request #537 from sir-gon/feature/euler002
[Hacker Rank]: Project Euler #2: Even Fibonacci numbers solved ✓
2 parents 24860b5 + 94a9a90 commit 7a1c05c

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed
Lines changed: 47 additions & 0 deletions
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 $.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/projecteuler/euler002.md]]
3+
*/
4+
5+
export function fiboEvenSum(n) {
6+
let fibo1 = 1n;
7+
let fibo2 = 1n;
8+
let total = 0n;
9+
10+
while (fibo1 + fibo2 < n) {
11+
const fibo = fibo1 + fibo2;
12+
fibo1 = fibo2;
13+
fibo2 = fibo;
14+
15+
if (fibo % 2n === 0n) {
16+
total += fibo;
17+
}
18+
}
19+
20+
return total;
21+
}
22+
23+
export function euler002(n) {
24+
return fiboEvenSum(n);
25+
}
26+
27+
export default { euler002 };
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../logger.js';
3+
4+
import { euler002 } from './euler002.js';
5+
6+
import TEST_CASES from './euler002.testcases.json';
7+
8+
describe('euler002', () => {
9+
it('euler002 JSON Test cases', () => {
10+
expect.assertions(2);
11+
12+
TEST_CASES.forEach((test) => {
13+
const calculated = euler002(test.n);
14+
console.log(`euler002(${test.n}) solution found: ${test.expected}`);
15+
16+
expect(`${calculated}`).toBe(`${test.expected}`);
17+
});
18+
});
19+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{ "n": 10, "expected": 10 },
3+
{ "n": 100, "expected": 44 }
4+
]

0 commit comments

Comments
 (0)