Sorry for the delayed response.

OK... here's an example; my solution to problem 187:

(defn divides? [n d]
  (zero? (rem n d))
)

(declare prime? prime-seq)

(defn prime? [n]
  (if (< n 2)
    false
    (every? #(not (divides? n %)) (take-while #(<= (* % %) n) prime-
seq)))
)

(def prime? (memoize prime?))

(def prime-seq
  (lazy-seq (concat '(2 3 5 7)
    (filter #(prime? %) (map #(+ (* 2 %) 7) (iterate inc 1)))))
)

(defn factorize [n]
  (loop [test-primes prime-seq
         tmp n
         retval '()]
    (if (= 1 tmp)
      retval
      (if (prime? tmp)
        (concat retval (list tmp))
        (let [p (first test-primes)]
          (if (divides? tmp p)
            (recur test-primes (quot tmp p) (concat retval (list p)))
            (recur (rest test-primes) tmp retval))))))
)

(defn twice-composite? [n]
  (= 2 (count (factorize n)))
)

(count (filter twice-composite? (range 1 30)))

This appears to be correct, since I get the answer 10 for this one
case. However, trying to run for n<10^6 takes 6-11 seconds, and for
10^7 at least three minutes, so running for 10^8 is like going to take
on the order of hours.

I know that my prime number generator is nowhere near optimal, and I
know that in order to efficiently solve a lot of the Euler problems,
you cannot simply brute force them; you need to choose your algorithm
wisely.

Anyway... any suggestions would be greatly appreciated.

dan kefford

On Nov 2, 4:52 pm, Alan <a...@malloys.org> wrote:
> Usually it's more about the algorithm than the language. Java can
> generally do things faster than clojure, simply because it has fewer
> layers, but the speedup is a linear factor. If the java solution for
> 1000 elements takes 5ms and your clojure code takes even a second,
> it's likely that clojure isn't to blame. Maybe you could post one of
> your solutions, or simply compare it to a java solution yourself?
>
> On Nov 2, 10:32 am, Dan Kefford <dan_keff...@hotmail.com> wrote:
>
> > Hello fellow clojurians...
>
> > I've been using Clojure now fairly regularly for the past two months
> > solving problems on Project Euler. I've been fairly successful solving
> > them but there are times where the performance of my code either
> > stinks (the answer may come back in 5-10 minutes) or not at all even
> > though I have effectively solved the problem (the code is correct and
> > will return the answer for n < 1000 but for n < 10^8... forget about
> > it.)
>
> > My question is: are there any materials out there on how to optimize
> > performance of Clojure code? There doesn't seem to be a lot out there.
>
> > Thanks in advance,
>
> > dan kefford

-- 
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