diff --git a/docs/hackerrank/projecteuler/euler002.md b/docs/hackerrank/projecteuler/euler002.md new file mode 100644 index 00000000..27c7fb0c --- /dev/null +++ b/docs/hackerrank/projecteuler/euler002.md @@ -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 $. diff --git a/src/hackerrank/projecteuler/euler002.test.ts b/src/hackerrank/projecteuler/euler002.test.ts new file mode 100755 index 00000000..89704b94 --- /dev/null +++ b/src/hackerrank/projecteuler/euler002.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from '@jest/globals'; +import { logger as console } from '../../logger'; + +import { euler002 } from './euler002'; + +import TEST_CASES from './euler002.testcases.json'; + +describe('euler002', () => { + it('euler002 JSON Test cases', () => { + expect.assertions(2); + + TEST_CASES.forEach((test) => { + const calculated = euler002(test.n); + console.log(`euler002(${test.n}) solution found: ${test.expected}`); + + expect(`${calculated}`).toBe(`${test.expected}`); + }); + }); +}); diff --git a/src/hackerrank/projecteuler/euler002.testcases.json b/src/hackerrank/projecteuler/euler002.testcases.json new file mode 100644 index 00000000..5af57f0a --- /dev/null +++ b/src/hackerrank/projecteuler/euler002.testcases.json @@ -0,0 +1,4 @@ +[ + { "n": 10, "expected": 10 }, + { "n": 100, "expected": 44 } +] diff --git a/src/hackerrank/projecteuler/euler002.ts b/src/hackerrank/projecteuler/euler002.ts new file mode 100644 index 00000000..91c07ab0 --- /dev/null +++ b/src/hackerrank/projecteuler/euler002.ts @@ -0,0 +1,27 @@ +/** + * @link Problem definition [[docs/hackerrank/projecteuler/euler002.md]] + */ + +export function fiboEvenSum(n: bigint): bigint { + let fibo1 = 1n; + let fibo2 = 1n; + let total = 0n; + + while (fibo1 + fibo2 < n) { + const fibo = fibo1 + fibo2; + fibo1 = fibo2; + fibo2 = fibo; + + if (fibo % 2n === 0n) { + total += fibo; + } + } + + return total; +} + +export function euler002(n: number): bigint { + return fiboEvenSum(BigInt(n)); +} + +export default { euler002 };