2.el 773 B

12345678910111213141516171819202122232425262728293031323334
  1. ;; Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10
  2. ;; terms will be:
  3. ;; 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  4. ;; By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
  5. (defvar x 1)
  6. (defvar y 2)
  7. ;; we are setting the sum equal to the sum of the first 3 fibonacci numbers: 1, 1, 2
  8. (defvar sum 4) ;; sum = 1 + 1 + 2
  9. (defun next-fib-number ()
  10. "finds the next fibonacci number"
  11. (cond
  12. ((< x y)
  13. (setq x (+ x y)))
  14. ((< y x)
  15. (setq y (+ x y)))))
  16. (next-fib-number)
  17. (defvar i 0)
  18. (while (< i 4000000)
  19. (setq sum (+ sum (next-fib-number)))
  20. (setq i (+ i 1)))
  21. (print sum)
  22. (provide '2)
  23. ;;; 2.el ends here