I've tried Project Euler 2 now.
For those unfamiliar, Project Euler Problem 2 states: find the sum of
all even-valued fibonacci terms that are less than four million.
Let's assume that I have the code to fill the vector:
(def fib-seq
((fn rfib [a b]
(lazy-seq (cons a (rfib b (+ a b)))))
0 1))
(def fib-vec
(into [] (filter even? (take 35 fib-seq))))
Many people reading the Clojure Samples will be familiar with the
first sample. My own implementation did not want the 'take' function
to work. However, that is a question for another day. Assume that the
magic number 35 in the second function does indeed give me all even-
valued fibonacci terms under four million. (I did not know how to
specify a predicate depending on the value of (fib-seq) to check if
the value is under 4 million.)
With the function '(reduce + (fib-vec))' I effortlessly summed up all
values in the vector. This is peculiar, because I first wanted to
write a function that does this for me, but to no avail.
(defn answer []
(let [ansVec [0]]
(while (not-empty fib-vec)
(conj ansVec (+ (last ansVec) (last fib-vec)))
(into fib-vec (pop fib-vec)))))
This function never terminates. Why not? Where am I thinking wrong?
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en