Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var that = this;
- var a = 1;
- var b = 0;
- var sum = 0;
- // Generate Fibonacci series
- // We're basically flip flopping the values of
- // a & b to store our previous terms for the next round
- function fib(a, b) {
- c = a + b;
- if (a > b) {
- that.b = c;
- } else {
- that.a = c;
- }
- return c;
- }
- // Now recurse through the Fibonacci sequence up to 4,000,000
- // and get the sum of all the even numbered terms
- function getFib() {
- f = fib(that.a, that.b);
- if (f < 4000000) {
- if ((f % 2) === 0) {
- that.sum = that.sum + f;
- }
- that.getFib();
- }
- }
- getFib(); // Start
- document.writeln(sum); // Output
Add Comment
Please, Sign In to add comment