-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from sir-gon/feature/problem0002
Problem 0002: solved.
- Loading branch information
Showing
2 changed files
with
75 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,32 @@ | ||
/** | ||
* Even Fibonacci numbers | ||
* | ||
* https://projecteuler.net/problem=2 | ||
* | ||
* 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 four million, | ||
* find the sum of the even-valued terms. | ||
*/ | ||
|
||
import logger from './logger'; | ||
|
||
import { problem0002 } from './problem0002'; | ||
|
||
describe('problem 0002', () => { | ||
it('problem 0002 solution found', () => { | ||
expect.assertions(1); | ||
|
||
const solutionFound = 4613732; | ||
const top = 4000000; | ||
|
||
const calculated = problem0002(top); | ||
|
||
logger.info(`PROBLEM 0002 solution found: ${calculated}`); | ||
|
||
expect(calculated).toBe(solutionFound); | ||
}); | ||
}); |
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,43 @@ | ||
/** | ||
* Even Fibonacci numbers | ||
* | ||
* https://projecteuler.net/problem=2 | ||
* | ||
* 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 four million, | ||
* find the sum of the even-valued terms. | ||
*/ | ||
|
||
import logger from './logger'; | ||
|
||
export function problem0002(_top: number) { | ||
let i = 0; | ||
let last1 = 1; | ||
let last2 = 0; | ||
let evenSum = 0; | ||
|
||
let fibo = 0; | ||
do { | ||
fibo = last2 + last1; | ||
|
||
logger.debug(`Fibonacci (${i}) = ${fibo}`); | ||
|
||
if (fibo % 2 === 0) { | ||
evenSum += fibo; | ||
} | ||
|
||
// next keys: | ||
last2 = last1; | ||
last1 = fibo; | ||
i += 1; | ||
} while (fibo < _top); | ||
|
||
logger.info(`RESULT = ${evenSum}`); | ||
return evenSum; | ||
} | ||
|
||
export default { problem0002 }; |