Reputation: 133
I was attempting to do some problems on project euler. For the second one I did get the correct answer but I cheated a bit.
Heres the problem:
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.
Heres my code:
var fib = [1,2];
var i = 0;
var sum = 0;
while (fib[0] + fib[1] < 4000000){
i = fib[0] + fib[1];
console.log(i); //just to show myself the number came out correctly.
fib[0] = fib[1];
fib[1] = i;
if (i % 2 === 0){
sum += i;
}
}
console.log(sum + 2);
I added sum by 2 because I can't figure a way for the code to add the initial fib[1], which is a even number itself. Thanks.
Upvotes: 0
Views: 2448
Reputation: 1
function firstNofFibonacci(n)
{
let fibArr = [];
for (let i = 0; i < n; i++) {
if (fibArr.length > 1) {
fibArr.push(fibArr[fibArr.length - 1] +
fibArr[fibArr.length - 2]);
} else {
fibArr.push(i);
};
};
return fibArr;
}
Upvotes: 0
Reputation: 31
let num1 = 0;
let num2 = 1;
let total = 0;
let num3 = 0;
let i = 0;
while(num1 + num2 < 4000000){
num3 = num1 + num2;
num1 = num2; //as loop keep going is changes the position of number
num2 = num3; //as loop keep going is changes the position of number
if(num3 % 2 === 0 ){ // check weather number is even or not
total += num3; //every even number gets added
}
i++
}
console.log(total);
Upvotes: 0
Reputation: 472
Simple solution
var sum = 0;
var term1 = 1;
var term2 = 1;
while (term2 < 4000000) {
term2 = term1 + term2;
term1 = term2 - term1;
sum += term2 % 2 === 0 ? term2 : 0;
}
console.log(sum);
Upvotes: 2
Reputation: 1
This is my code for the same problem. It is not perfect but I hope this will help someone who reads this.
function fibonacci(stop) {
var a = 1, b = 0, c, storage = []
for (var i = 0; a < stop; i++) {
c = a
a = a + b
b = c
if (b%2 == 0) {
let even = b
storage.unshift(even)
}
}
var all = storage.reduce((x, y) => {return x + y}, 0)
console.log(all)
}
fibonacci(4000000); // 4613732
well, what I did here is that I create a function with the stop parameter (where you tell the function to stop) and then I create variables to make the Fibonacci sequence and create a variable where I can store the even values. Then push all the even values in the array, at the end of the function I add all the values in a variable and console.log() the value
If you have suggestions or comments all ears :)
Upvotes: -1
Reputation: 19
for (var sum = 0, i = 0, j = 0, k = 1; 4e6 > j + k; i++) i = j + k, j = k, k = i, sum += 0 == i % 2 ? i : 0;
It could help you.
Upvotes: 0
Reputation: 816
If you start with [1, 2], the first fibonacci number that you get is 3.
In your loop you have:
i = 1+2 = 3
i = 2+3 = 5
So simply start with [0, 1]:
i = 0+1 = 1
i = 1+1 = 2
i = 1+2 = 3
Fixed code:
var fib = [0,1];
var i = 0;
var sum = 0;
while (fib[0]+fib[1] < 4000000){
i= fib[0]+fib[1];
fib[0]=fib[1];
fib[1]=i;
if(i%2 === 0){
sum += i;
}
}
console.log(sum);
Upvotes: 1