I'm working on problem 66 at project euler and this is my solution so
far.  It calculates the minimum "x"s just fine but takes almost a
minute when I turn limit up to 20.  It's not going to solve the
problem fast enough but what I find strange it that if I set the limit
to 20 then nothing happens for a while and then the minimum "x"s are
quickly spit out.  Basically, it seems like the sequence in the loop
isn't lazy and I don't understand why.

Algorithm:  For each D iterate over the squares.  The minimum x will
be the first square for which decrementing it and dividing by D is
another square.

(time (let [limit 10
            sqrt (fn [n] (if (zero? n)
                           0
                           (loop [guess (- n (/ (- (* n n) n) (* 2 n))) 
last-guess n]
                             (if (< (- last-guess guess) 1/10)
                               (let [dec-guess (long guess)
                                     inc-guess (inc dec-guess)]
                                 (condp = n
                                     (* dec-guess dec-guess) dec-guess
                                     (* inc-guess inc-guess) inc-guess
                                     guess))
                               (recur (- guess (/ (- (* guess guess) n) (* 2 
guess)))
guess)))))]
        (loop [x (for [D (range (inc limit))
                       :when (not (integer? (sqrt D)))]
                   (sqrt (first (filter (comp integer? #(sqrt (/ (dec %) D)))
                                        (map #(* % %) (range 2 
Double/POSITIVE_INFINITY))))))]
          (when (first x)
            (println (first x))
            (recur (rest x))))))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to