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>
This commit is contained in:
@ -9,7 +9,7 @@ forumTopicId: 301838
|
||||
<section id='description'>
|
||||
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:
|
||||
<div style='text-align: center;'>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</div>
|
||||
By considering the terms in the Fibonacci sequence that do not exceed the <code>n</code>th term, find the sum of the even-valued terms.
|
||||
By considering the terms in the Fibonacci sequence whose values do not exceed <code>n</code>, find the sum of the even-valued terms.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
@ -22,14 +22,16 @@ By considering the terms in the Fibonacci sequence that do not exceed the <code>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>fiboEvenSum(10)</code> should return 44.
|
||||
testString: assert.strictEqual(fiboEvenSum(10), 44);
|
||||
- text: <code>fiboEvenSum(18)</code> should return 3382.
|
||||
testString: assert.strictEqual(fiboEvenSum(18), 3382);
|
||||
- text: <code>fiboEvenSum(23)</code> should return 60696.
|
||||
testString: assert.strictEqual(fiboEvenSum(23), 60696);
|
||||
- text: <code>fiboEvenSum(43)</code> should return 350704366.
|
||||
testString: assert.strictEqual(fiboEvenSum(43), 350704366);
|
||||
- text: <code>fiboEvenSum(10)</code> should return 10.
|
||||
testString: assert.strictEqual(fiboEvenSum(10), 10);
|
||||
- text: <code>fiboEvenSum(60)</code> should return 44.
|
||||
testString: assert.strictEqual(fiboEvenSum(60), 44);
|
||||
- text: <code>fiboEvenSum(1000)</code> should return 798.
|
||||
testString: assert.strictEqual(fiboEvenSum(1000), 798);
|
||||
- text: <code>fiboEvenSum(100000)</code> should return 60696.
|
||||
testString: assert.strictEqual(fiboEvenSum(100000), 60696);
|
||||
- text: <code>fiboEvenSum(4000000)</code> should return 4613732.
|
||||
testString: assert.strictEqual(fiboEvenSum(4000000), 4613732);
|
||||
- text: Your function should return an <code>even</code> 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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user