Guest User

Untitled

a guest
Jul 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. var that = this;
  2. var a = 1;
  3. var b = 0;
  4. var sum = 0;
  5.  
  6. // Generate Fibonacci series
  7. // We're basically flip flopping the values of
  8. // a & b to store our previous terms for the next round
  9. function fib(a, b) {
  10.  
  11. c = a + b;
  12.  
  13. if (a > b) {
  14. that.b = c;
  15. } else {
  16. that.a = c;
  17. }
  18.  
  19. return c;
  20. }
  21.  
  22. // Now recurse through the Fibonacci sequence up to 4,000,000
  23. // and get the sum of all the even numbered terms
  24. function getFib() {
  25.  
  26. f = fib(that.a, that.b);
  27.  
  28. if (f < 4000000) {
  29. if ((f % 2) === 0) {
  30. that.sum = that.sum + f;
  31. }
  32.  
  33. that.getFib();
  34. }
  35. }
  36.  
  37. getFib(); // Start
  38.  
  39. document.writeln(sum); // Output
Add Comment
Please, Sign In to add comment