Created
July 12, 2013 09:46
Project Euler . Problem 2 . Even Fibonacci numbers : 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 ev…
This file contains hidden or 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
var TARGETNUM = 4000000; | |
var i = 0; | |
var b = 0; | |
var a = 1; | |
var answer = 0; | |
while (true) { | |
if (b%2==0) answer = answer + b; | |
tmp = b; | |
b = a; | |
if (TARGETNUM < b) break; | |
a = tmp + a; | |
i++; | |
} | |
console.log(answer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var sum = 2;
var n = 1;
var sumEven = 0;
while(true) {
if (sum < 4000000) {
sum = sum + n;
n = sum - n;
if (sum % 2 == 0) {
sumEven = sumEven + sum;
}
} else break
}
alert(sumEven);