So the solution in part 1 was no good - it took half an hour to run. I spent a while trying and failing to make a lazy, purely functional version but after talking to Ed about his solution I came up with this, which runs in about 500msec :
(defn factor? [big small]
(zero? (mod big small)))
(defn prime? [n]
(not-any? #(factor? n %) (range 2 n)))
(defn primes []
(filter #(prime? %) (iterate inc 2)))
(defn prime-factors [n]
(let [factors (atom ())]
(last
(for [candidate (primes) :while (< (apply * @factors) n)]
(if (factor? n candidate)
(swap! factors conj candidate))))))
(defn solve []
(first (prime-factors 600851475143)))
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
No comments:
Post a Comment