I am performing a Project Euler task in Clojure and I want to find the sum of all the even numbers in the fibonacci sequence to a specific number.
Code for a function that does this below. I know that there are faster and easier ways to do this, I'm just experimenting with recursion using a loop and repeating. However, the code does not seem to work; it never returns a response.
(defn fib-even-sum [upto] (loop [previous 1 nxt 1 sum 0] (if (or (<= upto 1) (>= nxt upto)) sum) (if (= (mod nxt 2) 0) (recur nxt (+ previous nxt) (+ sum nxt)) (recur nxt (+ previous nxt) sum))))
I was not sure if I could repeat twice in the same loop or not. I'm not sure if this is causing the problem?
source share