On Nov 4, 2010, at 1:32 PM, cej38 wrote:
> It is wonderful that people are so willing to help with a specific
> problem, and I encourage you to continue doing so, but I don't think
> anyone has answered the real question. Is there material out there
> that describes some of the mechanisms, tools,
It is wonderful that people are so willing to help with a specific
problem, and I encourage you to continue doing so, but I don't think
anyone has answered the real question. Is there material out there
that describes some of the mechanisms, tools, ideas, etc. that will
allow the average clojure u
If you're looking to generate all the numbers with exactly two prime
factors (counting multiplicity), I have clojure code that generates
the ones up to 10^8 in under two minutes:
com.example.sbox=> (time (count (semiprimes-to 1)))
"Elapsed time: 106157.33292 msecs"
41803068
It's single-th
If you want to do this, you can do it simpler, without the loop and temp var:
(defn twice-composite? [n]
(->> (take-while #(< % n) prime-seq)
(every #(or (divides? (* 2 %) n)
(not (divides? % n
but this is not what you want. See the hints I sent you off the list.
On We
All the time spent factoring is wasted. You can *construct* and/or
count the semiprimes far more efficiently than your method of
factoring each number one a time in order to filter the semiprimes
from the entire range of numbers up to 10^8. Your approach is
fundamentally slow; this has nothing to
One idea that I had was to inline the factoring logic into twice-
composite. Who cares about the factors? We just want to know if there
are two or not:
(defn twice-composite? [n]
(loop [ps prime-seq
tmp n
p-count 0]
(if (< 2 p-count)
false
(if (= 1 tmp)
Your Clojure implementation of your particular approach is reasonable,
but you need a cleverer approach. I'll send you a hint offline.
--
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
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
Yes, on the handful of Project Euler exercises I've attempted, my
algorithm choice was frequently the source of poor performance.
Are you familiar with the clojure-euler wiki at
http://clojure-euler.wikispaces.com/
? After I do an Euler exercise I compare my code and runtime with
other Clojure s
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
10 matches
Mail list logo