From 8cf64f25396d4fe1a6e7119d38ba56403bd006f7 Mon Sep 17 00:00:00 2001 From: Sidtharthan A N Date: Fri, 7 Feb 2020 21:39:35 +0530 Subject: [PATCH] ProjectEuler | Update Problem statement of 002 to match projecteuler.net (#38101) * Correct ProjectEuler:002 definition and tests From projecteuler.net: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. Correct: take all f(n) where f(n) <= 4,000,000 and f(n) is even Incorrect: take all f(n) where n <= 4,000,000 and f(n) is even * Incorporate PR review suggestios to ProjectEuler 002 Reword the problem statement Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> Co-authored-by: Tom <20648924+moT01@users.noreply.github.com> --- ...roblem-2-even-fibonacci-numbers.english.md | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers.english.md b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers.english.md index f1c0bdbd34..842f47e177 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers.english.md @@ -9,7 +9,7 @@ forumTopicId: 301838
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 that do not exceed the nth term, find the sum of the even-valued terms. +By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
## Instructions @@ -22,14 +22,16 @@ By considering the terms in the Fibonacci sequence that do not exceed the ```yml tests: - - text: fiboEvenSum(10) should return 44. - testString: assert.strictEqual(fiboEvenSum(10), 44); - - text: fiboEvenSum(18) should return 3382. - testString: assert.strictEqual(fiboEvenSum(18), 3382); - - text: fiboEvenSum(23) should return 60696. - testString: assert.strictEqual(fiboEvenSum(23), 60696); - - text: fiboEvenSum(43) should return 350704366. - testString: assert.strictEqual(fiboEvenSum(43), 350704366); + - text: fiboEvenSum(10) should return 10. + testString: assert.strictEqual(fiboEvenSum(10), 10); + - text: fiboEvenSum(60) should return 44. + testString: assert.strictEqual(fiboEvenSum(60), 44); + - text: fiboEvenSum(1000) should return 798. + testString: assert.strictEqual(fiboEvenSum(1000), 798); + - text: fiboEvenSum(100000) should return 60696. + testString: assert.strictEqual(fiboEvenSum(100000), 60696); + - text: fiboEvenSum(4000000) should return 4613732. + testString: assert.strictEqual(fiboEvenSum(4000000), 4613732); - text: Your function should return an even value. testString: assert.equal(fiboEvenSum(10) % 2 === 0, true); @@ -66,16 +68,14 @@ const fiboEvenSum = (number) => { if (number <= 1) { return 0; } else { - let evenSum = 2, first = 1, second = 2, fibNum; // According to problem description our Fibonacci series starts with 1, 2 - for (let i = 3; i <= number; i++) { - fibNum = first + second; - first = second; - second = fibNum; - if (fibNum % 2 == 0) { - evenSum += fibNum; - } + let evenSum = 0, prevFibNum = 1, fibNum = 2; // According to problem description our Fibonacci series starts with 1, 2 + for (let i = 2; fibNum <= number; i++) { + if (fibNum % 2 == 0) { + evenSum += fibNum; } - return evenSum; + [prevFibNum, fibNum] = [fibNum, prevFibNum + fibNum]; + } + return evenSum; } } ```