Project Euler: Problem 2: Even Fibonacci Numbers - fiboEvenSum(10) sh… (#34722)
This commit is contained in:
committed by
Kristofer Koishigawa
parent
7dac2fc086
commit
39e36a1702
@ -8,7 +8,7 @@ title: 'Problem 2: Even Fibonacci Numbers'
|
|||||||
<section id='description'>
|
<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:
|
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>
|
<div style='text-align: center;'>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</div>
|
||||||
By considering the terms in the Fibonacci sequence whose values do not exceed <code>n</code>th term, find the sum of the even-valued terms.
|
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.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
@ -21,16 +21,16 @@ By considering the terms in the Fibonacci sequence whose values do not exceed <c
|
|||||||
|
|
||||||
```yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: <code>fiboEvenSum(10)</code> should return 188.
|
- text: <code>fiboEvenSum(10)</code> should return 44.
|
||||||
testString: assert.strictEqual(fiboEvenSum(10), 188, '<code>fiboEvenSum(10)</code> should return 188.');
|
testString: assert.strictEqual(fiboEvenSum(10), 44, '<code>fiboEvenSum(10)</code> should return 44.');
|
||||||
|
- text: <code>fiboEvenSum(18)</code> should return 3382.
|
||||||
|
testString: assert.strictEqual(fiboEvenSum(18), 3382, '<code>fiboEvenSum(18)</code> should return 3382.');
|
||||||
- text: <code>fiboEvenSum(23)</code> should return 60696.
|
- text: <code>fiboEvenSum(23)</code> should return 60696.
|
||||||
testString: assert.strictEqual(fiboEvenSum(23), 60696, '<code>fiboEvenSum(23)</code> should return 60696.');
|
testString: assert.strictEqual(fiboEvenSum(23), 60696, '<code>fiboEvenSum(23)</code> should return 60696.');
|
||||||
- text: <code>fiboEvenSum(43)</code> should return 1485607536.
|
- text: <code>fiboEvenSum(43)</code> should return 350704366.
|
||||||
testString: assert.strictEqual(fiboEvenSum(43), 1485607536, '<code>fiboEvenSum(43)</code> should return 1485607536.');
|
testString: assert.strictEqual(fiboEvenSum(43), 350704366, '<code>fiboEvenSum(43)</code> should return 350704366.');
|
||||||
- text: Your function is not returning the correct result using our tests values.
|
|
||||||
testString: assert.strictEqual(fiboEvenSum(18), 3382, 'Your function is not returning the correct result using our tests values.');
|
|
||||||
- text: Your function should return an <code>even</code> value.
|
- text: Your function should return an <code>even</code> value.
|
||||||
testString: assert.equal(fiboEvenSum(31) % 2 === 0, true, 'Your function should return an <code>even</code> value.');
|
testString: assert.equal(fiboEvenSum(10) % 2 === 0, true, 'Your function should return an <code>even</code> value.');
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -62,18 +62,20 @@ fiboEvenSum(10);
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const fiboEvenSum = (number) => {
|
const fiboEvenSum = (number) => {
|
||||||
let temp, sum = 0, a = 0, b = 1;
|
if (number <= 1) {
|
||||||
while (number >= 0) {
|
return 0;
|
||||||
temp = a;
|
} else {
|
||||||
a = b;
|
let evenSum = 2, first = 1, second = 2, fibNum; // According to problem description our Fibonacci series starts with 1, 2
|
||||||
b += temp;
|
for (let i = 3; i <= number; i++) {
|
||||||
number --;
|
fibNum = first + second;
|
||||||
if ((b % 2) === 0) {
|
first = second;
|
||||||
sum += b;
|
second = fibNum;
|
||||||
|
if (fibNum % 2 == 0) {
|
||||||
|
evenSum += fibNum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return evenSum;
|
||||||
return sum;
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user