This one was fun. I also learned a good lesson the hard way about lazy sequences. When I was playing with the code in the REPL everything worked fine, but I got confused when I hooked everything up and started getting empty lists out of functions. It looked like functions were returning the values pointed to by the atoms before I modified them. I'd read about how lazy sequences don't get evaluated until they are required, but it didn't occur to me that "for" would return immediately, without evaluating anything. In the REPL this doesn't happen of course.
Was also my first look at maps, and the loop macro.
Solves in about 40sec
(defn- next-int [n]
(if (even? n)
(/ n 2)
(+ (* n 3) 1)))
(defn- grow [start-num]
(let [sequence (atom (list start-num))]
(loop [head (first @sequence)]
(let [next (next-int head)]
(swap! sequence conj next)
(if (= 1 next)
@sequence
(recur next))))))
(defn- measure-sequences []
(let [counts (atom {})]
(doall
(for [start-num (range 1 1000000)]
(let [sequence (grow start-num)]
(swap! counts assoc start-num (count sequence)))))
@counts))
(defn max-map-entry [map]
(first (sort (comparator #(> (val %1) (val %2))) (seq map))))
(defn solve []
(first (max-map-entry (measure-sequences))))
Org-mode PDF export failure
-
Having replaced my laptop due to a tea related accident, I am finding a few
things that I need to reconfigure. Org exports are one of them. On my new
fedor...
14 years ago