# Project Euler No. 2 in Emacs elisp

My second Project Euler solution.

Project Euler No. 2

This was a little tougher to solve than No. 1. I decided to use recursion to solve it, and as I haven’t had to write anything that used recursion since I was at Uni, it took me a while go remember how to do it. Also, it was an elisp learning exercise.

Things I learnt:

1. successive statements inside an if statement need to be inside a progn.
2. passing in the evens variable was the way to solve this without needing a static variable. Thanks to spacebat for that one. Apparently that is the functional programming way of doing things.
```;; Project Euler no 2. Calculate the sum of all even terms in the
;; fibonacci sequence of numbers that do not exceed 4,000,000

(defun fibonacci (x y)
"calculate the next number in the fibonacci sequenece"
(+ x y))

(defun fibonacci-sum  (fib1 fib2 target &optional evens)
"find the next fibonacci number bigger than target, given fib1 and fib2"
(unless evens
(setq evens 0))
(let ((fib3 (fibonacci fib1 fib2 ))
)
(if (<=  fib3 target)
(progn (if (zerop (mod fib3 2))
(setq evens (+ fib3 evens)))
(message (format "fib3 is: %d, evens: %d" fib3 evens))
(setq fib3  (fibonacci-sum fib2 fib3 target evens)))
(format "fib3: %d > %d target, evens = %d" fib3 target evens)
)
)
)

(fibonacci-sum 0 1 4000000)
```