Skip to content

Instantly share code, notes, and snippets.

@atushi
Created July 12, 2013 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atushi/5983201 to your computer and use it in GitHub Desktop.
Save atushi/5983201 to your computer and use it in GitHub Desktop.
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…
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);
@Lemoneey
Copy link

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment